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.