Sending Email with PHP on IIS
Enabling Email[edit]
Make sure the SMTP port is open on the router.<<< not necessary for outbound traffic- Make sure the SMTP service is running on the server.
PHP Configuration[edit]
Specifying the sender[edit]
The real hurtle I encountered was this error:
warning: mail() [function.mail]: SMTP server response: 501 5.5.4 Invalid Address
The problem is that by default SMTP on Windows doesn't accept emails in the format From: John Doe <johndoe@domain.com> even though that format is RFC-compliant.
Something like this will fail on Windows (but work fine on *nix)
$headers = "From: {$sender_name} <{$sender_email}>\r\n";
mail($to, $subject, $body, $headers);
Whereas this will work:
$headers = "From: {$sender_email}\r\n";
Fix: edit php.ini:
sendmail_from = me@domain.com
Specifying the SMTP host[edit]
The "SMTP" setting in php.ini controls the SMTP server value.
Within an application this setting can be changed with ini_set():
ini_set('SMTP', '[SMTP_HOSTNAME]');
PHP Code[edit]
mailer class
require_once(COMMON_CLASS_DIR."utils/mail_class.php");
sample code
try
{
ob_start();
include (APP_TEMPLATE_DIR."email/email_template.php");
$body = ob_get_contents();
ob_end_clean();
$mail = new mail_class(
$sender_name,
$sender_addr,
$recipient_name,
$recipient_addr,
$subject,
$body,
false);
$mail->send();
unset($mail);
}
catch (Exception $ex) {
throw new Exception("Mail failed: ".$ex->getMessage());
}