Monday, September 21, 2009

Enabling PHP mail on Snow Leopard using your ISP SMTP Server and verified sender

When trying to get postfix/php to send mail on my development box using my ISP account my emails were getting bounced as spam. This was not fixed by other internet posts on the subject involving editing php.ini and /etc/hostconfig.

Trying to send to my google account:
Sep 22 12:02:07 development postfix/smtp[65590]: E822C13C426: host gmail-smtp-in.l.google.com[209.85.222.93] said: 421-4.7.0 [60.234.235.17] Our system has detected an unusual amount of 421-4.7.0 unsolicited mail originating from your IP address. To protect our 421-4.7.0 users from spam, mail sent from your IP address has been temporarily 421-4.7.0 blocked. Please visit http://www.google.com/mail/help/bulk_mail.html 421 4.7.0 to review our Bulk Email Senders Guidelines. 33si2020982pzk.56 (in reply to end of DATA command)
Trying to send to my hotmail account:
Sep 22 11:46:29 development postfix/smtp[65417]: 9C41113C3E6: to=, relay=mx3.hotmail.com[65.55.37.88]:25, delay=0.97, delays=0.15/0.03/0.6/0.2, dsn=5.0.0, status=bounced (host mx3.hotmail.com[65.55.37.88] said: 550 DY-001 Mail rejected by Windows Live Hotmail for policy reasons. We generally do not accept email from dynamic IP's as they are not typically used to deliver unauthenticated SMTP e-mail to an Internet mail server. http://www.spamhaus.org maintains lists of dynamic and residential IP addresses. If you are not an email/network admin please contact your E-mail/Internet Service Provider for help. Email/network admins, please visit http://postmaster.live.com for email delivery information and support (in reply to MAIL FROM command))

All I needed to do to get postfix sending emails was:
  1. Telnet to my ISP mail server and check it would send email from a trusted source (my ISP account)
  2. Tell postfix to relay through my ISP's mail server:
    • sudo vi /etc/postfix/main.cf
    • relayhost = mail.orcon.net.nz
  3. Create an authentic sender address that postfix would use:
    • your mail log (mail.log) will show a send email address looking something like login@machine.local where login is your username on your computer and machine.local reflects your computer address on your home network. Hotmail, gmail, etc expect a verifiable address not something local like this. So we change it. Copy the local address as we need to create a mapping for it.
    • create a postfix generic mapping
      • sudo vi /etc/postfix/generic
      • login@machine.local myemail@myisp.com, or
      • login@machine.local me@gmail.com
      • I just added this line at the end of the file
    • create the hash db:
      • sudo postmap /etc/postfix/generic
    • add the mapping to postfix:
      • sudo vi /etc/postfix/main.cf
      • smtp_generic_maps = hash:/etc/postfix/generic
      • I put this in the section under # ADDRESS REWRITING comment
    • start/reload postfix:
      • sudo postfix start, or
      • sudo postfix reload (if it's already running)
That was it; mails were now being sent through and accepted by the destination mail server. This requires I send email when connect to the net using my ISP. If I can I'll create another post for using the same ISP mail server but connected using another ISP (for all you café coders out there).

After that the following php tested fine:

<?php

$name = "Test Mailer"; //senders name
$email = "anyone@somedomain.com"; //senders e-mail adress
$recipient = "testinbox@gmail.com"; //recipient
$mail_body = "PHP test body"; //mail body
$subject = "PHP test mail"; //subject
$header = "From: ". $name . " <" . $email . ">\r\n"; //optional headerfields

mail($recipient, $subject, $mail_body, $header); //mail command

?>

No comments:

Post a Comment