Sending PHP emails
Spent a few hours over the long weekend working on an email issue with one of my projects. The solutions was incredibly dumb… as in a misconfiguration on the hosting provider, but I did come up with a nice email function with PHPMailer worth sharing.
function SendEmail($to, $subject, $body, $flag)
{
// the true param means it will throw exceptions on errors, which we need to catch
$mail = new PHPMailer(true);
$mail->IsSMTP(); // enable SMTP
// $mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "email_host";
$mail->Port = email_host_port_number;
$mail->IsHTML(true);
$mail->Username = "smtp_username";
$mail->Password = "smtp_username_password";
$mail->SetFrom('from_email', 'from_name');
$mail->ClearReplyTos(); // Need to clear reply-to for only 1 address to show up
$mail->AddReplyTo('reply_to_email', 'reply_to_name');
$mail->AddAddress($to);
if ($flag == "flag_name") { // Use this to programmatically add CCs
$mail->AddCC('cc_email', 'cc_name');
$mail->AddCC('cc_email_2', 'cc_name_2');
}
$mail->Subject = $subject;
$mail->Body = $body;
$mail->Send();
}