Mail - mutt etc

From Wiki

Jump to: navigation, search

Contents

Send File via mail on the Command Line

apt-cache search sharutils
//sharutils is required for uuencode
(echo Email Body Text; uuencode ~/file1.zip file1.zip;) | mail -s "Email Subject" recipient@example.com

Thunderbird picks up nicely on the attachment. Pine or Mutt doesnt show up the image as nice and will require further trickery. Ref: http://blog.moybella.net/2007/04/24/sending-email-with-attachments-from-the-command-line

Better Method of Emailing a File

apt-get install mpack

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

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

Cheers doneagain.

Misc.

To setup email:

To set system wide config - or environment variable:

/etc/profile

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
#!/bin/sh
logger -p mail.info 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
<?php
putenv("HTTP_HOST=". $_SERVER["HTTP_HOST"]);
putenv("SCRIPT_NAME=". $_SERVER["SCRIPT_NAME"]);
putenv("REMOTE_ADDR=". $_SERVER["REMOTE_ADDR"]);
?>


The above should do it. All emails send by PHP using the mail() command will now be logged. Check /var/log/mail.info 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/main.cf 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/main.cf
#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:

<?php
for ($count=0; $count <=100; $count++){
mail("recipient@server.com","subject","message","sender@server2.com");
echo "mail $count";
}
?>

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

References: http://www.postfix.org/postconf.5.html#smtp_connection_cache_on_demand
http://www.postfix.org/CONNECTION_CACHE_README.html
http://lists.debian.org/debian-isp/2008/02/msg00108.html
http://fixunix.com/debian/353750-etch-postfix-limiting-sent-emails-per-hour.html


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"

Ref: http://www.elho.net/mutt/maildir/


Info On:

http://gd.tuwien.ac.at/infosys/mail/qmail/qmail-manual-html/misc/INSTALL.mbox.html

Good on Mutt Config:

http://www.panix.com/help/mail.newmail.mutt.html

Mutt Defaults and .mutt

http://www.fefe.de/muttfaq/faq.html

Personal tools