How to Send Email via SMTP Relay from Your Application

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?

ScenarioRecommendation
Low-volume transactional email, existing mail serverRelay through your own Postfix
Critical deliverability (password resets, receipts) at scaleThird-party transactional provider
Bulk marketing campaignsDedicated 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
  • smtp relay, phpmailer, nodemailer, transactional email, send email from app
  • 0 utilizatori au considerat informația utilă
Răspunsul a fost util?

Articole similare

How to Set Up a VPS for Email Marketing (Complete Guide)

Running your own email marketing infrastructure on a VPS gives you full control over...

How to Configure SPF, DKIM, and DMARC (Complete Guide)

SPF, DKIM, and DMARC are the three DNS-based email authentication standards that Gmail, Outlook,...

How to Set Up Reverse DNS (PTR/rDNS) for Email Deliverability

Reverse DNS (PTR record) maps your server's IP address back to a hostname — the opposite of...

IP Warm-Up: A Safe 30-Day Schedule for New Sending IPs

Mailbox providers like Gmail and Outlook monitor sending behavior closely and treat sudden...

Best Email Marketing Software for a VPS (MailWizz, Mautic, Listmonk & More)

Choosing the right self-hosted email marketing platform affects deliverability, ease of...