Sending Email with PHP on IIS: Difference between revisions
Jump to navigation
Jump to search
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Enabling Email== | == Enabling Email == | ||
*<strike>Make sure the SMTP port is open on the router.</strike> <<< not necessary for outbound traffic | * <strike>Make sure the SMTP port is open on the router.</strike> <<< not necessary for outbound traffic | ||
*Make sure the SMTP service is running on the server. | * Make sure [[Starting_and_Stopping_SMTP_on_Dev_Server|the SMTP service is running on the server]]. | ||
== PHP Configuration == | |||
=== Specifying the sender === | |||
The real hurtle I encountered was this error: | The real hurtle I encountered was this error: | ||
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
| Line 23: | Line 26: | ||
sendmail_from = me@domain.com | sendmail_from = me@domain.com | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Specifying the SMTP host === | |||
The "`SMTP`" setting in `php.ini` controls the SMTP server value. | |||
Within an application this setting can be changed with `ini_set()`: | |||
<syntaxhighlight lang="php"> | |||
ini_set('SMTP', '[SMTP_HOSTNAME]'); | |||
</syntaxhighlight> | |||
==PHP Code== | ==PHP Code== | ||
mailer class | mailer class | ||
Latest revision as of 17:51, 15 August 2013
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());
}