October 11, 2024

command line email howto

There are lots of reasons to want to send email from your server command line, whether it’s website notification, testing a remote (or local) system, or just to learn

using awk/sendmail to send an attachment

awk 'BEGIN{print "Subject:Bash rocks!\nFrom:Whoever <your@email.com>"}{printf("%s\015\n", $0)}' filecontentsforbody.txt | sendmail -t your@email.com

using mutt to mail an attachment

mutt -s "Test mail" -a /tmp/file.tar.gz vivek@nixcraft.co.in < /tmp/mailmessage.txt

sending attachment with a script

with this script, you just type:

mailscript.sh fileyouwanttosend.txt

here’s the script:

vi mailscript.sh
  #!/bin/bash
  FILE=$1
  RECIPIENT=myemail@yahoo.com
  (echo "This file $FILE was sent from Linux."
  for i in `ls -1 $FILE`
  do
  file_name=`basename $i`
  uuencode $i $file_name
  done
  ) | mail -s "Mail sent with the attachment file: $FILE" "$RECIPIENT"

If you want to use php to call command line email, phpmailer is a pretty good option with lots of features. There’s help for this command line email stuff on the unix.com website, they’re usually really nice and will help you 🙂 Also, you can get examples at cyberciti.biz, that’s helpful too.

testing your local mail server

this works with your server’s local mail server, whether Sendmail, Postfix, Qmail, Exim or whatever:

[root@server]$
 telnet localhost 25
 Trying 127.0.0.1...
 Connected to server (127.0.0.1).
 Escape character is '^]'.
 220 localhost.localdomain; ESMTP Thu, 6 Mar 2003 17:57:01 -0500
 helo localhost
 250 localhost.localdomain Hello wolf [127.0.0.1],
 mail from: bernier@localhost
 250 2.1.0 bernier@localhost... Sender ok
 rcpt to: robert.bernier5@sympatico.ca
 250 2.1.5 robert.bernier5@sympatico.ca... Recipient ok
 data
 354 Enter mail, end with "." on a line by itself
 subject; I'm hungry, send pizza
 Please send me one large pepperoni all dressed.
 Don't forget the pineapples!
 .
 250 2.0.0 h26N3hs18884 Message accepted for delivery
 quit
 221 2.0.0 localhost.localdomain closing connection
 Connection closed by foreign host.

sending mail to multiple recipients at once

sort of a local mailing list. You might use this if you wanted notifications to be sent to a single email address, which would then send the same email to 3 people. This is handy also if you have 3 sysadmins that need to be notified of something on the server. This assumes you have something like Postfix already installed. First, we set up root notifications to go to one email, then we set up another notification email, like from a website, called “webnotification” that will notify a few emails at once.

vi /etc/aliases
  .
  .
  root: yourusername
  yourusername: youremail@gmail.com
  webnotification: email1@gmail.com, anotheremail@yahoo.com
(now save and run)
newaliases

so now you can send an email to webnotification@localhost, using the test script above (or your website), and it should deliver that email to both email1@gmail.com, and anotheremail@yahoo.com

imapsync --nosyncacls --subscribe --syncinternaldates --host1 mail.sourcemailserver.com --user1 sourceemail@sourceserver.com --password1 sourcepassword --host2 mail.destinationserver.com --user2 destinationmail@destinationserver.com --password2 destinationpassword --noauthmd5 --sep2 "/" --prefix2 ""

If you want to just sync one folder, not the whole thing, add the folder directive like:

imapsync --nosyncacls --subscribe --syncinternaldates --host1 mail.sourcemailserver.com --user1 sourceemail@sourceserver.com --password1 sourcepassword --host2 mail.destinationserver.com --user2 destinationmail@destinationserver.com --password2 destinationpassword --noauthmd5 --prefix2 "" --folder INBOX

If your destination server is a zimbra box, you may get a No Cleartext Login error, if you do, do this:

su zimbra
cd /opt/zimbra/bin/
zmprov ms `zmhostname` zimbraImapCleartextLoginEnabled TRUE
curl -u users@domain.com:name --data-binary @/tmp/domain_contacts.vcf http://mail.server.com/service/home/users@domain.com/contacts?fmt=vcf

install imapsync on CentOS

perl -MCPAN -e "install Test::Inter"
perl -MCPAN -e "install Parse::RecDescent"
perl -MCPAN -e "IO::Socket::SSL"
perl -MCPAN -e "Digest::HMAC_MD5"
perl -MCPAN -e "install Getopt::Long"
perl -MCPAN -e "CPAN::Shell->force(qw(install Date::Manip));"
perl -MCPAN -e "CPAN::Shell->force(qw(install Mail::IMAPClient));"
perl -MCPAN -e "CPAN::Shell->force(qw(install Term::ReadKey));"
wget http://packages.sw.be/imapsync/imapsync-1.350-1.el5.rf.noarch.rpm
rpm -i --nodeps imapsync-1.350-1.el5.rf.noarch.rpm

reference