Last active
September 10, 2019 05:05
-
-
Save craigvantonder/e0503be23fada59834fa7e31c73f78a1 to your computer and use it in GitHub Desktop.
A wrapper for PHP mailer
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 | |
function sendMail($mailFrom, $replyTo, $mailTo, $ccTo, $mailSubject, $mailContent, $mailTemplate) | |
{ | |
// Create a new PHPMailer instance | |
$mail = new PHPMailer(); | |
// Tell PHPMailer to use SMTP | |
$mail->isSMTP(); | |
// Enable SMTP debugging | |
// 0 = off (for production use) | |
// 1 = client messages | |
// 2 = client and server messages | |
$mail->SMTPDebug = 0; | |
//Ask for HTML-friendly debug output | |
$mail->Debugoutput = 'html'; | |
// Set the hostname of the mail server | |
$mail->Host = "hostname of your mail server"; | |
// Set the SMTP port number - likely to be 25, 465 or 587 | |
$mail->Port = 25; | |
// Whether to use SMTP authentication | |
$mail->SMTPAuth = true; | |
// Secure transfer enabled REQUIRED for GMail | |
$mail->SMTPSecure = 'ssl'; | |
// Username to use for SMTP authentication | |
$mail->Username = "username for sending emails"; | |
// Password to use for SMTP authentication | |
$mail->Password = "password for the above username"; | |
// Set who the message is to be sent from | |
$mail->setFrom($mailFrom['address'], $mailFrom['name']); | |
// If the reply to values are present | |
if (isset($replyTo['address']) && isset($replyTo['name'])) { | |
// Set an alternative reply-to address | |
$mail->addReplyTo($replyTo['address'], $replyTo['name']); | |
} | |
// Set who the message is to be sent to | |
$mail->addAddress($mailTo['address'], $mailTo['name']); | |
// Set who the message is to be cc'd to | |
if (count($ccTo) > 0) { | |
foreach ($ccTo as $ccToKey=>$ccToValues) { | |
$mail->addCC($ccToValues['address'], $ccToValues['name']); | |
} | |
} | |
// If an attachment exists | |
if (isset($mailContent['attachment'])) { | |
// Add it to the email | |
$mail->addAttachment($mailContent['attachment']); | |
} | |
// Set the subject line | |
$mail->Subject = $mailSubject; | |
// Define mail message <----------- NB!!!!!!!! | |
require_once pS . '/mailers/layouts/'.$mailTemplate.'.email.php'; | |
// Convert HTML into a basic plain-text alternative body | |
$mail->msgHTML($mailMessage); | |
// Define basic mail message <----------- NB!!!!!!!! | |
require_once pS . '/mailers/layouts/'.$mailTemplate.'.basic.php'; | |
// Send the message, check for errors | |
if (!$mail->send()) { | |
return array=>(false, "Mailer Error: " . $mail->ErrorInfo); | |
} else { | |
return array=>(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment