Web Development

Sending E-Mail Through Your website

SMTP(Simple Mail Transfer Protocol)

It is protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP. In addition, SMTP is generally used to send messages from a mail client to a mail server.

Setting up SMTP

Type the following commands in the command prompt

sudo apt-get install ssmtp

Edit the ssmtp config file :

sudo vim /etc/ssmtp/ssmtp.conf

Enter this in the file:

root=username@gmail.com mailhub=smtp.gmail.com:465 rewriteDomain=gmail.com AuthUser=username AuthPass=password FromLineOverride=YES UseTLS=YES

Enter the email address of the person who will receive your email:

ssmtp recepient_name@gmail.com

Now type this in the command prompt:

To: recipient_name@gmail.com
From: username@gmail.com
Subject: Sent from a terminal!
The content of your mail goes here

To send the email i.e. after you are done typing the mail you want to send: Ctrl + D

Install ssmtp Install ssmtp: sudo apt-get install ssmtp

Edit the ssmtp config file : gksu gedit /etc/ssmtp/ssmtp.conf

Enter this in the file:

root=username@gmail.com
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password
FromLineOverride=YES
UseTLS=YES

Enter the email address of the person who will receive your email:

ssmtp recepient_name@gmail.com

Now enter this:

To: recipient_name@gmail.com
From: username@gmail.com
Subject: Sent from a terminal!

Your content goes here. Lorem ipsum dolor sit amet, consectetur adipisicing.

To send the email: Ctrl + D

You can also save the text mentioned in Point 5 into a text file and send it using:

ssmtp recipient_name@gmail.com < filename.txt

Postfix

It is Wietse Venema's mail server that started life at IBM research as an alternative to the widely-used Sendmail program. Postfix has several hundred configuration parameters that are controlled via the main.cf file.

Install Postfix

Postfix can easily be installed through apt-get:

sudo apt-get install postfix

During the installation, you will see a dialogue box appear, asking you which kind of installation you would prefer. Select “Internet Site”.

Follow up by entering the name of your domain, or your gmail.com i.e a mailing service like gmail.

Configure Postfix

Once Postfix is installed, go ahead and open the main configuration file.

sudo nano /etc/postfix/main.cf

There are a few changes that should be made in this file.

myhostname = example.com

Put in name of your domain into myhostname.

mydestination defines the domains that postfix is going to serve, in this case—localhost and your domain (eg. example.com). relayhost can be left, as is the default, empty.

mynetworks defines who can use the mail server. This should be set to local—creating an open mail server is asking for SPAM. This will usually have damaging effects on your server and may put you in line for discipline from your web hosting provider.

If it is not set up by default, as it should be, make sure you have the following text on that line:

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

The rest of the lines are set by default. Save, exit, and reload the configuration file to put your changes into effect:

sudo /etc/init.d/postfix reload

Configure Additional Emails

To redirect emails to specific emails, you can add users to the alias file. By default each user on the server will be able to read emails directed to their username@domain-name.com.

Open up the the alias database:

sudo nano /etc/postfix/virtual

Within that file, enter in the names of your users. For example:

sales@example.com username1 me@example.com username2

Once you are finished, save, exit, and run the following command:

postmap /etc/postfix/virtual

The last step is to reload postfix once more.

sudo /etc/init.d/postfix reload

Once Postfix is installed, mail can be sent to and from the server, although without a mail server like Dovecot or Cyrus, you will only be able to see the email on the server.

You send out email from the command line with the command “sendmail” and where you want the mail sent to:

sendmail sample-email@example.org

Once you enter the command, type your message, and when it is completed, you can send it off with ctrl-D

Incoming mail gets delivered into /var/mail/username