Postfix Relay Setup With Drupal and Basic Postfix Management

vim2-html

This article covers some basic steps to install the postfix mail server as a relay on Ubuntu, get Drupal to work properly in all mail environments, plus some general postfix management commands. These instructions may have to be adapted slightly to your environment and linux distribution, but they are generally applicable.

For more help in getting an intial server setup, please read Ubuntu Setup on a Linode VPS, and our PHP, Mysql, Apache Setup on Ubuntu articles.

  1. Postfix Installation
  2. Postfix Configuration Notes (As A Relay)
  3. Relaying Postfix Mail From Root – Localhost
  4. Drupal Email Configuration for Restrictive SMTP Servers
  5. Postfix Queue Management Commands

1. Postfix Installation

Below are the basic installation steps for postfix on an ubuntu server (many have it already installed). You can also use the drupal smtp module for this function but for high volume mailing and/or better control a local relay, such as postfix, is often helpful.

sudo apt-get install postfix If already installed you can reconfigure it by issuing the following command:
sudo dpkg-reconfigure postfix
Then enter the following for our purposes of setting up a relay:

Choose Satellite Configuration
System Mail Name: your maildomain name
SMTP relay host:mail.authsmtp.com (we often use authsmtp, but whatever your ISP smtp server is wold go here)
Root and Postmaster mail recipient: webmaster@yourdomain.com
Other destinations to accept mail for: localhost.localdomain,localhost (accept defaults)
Force synchronous updates on mail queue: No, unless you know what you are doing
Local Networks: defaults
Procmail for local delivery: Yes
Mailbox size limit: 0
Local address extension character: +
Internet Protocol:All

2. Postfix Configuration

If you ran the reconfigure command above you are most of the way there, but you should edit your config file anyhow and look over the following. It should look like this:

sudo nano /etc/postfix/main.cf

myhostname = www.mydomain.com
mydomain = mydomain.com
myorigin = localhost
smtpd_banner = $myhostname ESMTP $mail_name
biff = no
append_dot_mydomain = no
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = localdomain, localhost, localhost.localdomain, localhost
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
# SECURITY NOTE: Listening on all interfaces. Make sure your firewall is
# configured correctly
inet_interfaces = loopback-only
relayhost = mail.authsmtp.com ## whatever your is
smtp_connection_cache_destinations = mail.authsmtp.com
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:username:password
smtp_sasl_security_options = noanonymous
default_destination_concurrency_limit = 4 ## to stay within your ISP’s policy
soft_bounce = yes
inet_protocols = all

Then make sure your aliases are ok:

You then want edit your /etc/aliases file (sudo nano /etc/aliases) and fill it out for your needs:

# See man 5 aliases for format
postmaster:    root

After this you can run:

sudo newaliases

And finally restart postfix

sudo /etc/init.d/postfix restart

3. Relaying Postfix Mail From Root – Localhost

As we have setup postfix above to relay all mail to an external smtp server you will lose mail from root – or root@localhost accounts.  To remedy this you can setup an alias via the following steps:

# cd /etc/postfix
# touch canonical
# echo "sender_canonical_maps = hash:/etc/postfix/canonical" >> /etc/postfix/main.cf
# echo "root allowedsender@anotherdomain.com" > /etc/postfix/canonical
# echo "root@localhost allowedsender@anotherdomain.com >> /etc/postfix/canonical
# postmap /etc/postfix/canonical
# /etc/init.d/postfix restart

To Test:
# echo TEST | mail -s TEST testemail@anotherdomain.com

4. Drupal Email Configuration for Restrictive SMTP Servers

By default, drupal doesn’t work well with ISP that require appropriately formed From and Reply-To Headers. There are issues in the queue to have this work properly in version 7 but for 5 and 6 you will need to edit the core contact module (sites/modules/contact.module).

Below is a copy of the patch file for version 5 of drupal, but it is very close to 6. It is best to edit the file yourself rather than try to submit this as a patch as we have other edits in the file and the line numbers may not be correct for you.

+++ contact.module 2009-01-29 20:48:45.000000000 -0500
@@ -418,10 +418,17 @@ function contact_mail_user_submit($form_
// Prepare all fields:
$to = $account->mail;
if ($user->uid) {
–    $from = $user->mail;
+//    $from = $user->mail;
+// Changed To Set Proper From and Reply-To And Add New Variable For User Copy
+ $usercopy = $user->mail;
+ $from = variable_get(‘site_mail’,”);
+ $headers = array(‘Reply-To’ => $user->mail);
}
else {
–    $from = $form_values[‘mail’];
+//    $from = $form_values[‘mail’];
+// Changed To Set Proper From and Reply-To
+ $from = variable_get(‘site_mail’,”);
+ $headers = array(‘Reply-To’ => $form_values[‘mail’]);
}

// Format the subject:
@@ -431,11 +438,14 @@ function contact_mail_user_submit($form_
$body = implode(“nn”, $message);

// Send the e-mail:
–  drupal_mail(‘contact-user-mail’, $to, $subject, $body, $from);

+//  drupal_mail(‘contact-user-mail’, $to, $subject, $body, $from);
+//  Changed To Add Headers
+ drupal_mail(‘contact-user-mail’, $to, $subject, $body, $from, $headers);
// Send a copy if requested:
if ($form_values[‘copy’]) {
–    drupal_mail(‘contact-user-copy’, $from, $subject, $body, $from);
+//    drupal_mail(‘contact-user-copy’, $from, $subject, $body, $from);
+//  Changed To Add Headers
+    drupal_mail(‘contact-page-copy’, $from, $subject, $body, $from, $headers);
}

5. Postfix Queue Management Commands

Now that you have everything working correctly, here are some useful postfix commands

# mailq  (show items in the queue – when you are relaying this is generally failed items)
# postcat -q queueid (read the contents of a message in the queue)
# postqueue -f (try to deliver all items in the queue immediately)
# postsuper -d ALL (remove all items in the queue)

Basic EXIM Commands For SMTP Relay

vim-html-code

Exim is an alternative mailhandler similar to postfix – with both being replacements for sendmail.  We generally use google apps for many of our domains, but some websites require outbound mail setups.  For this we tend to use authsmtp as our smtp relay service and setup exim or postfix to accomodate this.

The instructions below provide basic exim management commands, largely useful for debugging in a relay environment:

  • Queue Management
  • exim -bp ## list messages in the queue
  • exim -q -v ## send messages in queue
  • exim -Mrm <message-id> ## Message remove from queue
  • exiqgrep -i | xargs exim -Mrm ## Remove **all** messages in the queue
  • Message Management
  • exim -Mvh <message-id> ## Message view header
  • exim -Mvb <message-id> ## Message view body
  • exim -Mvl <message_id> ## Message view message log