Sending Mail from PHP

While it’s relatively easy to send basic email from PHP scripts, it’s not necessarily easy or straightforward to do it in such a way that bounced messages go to you rather than to Birdhouse administrators, or more importantly, to minimize the likelihood that receiving mail servers will mark your message as spam (which can result in mail sent through Birdhouse being added to blacklists).

To simplify the process for PHP developers on Birdhouse, we’ve installed the PHPMailer class. If implemented exactly as below, you’ll do yourself and all Birdhouse users a huge favor. Thanks for your assitance with this.


<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();

// Note: YES, you need to use both the From and Sender lines below.
// From is the simple From header, while Sender: sets the Return-Path
// header, which is essential to preventing your message from being
// flagged as spam by receiving servers.
$mail->From = "you@yourdomain.com";
$mail->Sender = "you@yourdomain.com";
$mail->FromName = "Your Name";
$mail->AddAddress("$recipient_email");

// You can add additional addresses here if you like;
// e.g. you can send a copy to yourself.

$mail->AddAddress("$otheraddress@yourdomain.com");

$mail->Subject = "Your subject line here";
$mail->Body = "$message_text";

// Comment out the next line if you want no forced line wrapping.
$mail->WordWrap = 70;

if(!$mail->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

Return to FAQs