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.


There is no de-facto client here really for this, most people simply use their MTA (Mail Transport Agents) to relay and then use sendmail(1) or mail(1) to inject mail to their MTA's queue (either directly or through SMTP port 25)

Most Unix machines come with an MTA, generally one of Sendmaill, Postfix or Exim.


In postfix you want to set your relayhost in /etc/postfix/main.cf (or whereever your main.cf configuration file is set).

relayhost = my.mailrelay.com


For sendmail, the term is called SMART_HOST and you can define it in sendmail.mc



In Exim, change the "routers" section of your configuration and add

  driver = domainlist
  transport = remote_smtp
  require_files = /etc/exim/smarthost
  route_list = "* my.mailrelay.com"


I've never seen Qmail come installed on a machine (mainly because of DJB's licensing terms) so it's probably not in your situation, but we might as well list the last of the big 4 MTA's in one place.

echo ":my.mailrelay.com" > /var/qmail/control/smtproutes


Some people might say, use a script to inject messages to the remote SMTP server over port 25/487. This solution is flawed. SMTP is designed so that mail servers can temporarily reject mail for whatever reason. All SMTP servers have fallback mechanisms so that messages will be retried and retried. Now your script could implement this, but then you are going down the route of re-inventing the wheel. Practically every Linux system comes installed with an MTA and they're trivial to install on Linux and every other Unix system I've come across. Configuring them can be intimidating when you're starting off, but email is so ubiqutous you need to learn this if you want to administrate Unix-like machines.

If the remote SMTP server you are relaying through is simply allowing you to relay by IP, the above should be sufficient. However, it's possible the remote SMTP server you want to relay through will require you to use SMTP authentication, and/or SSL/TLS to relay through. In this case, you'll need to configure your MTA to provide these details.


If you only want to send mails from your system and you do not want to receive mails with your own MTA or need any fancy special configuration, you might as well install one of the minimal MTAs like sSMTP, nbsmtp or nullmailer.

These come with a sendmail-compatible interface (/sbin/sendmail or /usr/sbin/sendmail) and will be used by mail, mailx or nail (or any other CLI mail client) to send mails.


For a start, you can use the mailx command (see man mailx), which can send mail from the command line.

For this to work, you will also need a local SMTP server installation that acts as a relay to the external one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy