Mail - sending
From NewbieDOC
- Andrei Popescu
- andreimpopescu at gmail dot com (author)
- Chris Lale
- chrislale AT users DOT berlios DOT de (publisher)
Go to NewbieDOC index
Revision History
Revision 1.0 | 9th February 2007 | Revised by Andrei Popescu | ||||||||||||||
Initial release. | ||||||||||||||||
| ||||||||||||||||
Revision 1.1 | 27th November 2007 | Revised by Andrei Popescu | ||||||||||||||
Reformatted for newbiedoc package. Added Revision History, Abstract and License in Appendix. | ||||||||||||||||
Help for Debian newbies wanting to send mail using Postfix with smarthost configuration.
1 MotivesYou can send mail directly with any 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. If you ever get an error response than something is really wrong. 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. 2 Basic configurationIn 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 local mail Just some text . Cc: If you just type 'mail' you should be able to see your test e-mail. 3 Address rewritingMany providers are trying to reduce spam by rejecting mails from an inexistent domain. In order to send mails out on the internet we need to tell postfix to use the real mail address when sending. (Note: You could configure your client to set proper "From:" and "Envelope-From:" headers, but the purpose of this document is to create a client-independent setup). 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.) 4 SMTP authenticationMany ISPs also 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 for normal users: # 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. 5 SSL supportThe 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. 6 TroubleshootingIn 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. 7 Appendix A: LicenseCopyright (c) 2007 Andrei Popescu, andreimpopescu at gmail dot com
Go to NewbieDOC index |