Mail - mutt etc

From Wiki

Jump to: navigation, search


Send File via mail on the Command Line

apt-cache search sharutils
//sharutils is required for uuencode
(echo Email Body Text; uuencode ~/;) | mail -s "Email Subject"

Thunderbird picks up nicely on the attachment. Pine or Mutt doesnt show up the image as nice and will require further trickery. Ref:

Better Method of Emailing a File

apt-get install mpack

mpack -s "Subject" -c application/octet-stream filename
#the above works using no subject.

mpack -s "Subject" -c application/octet-stream filename -d body.txt
#the -d is used to attach a file as a body of the email.

Cheers doneagain.


To setup email:

To set system wide config - or environment variable:


MAIL=$HOME/Maildir export MAIL

This should make Mutt read the Maildir

Log PHP and mail()

vi /etc/php5/apache2/php.ini
;sendmail_path =
sendmail_path ="/usr/sbin/sendmail_wrapper"
auto_prepend_file ="/etc/set_php_headers.php"

vi /usr/sbin/sendmail_wrapper
logger -p vhostmail: site=${HTTP_HOST}, client=${REMOTE_ADDR}, script=${SCRIPT_NAME}
/usr/sbin/sendmail -t -i $*

chmod 755 /usr/sbin/sendmail_wrapper

vi /etc/set_php_headers.php
putenv("HTTP_HOST=". $_SERVER["HTTP_HOST"]);

The above should do it. All emails send by PHP using the mail() command will now be logged. Check /var/log/ for the logs. This is working fine on a reasonably heavy webserver.

Limit Outgoing Email

While the above script logs emails and munin can notify if a mailqueue exceeds a certain amount in a period of time, it still could be too late, and 1000's of emails could already have been sent before it is noticed using munin alerts (5 minute default polling).

From looking into this a lot, it is not easy to limit "outgoing" email using Postfix. While there are several limits, these are mainly for incoming mail. There is policyd, however it needs a MySQL database which is a bit much work.

An iptable rule was chosen to limit outgoing email. A config also had to be added to postfix/ making sure a new connection was made for every email to be sent. (Otherwise postfix could send multiple emails using the single connection, and the iptable rule only stops new connections.)

iptables -A OUTPUT -p tcp --dport 25 -m state --state NEW -j LOG
#Note the above log is not needed. Its just good to test first with. Logs get put in /var/log/syslog
iptables -A OUTPUT -p tcp --dport 25 -m state --state NEW -m limit --limit 10/minute --limit-burst 10 -j ACCEPT
#the -limit-burst is not required but added for config.
iptables -A OUTPUT -p tcp --dport 25 -m state --state NEW -j DROP

vi /etc/postfix/
#add the following line to force postfix to open a new connection for every email to be sent.
smtp_connection_cache_on_demand = no

That should be it. Of course the above iptable rules would need to be added to a startup script in init.d etc. A simple script to test:

for ($count=0; $count <=100; $count++){
echo "mail $count";

If anyone has any feedback, I would be interested in hearing it. Emails to sburke(at)


Set Mutt to use Maildir

vi /etc/Muttrc
//add in
set mbox_type=Maildir
set folder="~/Maildir"
set mask="!^\\.[^.]"
set mbox="~/Maildir"
set record="+.Sent"
set postponed="+.Drafts"
set spoolfile="~/Maildir"


Info On:

Good on Mutt Config:

Mutt Defaults and .mutt

Personal tools