Skip to content

Instantly share code, notes, and snippets.

@atomtigerzoo
Last active February 16, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomtigerzoo/c3be14d9e8c1d89c66a8 to your computer and use it in GitHub Desktop.
Save atomtigerzoo/c3be14d9e8c1d89c66a8 to your computer and use it in GitHub Desktop.
A simple way to send mails from a virtual server to another SMTP host for relaying/forwarding mails. Suitable for development - or just for kicks on a virtual server.

Relay mails to an external SMTP server

This shows you how to send mails from a - in this case virtual debian server inside VirtualBox - server to an external SMTP server. You can use your own server, Gmail or any other SMTP server you have access to.

This can then be used to send mail from PHP, Rails, Python, console or whatever you like.

The steps are for a debian wheezy server.

You will need:

  • a username and password for the smtp server
  • the address and port of the smtp server
  • a (virtual) server

Install sSMTP

sudo apt-get install ssmtp

Configure sSMTP

Open the config file and make it look like the following. Change all uppercase strings (except the YES and NOs) to the your personal login, server, etc. I did not replace the port, as it should be the standard of 587.

sudo nano /etc/ssmtp/ssmtp.conf

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=YOUR_EMAIL_ADDRESS_HERE

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=YOUR_SMTP_SERVER_HERE:587

# Where will the mail seem to come from?
# Set it to something like yourdomainname.com
rewriteDomain=YOUR_DOMAIN_NAME_HERE

# The full hostname
hostname=YOUR_EMAIL_ADDRESS

# Do not replace the YES ;)
UseSTARTTLS=YES

AuthUser=YOUR_USERNAME_FOR_THE_SMTP_SERVER
AuthPass=YOUR_PASSWORD_FOR_THE_SMTP_SERVER

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Save the file with ctrl+x

You can configure forwarding of mails in the following file. You don't need to if you don't want/need forwarding.

sudo nano /etc/ssmtp/revaliases

# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.

root:YOUR_EMAIL_ADDRESS:YOUR_SMTP_SERVER_HERE:587

Save it with ctrl+x

Test it

You can test it with the following command on the commandline:

sudo ssmtp YOUR_EMAIL_HERE

Type some words, add a new line and close with (double) crtl+d to send the message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment