Created
February 20, 2011 15:21
-
-
Save arnorhs/836042 to your computer and use it in GitHub Desktop.
Example code for a blog entry about delivering email using PHP on arnorhs.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
mail( | |
'[email protected]', | |
'This is the subject', | |
'This is a message.' | |
); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// added a "from" address. Notice that headers are seperated by a CRLF (\r\n). \n will not work. | |
$headers = array( | |
'From: [email protected]', | |
'Reply-To: [email protected]' | |
); | |
$headers = implode("\r\n", $headers)."\r\n"; | |
mail( | |
'[email protected]', | |
'This is the subject', | |
'This is a message.', | |
$headers | |
); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
you'll need to have already downloaded PHP mailer and copied the | |
files to the same location as your PHP script | |
*/ | |
require_once('class.phpmailer.php'); | |
// the parameter true specifies that errors will be thrown instead | |
// of functions returning false | |
$mail = new PHPMailer(true); | |
// Tell PHP Mailer that you'll be using an SMTP server: | |
$mail->IsSMTP(); | |
/* | |
Sometimes you can use SMTP servers without a user/password. That mostly | |
when you are connecting within the same network as your SMTP server, but | |
you'll probably not be doing that, so enable SMTP authentication. | |
*/ | |
$mail->SMTPAuth = true; | |
// set up authentication parameters: | |
$mail->Host = "smtp.KVWNTV.com"; | |
$mail->Port = 25; | |
$mail->Username = "[email protected]"; | |
$mail->Password = "iamsoawesome"; | |
/* | |
Now, you'll want to tell the recipient who sent the email - it might | |
be some totally different address/username that you use for | |
authentication on the SMTP server | |
*/ | |
$mail->SetFrom('[email protected]', 'Ron Burgundy'); | |
$mail->Subject = "This is the subject"; | |
$mail->AltBody = "This is a text version of the message"; | |
$mail->MsgHTML("This is the <b>HTML</b> version of the email"); | |
$mail->AddAddress("[email protected]", "Veronica"); | |
try { | |
$mail->Send(); | |
echo "<h1>Message Sent OK</h1>"; | |
} catch (phpmailerException $e) { | |
// Pretty error messages from PHPMailer | |
echo $e->errorMessage(); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Adding an attachment to your email. This is very hard if you do it | |
// yourself, manually with the mail() function: | |
$mail->AddAttachment('/path/to/your/file.zip'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('class.phpmailer.php'); | |
$mail = new PHPMailer(true); | |
$mail->IsSMTP(); | |
$mail->SMTPAuth = true; | |
// set up Sendgrid's SMTP settings (values may vary between accounts) | |
$mail->Host = "smtp.sendgrid.net"; | |
$mail->Port = 25; | |
$mail->Username = "[email protected]"; | |
$mail->Password = "iamstillsoawesome-2020"; | |
$mail->SetFrom('[email protected]', 'Ron Burgundy'); | |
$mail->Subject = "This is the subject"; | |
$mail->AltBody = "This is a text version of the message"; | |
$mail->MsgHTML("This is the <b>HTML</b> version of the email"); | |
$mail->AddAddress("[email protected]", "Veronica"); | |
try { | |
$mail->Send(); | |
echo "<h1>Message Sent OK using sendgrid's servers</h1>"; | |
} catch (phpmailerException $e) { | |
// Pretty error messages from PHPMailer | |
echo $e->errorMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment