Skip to content

Instantly share code, notes, and snippets.

@arnorhs
Created February 20, 2011 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnorhs/836042 to your computer and use it in GitHub Desktop.
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
<?php
mail(
'ron.burgundy@KVWNTV.com',
'This is the subject',
'This is a message.'
);
?>
<?php
// added a "from" address. Notice that headers are seperated by a CRLF (\r\n). \n will not work.
$headers = array(
'From: veronica.corningstone@nashville.us',
'Reply-To: no-reply@nashville.us'
);
$headers = implode("\r\n", $headers)."\r\n";
mail(
'ron.burgundy@KVWNTV.com',
'This is the subject',
'This is a message.',
$headers
);
?>
<?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 = "ron.burgundy@KVWNTV.com";
$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('ron.burgundy@KVWNTV.com', '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("veronica.corningstone@nashville.us", "Veronica");
try {
$mail->Send();
echo "<h1>Message Sent OK</h1>";
} catch (phpmailerException $e) {
// Pretty error messages from PHPMailer
echo $e->errorMessage();
}
?>
<?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');
?>
<?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 = "your.username@your-host.com";
$mail->Password = "iamstillsoawesome-2020";
$mail->SetFrom('ron.burgundy@KVWNTV.com', '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("veronica.corningstone@nashville.us", "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