How to Configure PostFix with Amazon SES and Multiple Domains in PHP: A Guide

Postfix is a widely used open-source Mail Transfer Agent (MTA) that enables routing and delivery of email. Amazon Simple Email Service (SES) is a flexible, cost-effective, and scalable email sending service. This article will guide you on how to integrate PHP, PostFix, Amazon SES, and multiple domains (transports).

How to Configure PostFix with Amazon SES and Multiple Domains in PHP: A Guide

Postfix is a widely used open-source Mail Transfer Agent (MTA) that enables routing and delivery of email. Amazon Simple Email Service (SES) is a flexible, cost-effective, and scalable email sending service. This article will guide you on how to integrate PHP, PostFix, Amazon SES, and multiple domains (transports).

What is PostFix and Amazon SES?

PostFix is a free MTA that routes and delivers email on a Unix system. It’s designed to be fast, easy to administer, and secure. Amazon SES, on the other hand, is a cloud-based email sending service designed to help digital marketers and application developers send marketing, notification, and transactional emails.

Requirements

  1. PHP 5.6 or higher
  2. Postfix 3.0 or higher
  3. Amazon SES account
  4. AWS SDK for PHP
  5. Access to DNS settings of your domains

Step 1: Setting up Amazon SES

Before using Amazon SES, you need to verify your domain and set up the SMTP settings.

  1. Log in to your AWS Management Console and navigate to Amazon SES.
  2. Go to the ‘Domains’ section in the SES dashboard and click on ‘Verify a New Domain’.
  3. Add your domain, check the ‘Generate DKIM Settings’ box, then verify it. Amazon SES will provide a TXT record for verification and a set of CNAME records for DKIM. Add these to your domain’s DNS settings.

Step 2: Configure Postfix to Use Amazon SES

Once your domain is verified, you can configure Postfix.

  1. Open your Postfix configuration file, usually located at /etc/postfix/main.cf.
  2. Add or modify the following lines:
relayhost = [email-smtp.region.amazonaws.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
  1. Create or edit the /etc/postfix/sasl_passwd file to include your Amazon SES SMTP credentials:
[email-smtp.region.amazonaws.com]:587 USERNAME:PASSWORD
  1. Update Postfix’s hash tables and restart Postfix:
sudo postmap /etc/postfix/sasl_passwd
sudo service postfix restart

Step 3: Setting Up Multiple Domain Transports

For handling multiple domains, you need to set up transport maps.

  1. Edit /etc/postfix/main.cf and add or modify:
transport_maps = hash:/etc/postfix/transport
  1. Create or edit the /etc/postfix/transport file and define your domains:
domain1.com smtp:[email-smtp.region.amazonaws.com]:587
domain2.com smtp:[email-smtp.region.amazonaws.com]:587
  1. Update Postfix’s hash tables and restart Postfix:
sudo postmap /etc/postfix/transport
sudo service postfix restart

Step 4: Sending Emails with PHP

Finally, you can now send emails using the PHP mail() function.

<?php
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email.';
$headers = 'From: sender@yourdomain.com' . "\r\n" .
    'Reply-To: sender@yourdomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Conclusion

Integrating PHP, Postfix, and Amazon SES provides a powerful, scalable, and cost-effective solution for sending emails. By leveraging these technologies, you can handle multiple domains and ensure your emails are delivered successfully.

Remember, managing email systems can be complex, and it is crucial to monitor your system to ensure everything is working correctly. Happy emailing!


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.