Mail - sending

From NewbieDOC

Motives: You can send mail directly with modern mail client (like Sylpheed-Claws). Why would you need a SMTP server, also known as Mail Transport Agent (MTA)? One reason is that it can be annoying if you have a flaky connection/mail server. You can get timeouts, errors, ... Your MTA can take over the message (much faster, because it resides on the same computer) and it will take care of sending it. Another reason is that it makes sending mail very easy and some programs don't even need configuration. Examples would be the mail clients mutt and mailx or the program reportbug. You can even expand on this configuration and use it on a mail gateway at home.

Important: This example setup is not suitable for a real internet mail server (with a proper host.domain). This is just for a home computer or mail gateway used for sending mail via a proper mail server provided by your ISP or similar service. For the receiving part I use getmail and will write a separate note for it.

Contents

1 Basic configuration

In Debian Etch we will need at least the package postfix and the package libsasl2-modules in case we need SMTP AUTH. Debian comes with exim as default MTA so we need to change this. (The same can be done with exim as well, but in my very personal oppinion, postfix is easier to configure).

aptitude install postfix libsasl2-modules

Aptitude will offer to remove exim (because it conflicts with postfix). Just say yes. (Note: libsasl2-modules might already be installed.) You should get a window with postfix configuration.

General type of configuration?

Choose "Internet with smarthost".

Mail name?

Accept the default which should be your host name.

SMTP relay host?

Insert your ISP's SMTP server:

smtp.example.com

If your ISP doesn't require SMTP AUTH then you are good to go. You can test like this:

mail your_username@your_hostname

You will be prompted for a subject. Write for example "test" and press enter. Write a few words, then enter . on a newline and press enter. You will again get a prompt for Cc: at which point you should press enter and your mail gets sent. It should look like this:

$ mail your_username@your_hostname
Subject: test
Just some text
.
Cc:

If you just type 'mail' you should be able to see your test e-mail.

2 Address rewriting

You probably won't be able to send mails to the outside world, because most probably your smarthost will reject mails from an inexistent domain. For this to work we need to tell postfix to use the real mail address when sending. As root edit /etc/postfix/main.cf and add following lines:

# Address rewriting
smtp_generic_maps = hash:/etc/postfix/generic

(The line starting with # is just a comment for later reference)

Now we need to create the generic address rewriting table. Create the file /etc/postfix/generic and add a line like this:

your_username@your_hostname your_real_address@example.com

Now run:

# postmap /etc/postfix/generic
# /etc/init.d/postfix reload

At this moment, if your ISP doesn't request SMTP AUTH, you should be able to send mails just with the mail command:

$ mail your_real_address@example.com
Subject: test real mail
Just some text
.
Cc:

You should have new mail. (Note: remember that mail is not an instant protocol. If your smarthost and mail server are not the same it might take a while until you receive the test mail.)

3 SMTP authentication

Many ISPs require SMTP AUTH as a measure to prevent spam. For this to work we need to add some more stuff to postfix's config. Remember, though postfix is a fully featured mail server, we are now configuring it as a client. Edit /etc/postfix/main.cf and add:

# SMTP AUTH as client
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
# This has to be like this, otherwise we get
# the 'no mechanism available' error
smtp_sasl_security_options =

Now create /etc/postfix/sasl_passwd with the following content:

smtp.example.com your_real_address:your_mail_password

Don't worry about putting your password in plain text. We will make the file and the resulting .db file inaccessible:

# postmap /etc/postfix/sasl_passwd
# chmod 600 /etc/postfix/sasl_passwd
# chmod 600 /etc/postfix/sasl_passwd.db
# /etc/init.d/postfix reload

This configuration should work with most mail servers. You can test (again) using 'mail' or any mail client. Graphical mail clients usually don't have a default configuration. When you create a new account just type 'localhost' for the SMTP server.

4 SSL support

The same as with the SMTP AUTH, we need to configure postfix to act as a client to an SSL server. There are a few variants for this, but I will post the config necessary to send mails via Gmail. (Note: address rewriting is futile for Gmail, as any mail that passes through their servers will use the gmail address in the From: header.)

We edit again the /etc/postfix/main.cf file and add the following line:

smtp_tls_policy_maps = hash:/etc/postfix/tls_policy

Of course, we also need to create the file /etc/postfix/tls_policy with the following contents:

smtp.gmail.com:587 encrypt

For this to work you need to set it as relayhost in main.cf, of course:

relayhost = smtp.gmail.com:587

The ':587' bit specifies the port if you didn't guess this by now. You can also use square brackets around the mail server's name to disable MX lookups. This might be necessary with some servers who don't have proper MX records.

Enjoy your new setup.

5 Troubleshooting

In case your testmails don't get through, the first place to chech is the mail log, which is stored in the file /var/log/mail.log. tail is a very nice utility for this as it shows you only the last rows of a file, which in a log should be the newest. Check your syntax for any typos and if you found the error (and corrected) you should run (as root):

# /etc/init.d/postfix restart

The 'restart' of the postfix daemon will make sure the new config is loaded and will also flush the queue. If you want to check, flush or delete messages from postfix's queue you can use postqueue(1).

Enjoy your new setup. For retrievind mail from a POP account see the Mail - retrieving and for sorting the Mail - sorting and filtering notes.


--amp77 20:41, 7 February 2007 (CET)

This document has been released by the author under a Free license (which should be indicated beneath this notice). You may use it freely according to its particular license.

Free Software License: Either GNU FDL or GNU GPL at your choice.

GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License."
GNU head This work is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This work is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Content is available under GNU Free Documentation License 1.2, unless otherwise stated.