Thursday, April 9, 2009

Send email via command line with sendmail

So you want to make sure sendmail is running and able to send email messages, but can't quite figure out the command to test it from the command line? It's easier than you may think, but it requires you to create an input file to specify the parameters.

Create a new file, we'll call it email.txt, with vi or whatever text editor you use. Then add parameters for the SMTP headers that you want to include in the message, such as "From:", "To:", and "Subject:". You also MUST remember to leave an extra, empty line at the end of the document because that signals the end of your email message. An example of the text for a basic message would be:

From:me@myemail.com
To:you@youremail.com
Subject:Hello, thanks for reading

Hi, I appreciate you reading this blog post.



You can specify other header parameters in the file as well, such as date, cc, etc by simply adding an entry for them. If you want to find out more, check the SMTP Wiki, or perform your own Google search to find resources.

After you've saved that text into your email.txt file, now you're ready to use sendmail to send this message. You can use the -t switch to import your file.

/usr/sbin/sendmail -t < email.txt

That is assuming you're still in the same directory as your email.txt file. If not, you'll want to specify the full path to the email.txt file so sendmail knows what to import.

If you're looking for more information, check out the original post I used to answer my question about sendmail.

2 comments:

Anonymous said...

Hi,

just thought I'd elaborate on this a bit.

If all you want to do is see whether something is running, then sendmail -t is fine.

If you want to test the actual installation, start with "telnet your-mta.com 25" to talk to the actual sendmail daemon. Then you'll need to talk some SMTP:

"ehlo localhost" will get the party started, then "mail from:your-user@your-domain.com" will tell sendmail who's sending. Follow that up with "rcpt to:some-recipient@destination-host.org" to tell sendmail who should get the mail. By typing "data" you tell the daemon to wait for input, then give it something like "subject:this is a test [enter] does this work? test from your-name [enter] . [enter] quit" to send an email with subject 'this is a test' and body 'does this work etc.' to recipient@destination.com. That's actually much simpler and makes more sense in the SMTP world.

To build on that, and make sure DNS and all that good stuff is working, try "sendmail -bv user@recipient-domain.com" to actually see whether things work as you expect. There are a whole host of further -d switches to the command. All in all, it's a damn sight more reliable than just creating a text file.

Hope this was useful!

Chris

rslygh said...

Thanks Chris. I think the title of my post was misleading for the intended purpose. You're correct with explaining that telnetting to port 25 on the sendmail server would be a better test. Thanks for pointing that out, and I've renamed the post accordingly