<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sparkle&#039;s Workshop &#187; Linux</title>
	<atom:link href="http://weavesky.com/category/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://weavesky.com</link>
	<description></description>
	<lastBuildDate>Tue, 24 Apr 2012 03:32:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>在Linux命令行下发送带附件的邮件</title>
		<link>http://weavesky.com/2008/03/10/mail-an-attachment-at-linux/</link>
		<comments>http://weavesky.com/2008/03/10/mail-an-attachment-at-linux/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 12:36:16 +0000</pubDate>
		<dc:creator>Sparkle</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://weavesky.com/2008/03/10/%e5%9c%a8linux%e5%91%bd%e4%bb%a4%e8%a1%8c%e4%b8%8b%e5%8f%91%e9%80%81%e5%b8%a6%e9%99%84%e4%bb%b6%e7%9a%84%e9%82%ae%e4%bb%b6/</guid>
		<description><![CDATA[购买的虚拟主机没有备份服务（另外收费），但是很好地有SSH权限和Cron权限，于是写了一个简单的shell脚本备份打包mysql的数据，然后email到我的邮箱。但是Linux的mail命令并不能发送附件，于是自己写了一个小程序来发送附件。由于我比较熟悉Ruby，于是就用Ruby来实现，当然你也可以用Python或者Perl来实现 to_mail = ARGV&#91;0&#93; backup_name = ARGV&#91;1&#93; backup_attach = ARGV&#91;2&#93; &#160; require 'net/smtp' require 'rubygems' require 'mailfactory' &#160; mail = MailFactory.new mail.to = to_mail mail.from = &#34;backup@weavesky.com&#34; mail.subject = &#34;#{backup_name} backup&#34; mail.text = &#34;no content&#34; mail.attach backup_attach &#160; Net::SMTP.start&#40;&#34;localhost&#34;&#41; do &#124;smtp&#124; smtp.send_message mail.to_s, mail.from, mail.to end 后来我找到一些更简单的做法 uuencode file.txt file.txt &#124; mail email@address.com 不过这种做法是不会产生附件的，仅仅将文件内容编码之后当content发送 如果你的机器上有装mutt的话，就更简单了 <a href='http://weavesky.com/2008/03/10/mail-an-attachment-at-linux/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>购买的虚拟主机没有备份服务（另外收费），但是很好地有SSH权限和Cron权限，于是写了一个简单的shell脚本备份打包mysql的数据，然后email到我的邮箱。但是Linux的mail命令并不能发送附件，于是自己写了一个小程序来发送附件。由于我比较熟悉Ruby，于是就用Ruby来实现，当然你也可以用Python或者Perl来实现</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">to_mail = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
backup_name = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
backup_attach = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/smtp'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mailfactory'</span>
&nbsp;
mail = MailFactory.<span style="color:#9900CC;">new</span>
mail.<span style="color:#9900CC;">to</span> = to_mail
mail.<span style="color:#9900CC;">from</span> = <span style="color:#996600;">&quot;backup@weavesky.com&quot;</span>
mail.<span style="color:#9900CC;">subject</span> = <span style="color:#996600;">&quot;#{backup_name} backup&quot;</span>
mail.<span style="color:#9900CC;">text</span> = <span style="color:#996600;">&quot;no content&quot;</span>
mail.<span style="color:#9900CC;">attach</span> backup_attach
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Net::SMTP</span>.<span style="color:#9900CC;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;localhost&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>smtp<span style="color:#006600; font-weight:bold;">|</span>
  smtp.<span style="color:#9900CC;">send_message</span> mail.<span style="color:#9900CC;">to_s</span>, mail.<span style="color:#9900CC;">from</span>, mail.<span style="color:#9900CC;">to</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>后来我找到一些更简单的做法</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">uuencode file.txt file.txt <span style="color: #000000; font-weight: bold;">|</span> mail email<span style="color: #000000; font-weight: bold;">@</span>address.com</pre></div></div>

<p>不过这种做法是不会产生附件的，仅仅将文件内容编码之后当content发送</p>
<p>如果你的机器上有装mutt的话，就更简单了</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;no content&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> mutt <span style="color: #660033;">-s</span> <span style="color: #ff0000;">&quot;subject&quot;</span> <span style="color: #660033;">-a</span> file.txt email<span style="color: #000000; font-weight: bold;">@</span>address.com</pre></div></div>

<p>我现在就是用这种做法</p>
]]></content:encoded>
			<wfw:commentRss>http://weavesky.com/2008/03/10/mail-an-attachment-at-linux/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

