84 lines
2.0 KiB
PHP
84 lines
2.0 KiB
PHP
<?php
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
require_once 'vendor/autoload.php';
|
|
|
|
class Gmail
|
|
{
|
|
function send(
|
|
$sender,
|
|
$subject,
|
|
$message,
|
|
$recipients,
|
|
$cc,
|
|
$isHtml = false,
|
|
$attachment = [],
|
|
$username,
|
|
$password
|
|
) {
|
|
$mail = new PHPMailer(true);
|
|
//Configure an SMTP
|
|
$mail->isSMTP();
|
|
$mail->Host = "smtp.gmail.com";
|
|
$mail->SMTPAuth = true;
|
|
// $mail->Username = "sascpone@gmail.com";
|
|
// $mail->Password = "ahpn zkcw ymkg hsij";
|
|
$mail->Username = $username;
|
|
$mail->Password = $password;
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$mail->Post = 587;
|
|
|
|
$mail->setFrom($sender["email"], $sender["name"]);
|
|
|
|
foreach ($recipients as $rec) {
|
|
$mail->addAddress($rec["email"], $rec["name"]);
|
|
}
|
|
|
|
foreach ($cc as $rec) {
|
|
$mail->addCC($rec["email"], $rec["name"]);
|
|
}
|
|
|
|
|
|
$mail->isHTML($isHtml);
|
|
|
|
$mail->Subject = $subject;
|
|
|
|
$mail->Body = $message;
|
|
|
|
if (count($attachment) > 0) {
|
|
$arr_tmp = [];
|
|
for ($i_att = 0; $i_att < count($attachment); $i_att++) {
|
|
$tmpName = tempnam("/tmp", "att-");
|
|
$fname = $attachment[$i_att]["name"];
|
|
$url = $attachment[$i_att]["url"];
|
|
if ($tmpName === false) {
|
|
return [false, "Failed Create tmpFile for $fname"];
|
|
}
|
|
$tmp_data = file_get_contents($url);
|
|
$status = file_put_contents($tmpName, $tmp_data);
|
|
if ($status === false) {
|
|
return [false, "Failed Create tmpFile for $fname put"];
|
|
}
|
|
$arr_tmp[] = $tmpName;
|
|
$mail->addAttachment($tmpName, $fname);
|
|
}
|
|
}
|
|
if (!$mail->send()) {
|
|
$mail->smtpClose();
|
|
foreach ($arr_tmp as $tmpName) {
|
|
unlink($tmpName);
|
|
}
|
|
return [false, $mail->ErrorInfo];
|
|
} else {
|
|
$mail->smtpClose();
|
|
foreach ($arr_tmp as $tmpName) {
|
|
unlink($tmpName);
|
|
}
|
|
return [true, ""];
|
|
}
|
|
}
|
|
}
|