Rather than configuring a full mail server, many applications only need to send transactional or notification emails (password resets, order confirmations, alerts). SMTP relay through your own Postfix server, or a third-party relay, is the simplest reliable approach.
Option 1 — Relay Through Your Own Postfix Server
If you already have Postfix configured (see How to Install and Configure Postfix as a Mail Transfer Agent), your application can connect directly:
PHP (using PHPMailer)
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'CHANGE_ME';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test.';
$mail->send();
Node.js (using Nodemailer)
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'localhost',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'CHANGE_ME'
}
});
transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'Test Email',
text: 'This is a test.'
});
Python (using smtplib)
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['Subject'] = 'Test Email'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg.set_content('This is a test.')
with smtplib.SMTP('localhost', 587) as server:
server.starttls()
server.login('[email protected]', 'CHANGE_ME')
server.send_message(msg)
Option 2 — Relay Through a Third-Party SMTP Provider
For applications where deliverability reliability matters more than infrastructure control, relaying through a dedicated transactional email provider (rather than self-managing reputation) is a common production pattern — particularly for password resets and time-sensitive notifications.
The connection code is nearly identical — only the host, port, and credentials change to match the provider's SMTP details.
Which Approach Should You Use?
| Scenario | Recommendation |
|---|---|
| Low-volume transactional email, existing mail server | Relay through your own Postfix |
| Critical deliverability (password resets, receipts) at scale | Third-party transactional provider |
| Bulk marketing campaigns | Dedicated email marketing platform with proper warm-up |
Securing the Relay Connection
- Always use STARTTLS or implicit TLS — never send credentials in plaintext
- Use a dedicated application-specific SMTP user, not a personal mailbox account
- Store SMTP credentials in environment variables, never hardcoded in source code
Common Errors
"Connection refused" on port 587 — confirm Postfix is running and the port is open in the firewall.
"Relay access denied" — the application isn't authenticating correctly, or Postfix's relay permissions need adjustment for the connecting user/network.
Emails sent successfully but land in spam — verify SPF, DKIM, and DMARC are correctly configured for the sending domain; see How to Configure SPF, DKIM, and DMARC (Complete Guide).
Best Practices
- Use application-specific credentials, never your personal mailbox login
- Always connect over an encrypted port
- Separate transactional sending from bulk marketing infrastructure when volume grows
Related Articles
- How to Install and Configure Postfix as a Mail Transfer Agent
- How to Configure SPF, DKIM, and DMARC (Complete Guide)
- How to Troubleshoot Email Not Sending or Going to Spam
