Skip to content

Instantly share code, notes, and snippets.

@RadGH
Created May 13, 2013 16:16
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 RadGH/5569528 to your computer and use it in GitHub Desktop.
Save RadGH/5569528 to your computer and use it in GitHub Desktop.
Easy to use PHP mail function for smtp using phpmailer
<?php
/*
--- Send mail using SMTP
Parameters:
to (string - required, email address of recipient)
to_name (string -optional, name of recipient
from (string - required, email address of sender)
from_name (string - optional, name of sender)
subject (string - optional, inherits 40 char from body if blank)
body (string - required, body text HTML)
attachments (string - optional, path to file)
attachments (array - optional, array of path/filenames) =>
path (string, required if attachments is array) full path to file
filename (string, always optional) short name of file that will be downloaded by user
Usage:
php_mail_smtp(array(
'to' => 'jdoe@example.org',
'from' => 'bob@example.org',
'from_name' => 'bob smith',
'body' => '<strong>Hello J!</strong>',
'attachments' => array(
array(
'path' => '/var/www/my-website.com/file.jpg',
'filename' => 'my-amazing-file.jpg'
),
array(
'path' => '/var/www/my-website.com/another-file.png'
)
)
));
*/
function php_mail_smtp( $args ) {
require_once($_SERVER['DOCUMENT_ROOT'] . '/class.phpmailer.php');
// Turn all argument keys into actual variables, based on key name
extract($args);
// Required arguments
if (!$to || !$from || !$body) return false;
// Inherit defaults if not set
if (!isset($to_name) || !$to_name) $to_name = $to;
if (!isset($from_name) || !$from_name) $from_name = $from;
if (!isset($subject) || !$subject) $subject = substr($body, 0, 40); // First 40 chars of body as subject
if (!isset($attachments) || !$attachments || !is_array($attachments))
$attachments = array();
$jobapp_email = 'sender@example.org';
$jobapp_name = 'Email Sender';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp-1.example.com;smtp-2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'sender@example.org'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = $from;
$mail->FromName = $from_name;
$mail->AddAddress($to, $to_name); // Add a recipient
$mail->AddReplyTo($jobapp_email, $jobapp_name);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
foreach($attachments as $attachment) {
$path = false;
$filename = false;
// Extract path and filename, when available, or if simple string just use as path
if ( is_array($attachment) ) {
if (isset($attachment['path'])) $path = $attachment['path'];
if (isset($attachment['filename'])) $filename = $attachment['filename'];
}else{
$path = $attachment;
}
// We couldn't find path, ignore
if (!$path) continue;
// If path isn't correct, ignore
if ( !$path || !file_exists($path) ) continue;
if ($filename)
$mail->AddAttachment($path, $filename); // Add attachments
else
$mail->AddAttachment($path); // Name is optional
}
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment