Skip to main content
Use PHPMailer or Symfony Mailer to send transactional and marketing email from PHP through mySMTP. Both libraries support TLS encryption and SMTP authentication.

Prerequisites

  • PHP 8.1 or later
  • Composer
  • Your mySMTP username and password from the setup email
  • Your mySMTP hostname from the setup email (shown as relayxx.mysmtp.com in these examples)

PHPMailer via Composer

Install

Basic send example

Replace relayxx.mysmtp.com with your real hostname from the mySMTP setup email. Load credentials from environment variables, never hardcode them in source files.

HTML body with attachment

If you use Symfony, configure the mailer DSN in .env or config/packages/mailer.yaml:
Replace USERNAME, PASSWORD, and relayxx.mysmtp.com with your actual credentials and hostname from the setup email. Symfony Mailer uses STARTTLS automatically on port 587.

Error handling

Always wrap PHPMailer calls in a try/catch block. Exception is thrown for SMTP connection failures, authentication errors, and rejected recipients. Inspect $mail->ErrorInfo for the exact server response.

Common errors

Your username or password is incorrect, or your account is not yet activated. Re-enter credentials exactly as shown in the setup email. See Setting up SMTP credentials.
Your network or host may block port 587. Try port 2525 with STARTTLS, or port 465 with PHPMailer::ENCRYPTION_SMTPS. See Supported SMTP ports and encryption.
The encryption setting does not match the port. Use ENCRYPTION_STARTTLS for ports 587 and 2525. Use ENCRYPTION_SMTPS for port 465.
Test your SPF, DKIM, and DMARC setup with the mySMTP Deliverability Toolbox before sending large volumes.
Use PHPMailer or Symfony Mailer to send transactional and marketing email from PHP through mySMTP. Both libraries support TLS encryption and SMTP authentication.

Prerequisites

  • PHP 8.1 or later
  • Composer
  • Your mySMTP username and password from the setup email
  • Your mySMTP hostname from the setup email (shown as relayxx.mysmtp.com in these examples)

PHPMailer via Composer

Install

Basic send example

Replace relayxx.mysmtp.com with your real hostname from the mySMTP setup email. Load credentials from environment variables, never hardcode them in source files.

HTML body with attachment

If you use Symfony, configure the mailer DSN in .env or config/packages/mailer.yaml:
Replace USERNAME, PASSWORD, and relayxx.mysmtp.com with your actual credentials and hostname from the setup email. Symfony Mailer uses STARTTLS automatically on port 587.

Error handling

Always wrap PHPMailer calls in a try/catch block. Exception is thrown for SMTP connection failures, authentication errors, and rejected recipients. Inspect $mail->ErrorInfo for the exact server response.

Common errors

Your username or password is incorrect, or your account is not yet activated. Re-enter credentials exactly as shown in the setup email. See Setting up SMTP credentials.
Your network or host may block port 587. Try port 2525 with STARTTLS, or port 465 with PHPMailer::ENCRYPTION_SMTPS. See Supported SMTP ports and encryption.
The encryption setting does not match the port. Use ENCRYPTION_STARTTLS for ports 587 and 2525. Use ENCRYPTION_SMTPS for port 465.
Test your SPF, DKIM, and DMARC setup with the mySMTP Deliverability Toolbox before sending large volumes.