<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Mail on Details...</title>
    <link>https://www.deepreflect.net/tags/mail/</link>
    <description>Recent content in Mail on Details...</description>
    <generator>Hugo</generator>
    <language>en-US</language>
    <copyright>Copyright © 2003 - 2026 Leonardo Rizzi</copyright>
    <lastBuildDate>Tue, 30 Jun 2026 03:28:29 +0200</lastBuildDate>
    <atom:link href="https://www.deepreflect.net/tags/mail/feed.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Linux - Send mail from command line</title>
      <link>https://www.deepreflect.net/2009/01/26/linux-send-mail-from-command-line/</link>
      <pubDate>Mon, 26 Jan 2009 03:28:56 +0000</pubDate>
       <guid isPermaLink="false">http://blog.deepreflect.net/?p=377</guid> 
      <description>&lt;p&gt;The Linux command line can be very powerful once you know how to use it. You can parse data, monitor&lt;/p&gt;
&lt;p&gt;Mutt:
One of major drawbacks of using the mail command is that it does not support the sending of attachments. mutt, on the other hand, does support it. I’ve found this feature particularly useful for scripts that generate non-textual reports or backups which are relatively small in size which I’d like to backup elsewhere. Of course, mutt allows you to do a lot more than just send attachments. It is a much more complete command line mail client than the “mail” command. Right now we’ll just explore the basic stuff we might need often. Here’s how you would attach a file to a mail:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>The Linux command line can be very powerful once you know how to use it. You can parse data, monitor</p>
<p>Mutt:
One of major drawbacks of using the mail command is that it does not support the sending of attachments. mutt, on the other hand, does support it. I’ve found this feature particularly useful for scripts that generate non-textual reports or backups which are relatively small in size which I’d like to backup elsewhere. Of course, mutt allows you to do a lot more than just send attachments. It is a much more complete command line mail client than the “mail” command. Right now we’ll just explore the basic stuff we might need often. Here’s how you would attach a file to a mail:</p>
<p><code># echo &quot;Sending an attachment.&quot; | mutt -a backup.zip -s &quot;attachment&quot; leo@deepreflect.net</code></p>
<p>This command will send a mail to <a href="mailto:leo@deepreflect.net">leo@deepreflect.net</a> with the subject (-s) “attachment”, the body text &ldquo;Sending an attachment.&rdquo;, containing the attachment (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id.
Shell scripting:
Now, with the basics covered you can send mails from your shell scripts. Here’s a simple shell script that gives you a reading of the usage of space on your partitions and mails the data to you.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e">#!/bin/bash
</span></span></span><span style="display:flex;"><span>df -h | mail -s <span style="color:#e6db74">&#34;disk space report&#34;</span> leo@deepreflect.net
</span></span></code></pre></div><p>Save these lines in a file on your Linux server and run it. You should receive a mail containing the results of the command. If, however, you need to send more data than just this you will need to write the data to a text file and enter it into the mail body while composing the mail. Here’s and example of a shell script that gets the disk usage as well as the memory usage, writes the data into a temporary file, and then enters it all into the body of the mail being sent out:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e">#!/bin/bash
</span></span></span><span style="display:flex;"><span>df -h &gt; /tmp/mail_report.log
</span></span><span style="display:flex;"><span>free -m &gt;&gt; /tmp/mail_report.log
</span></span><span style="display:flex;"><span>mail -s <span style="color:#e6db74">&#34;disk and RAM report&#34;</span> leo@deepreflect.net &lt; /tmp/mail_report.log
</span></span></code></pre></div><p>Now here’s a more complicated problem. You have to take a backup of a few files and mail then out. First the directory to be mailed out is archived. Then it is sent as an email attachment using mutt. Here’s a script to do just that:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e">#!/bin/bash
</span></span></span><span style="display:flex;"><span>tar -zcf /tmp/backup.tar.gz  /home/leo/files
</span></span><span style="display:flex;"><span>echo | mutt -a -s /tmp/backup.tar.gz <span style="color:#e6db74">&#34;daily backup of data&#34;</span> leo@deepreflect.net
</span></span></code></pre></div><p>The echo at the start of the last line adds a blank into the body of the mail being set out.</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
