1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95:
<?php
/**
* Created by PhpStorm.
* User: samtax
* Date: 08/07/2018
* Time: 7:47 AM
*/
/************************************************
* PHP Mailer
************************************************/
require PATH_LIBRARY.'mail/PHPMailer-master/src/Exception.php';
require PATH_LIBRARY.'mail/PHPMailer-master/src/PHPMailer.php';
require PATH_LIBRARY.'mail/PHPMailer-master/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/**
* @return PHPMailer
* @param bool $exception
*
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
*
*
*/
function mailer($exception = false){
$mail = new PHPMailer($exception); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Host = Config1::MAIL_HOST; // Specify main and backup SMTP servers
$mail->Username = Config1::MAIL_EMAIL; // SMTP username
$mail->Password = Config1::MAIL_PASSWORD; // SMTP password
$mail->Port = Config1::MAIL_PORT; // TCP port to connect to
return $mail; // - //$mail->send();
}
function mailer_send_mail_to_list($toUserEmail_and_UserName_keyValue = [], $subject, $htmlMessageContent, $attachmentPath = null, $fromEmail = null, $fromUserName_orCompanyName = null, $exception = false){
$mail = mailer($exception); // Passing `true` enables exceptions
try {
$result = '';
ob_start();
//Recipients
$mail->setFrom(($fromEmail? $fromEmail: Config1::APP_DEVELOPER_EMAIL), ($fromUserName_orCompanyName? $fromUserName_orCompanyName: Config1::APP_DEVELOPER_NAME));
$mail->addReplyTo(($fromEmail? $fromEmail: Config1::APP_DEVELOPER_EMAIL), ($fromUserName_orCompanyName? $fromUserName_orCompanyName: Config1::APP_DEVELOPER_NAME));
foreach ($toUserEmail_and_UserName_keyValue as $email=>$userName) {
$email1 = is_numeric($email)? $userName: $email;
$userName1 = is_numeric($email)? Form1::extractUserName($userName, false): $userName;
$mail->addAddress($email1, $userName1); // Add a recipient
}
//Attachments
if($attachmentPath) $mail->addAttachment($attachmentPath, 'Attachment File');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $htmlMessageContent;
//$mail->AltBody = Html1::removeTag($htmlMessageContent);
$status = $mail->send();
$result = ob_get_contents();
ob_end_clean();
if($exception && $result) dd( $result, 'Mail Status' );
return ResultStatus1::make($status,$status? 'Message has been sent': 'Message could not be sent. Error is : '.$mail->ErrorInfo, null);
} catch (Exception $e) {
return ResultStatus1::falseMessage('Message could not be sent. Error is : '.$mail->ErrorInfo);
}
}