Skip to main content

How to configure a php mail library (like Drupal 7 phpmailer) to use the correct server



  • Dan Morrison

    For Drupal 8, the process will be the same, but the syntax of configuration settings overrides will be different.

    • First explore the modules settings via the Drupal UI, and try setting it manually to whatever the value of $PLATFORM_SMTP_HOST is for your environment. Test that.

    • Second, Edit your sites settings.php or equivalent, and add a line like:

    $config['mailmodule.settings']['smtp_host'] = getenv('PLATFORM_SMTP_HOST');

    And you’ll be done.

    Check your chosen modules README or instructions for the actual syntax!

    Example: Drupal 8 Swiftmailer 8.2

    The module README tells you how to set
    "Configuration Overrides "
    which would need to be:

    $config['swiftmailer.transport']['smtp_host'] = getenv('PLATFORM_SMTP_HOST');
    $config['swiftmailer.transport']['smtp_port'] = 25
  • Dan Morrison

    Example: Drupal 8 SMTP module

    Configs are seen at /admin/config/system/smtp but it’s better to set these from the environment level in your settings.php like so:

    $config['smtp.settings']['smtp_host'] = getenv('PLATFORM_SMTP_HOST');

    To find out other config values you may want to set, investigate the output of drush config:get smtp.settings

  • Matthias Van Woensel

    Should probably be this now:

    $config['smtp.settings']['smtp_host'] = gethostbyname(getenv('PLATFORM_SMTP_HOST'));

    But in actuality, you don’t need to set Host and Port to be set.

    $mail = new PHPMailer(true);
    $mail->setFrom('', 'Mailer');
    $mail->addAddress('', 'Their Name');     //Add a recipient
    $mail->Subject = 'Test email phpmailer.php';
    $mail->Body = 'these are testmails in the body thingy';

Please sign in to leave a comment.