Search code examples
windowsphpmailermailgunemailrelay

Sending mail via relay using PHP on Windows


I am using PHPMailer (via SMTP) to send out emails via my websites. I am using a windows 2012 server as my mail server which is using Hmailserver. I am using Mailgun to relay my emails.

Things I have done:

  • I have setup and validated my mailgun settings.
  • I have tested sending and receiving emails via the server without the relay (works fine).

My dilemma:

  • At one stage I am going to have to state the relay information, such as the authentication or hostname. So far, i see two place where i can declare this - see below:

1) Specify via PHPMailer script:

$mail->IsSMTP();
$mail->host = "smtp.mailgun.org";
$mail->Username = "username";
$mail->Password = "password";

2) Specify in hmailserver admin (on the server):

http://puu.sh/cJLpk/c3d548981c.png

Which way do I do this if I want to relay all my emails?


Solution

  • Using your local mail server (hmailserver) as a relay will be faster (at least for your client scripts) and much more reliable. It should be configured to point at mailgun, as in your screen shot. Mailgun should provide you with credentials that you can use for authenticating the relay.

    When you send with PHPMailer, you should configure it to point at localhost, like this:

    $mail->IsSMTP();
    $mail->Host = 'localhost';
    $mail->Username = "username";
    $mail->Password = "password";
    

    (You may not need username and password for your local server). Though it may sound odd, using SMTP to localhost is often faster than calling a local sendmail binary (or using PHP's mail() function).

    In your original code you had host instead of Host - PHP is case-senetive for property names, so that would have been failing if that was your real code.

    That should be all there is to it.

    The only other complication is if hmailserver is also sending messages that are not supposed to go through mailgun, in which case you will need to get further into your hmailserver config.