Created
August 21, 2017 11:42
-
-
Save thedrint/703c2ac2d2b1ffe7cd4cd0833838fea5 to your computer and use it in GitHub Desktop.
Typical bitrix custom_mail function phpMailer and smtp (supports attaches)
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
function custom_mail ($to, $subject, $message, $additional_headers, $additional_parameters) | |
{ | |
/*\AddMessage2Log( | |
'To: '.$to.PHP_EOL. | |
'Subject: '.$subject.PHP_EOL. | |
'Message: '.$message.PHP_EOL. | |
'Headers: '.$additional_headers.PHP_EOL. | |
'Params: '.$additional_parameters.PHP_EOL | |
);*/ | |
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php'; | |
$to = str_replace(' ','',$to); | |
$mail = new PHPMailer; | |
$mail->CharSet = 'UTF-8'; | |
$address = explode(',', $to); | |
foreach ($address as $addr) | |
$mail->addAddress($addr); | |
// Просматриваем заголовки на предмет аттачей | |
$headers = explode("\n", $additional_headers); | |
// Определить аттачи легко - передается сепаратор таким вот образом | |
$attachHeader = 'Content-Type: multipart/mixed; boundary='; | |
foreach( $headers as $h ) | |
{ | |
// Если есть заголовок с ограничиителем | |
if( stripos($h, $attachHeader) === 0 ) | |
{ | |
// Вытаскиваем значение сепаратора | |
$bndr = substr($h, strlen($attachHeader)); | |
$bndr = trim($bndr, '"'); | |
// И дублируем его же, но в свойство phpMailer. | |
$mail->ContentType = 'multipart/mixed; boundary="' . $bndr . '"'; | |
// I know its dirty hack | |
break; | |
} | |
} | |
if (substr_count($message, "Content-Type: text/html")) | |
{ | |
// $mail->Encoding = '8bit'; | |
} | |
else | |
{ | |
// $mail->Encoding = '8bit'; | |
//$mail->isHTML(true); // Set email format to HTML | |
} | |
$mail->Subject = $subject; | |
$mail->Body = $message; | |
if( defined('PROJECT_SMTP_HOST') ) | |
{ | |
$mail->IsSMTP(); | |
$mail->SMTPDebug = defined('PROJECT_SMTP_DEBUG_LEVEL') ? PROJECT_SMTP_DEBUG_LEVEL : 0; // enables SMTP debug information (for testing) | |
$mail->Host = PROJECT_SMTP_HOST; | |
$mail->SMTPAuth = true; // enable SMTP authentication | |
$mail->Username = PROJECT_SMTP_USERNAME; | |
$mail->Password = PROJECT_SMTP_PASSWORD; | |
$mail->From = PROJECT_SMTP_FROM; | |
$mail->FromName = PROJECT_SMTP_FROMNAME; | |
$mail->SMTPSecure = PROJECT_SMTP_SECURITY; // "tls" or "ssl" | |
$mail->Port = PROJECT_SMTP_PORT; | |
//HACK: Dirty hack! if ssl not configured at hosting | |
$mail->SMTPOptions = array( | |
'ssl' => array( | |
'verify_peer' => false, | |
'verify_peer_name' => false, | |
'allow_self_signed' => true | |
), | |
); | |
} | |
if( !$mail->send() ) | |
{ | |
\AddMessage2Log('Mailer Error: ' . $mail->ErrorInfo); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment