tags:

views:

564

answers:

3

Hi All,

I would like to send an email through a remote SMTP server. How can this be done? I will be sending the email from a bash shell script.

I'm using a unix machine. uname -a returns: Linux linux 2.4.21 BrandZ fake linux i686 athlon i386 GNU/Linux

Thank you.

+2  A: 

Contacting a remote SMTP server directly is not generally the way this is done. What, for example, if the server is temporarily unavailable?

The easier route is to run a local mailserver such as postfix, exim or qmail, and set it up to send mail through a remote server. Then you can just use command-line sendmail to send your e-mail.

In postfix on Ubuntu, I put the following in master.cf:

relayhost = [smtp.my-isp.com]
smtp_generic_maps = hash:/etc/postfix/generic

You need the last line in case your ISP's mail server requires that all outgoing mail originates from [email protected]. Then you'll also need /etc/postfix/generic like this:

youruser@localhost [email protected]

Add other variants (e.g. [email protected]) as necessary.

Thomas
A: 

The mailx command can be used to send email non-interactively, but the classical implementation talks to a local mail transport agent. There are simple MTAs which will just send email through a remote SMTP server, and won't accept any local email.

Another solution is to use heirloom-mailx, (formerly known as nail I think) which supports talking to an SMTP server directly instead of using a local MTA. For instance, you may invoke it as follows:

heirloom-mailx -S smtp=smtp.your-isp.com \
               -S [email protected] \
               -s "subject" <<EOM
Hello, $name,
This is an automatic reminder, sent out once a month, ...
[the rest of your message]
EOM
Jérémie Koenig
A: 

i'm not sure if it still works.. but you might wanne use telnet. at least you should be able to write a bashscript using it

Dimitri