<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
function sendMail($to, $subject, $message, $from_email, $from_name, $cc = [], $bcc = [], $attachments = []) {
$mail = new PHPMailer(true);
try {
// SMTP SETTINGS
$mail->isSMTP();
$mail->Host = "smtp.yourserver.com";
$mail->SMTPAuth = true;
$mail->Username = "your-email@domain.com";
$mail->Password = "your-smtp-password";
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// FROM & TO
$mail->setFrom($from_email, $from_name);
// To accepts single or array
if (is_array($to)) {
foreach ($to as $email) {
$mail->addAddress($email);
}
} else {
$mail->addAddress($to);
}
// CC array
if (!empty($cc)) {
foreach ($cc as $email) {
$mail->addCC($email);
}
}
// BCC array
if (!empty($bcc)) {
foreach ($bcc as $email) {
$mail->addBCC($email);
}
}
// Attachments
if (!empty($attachments)) {
foreach ($attachments as $file) {
if (file_exists($file)) {
$mail->addAttachment($file);
}
}
}
// CONTENT
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
return true;
} catch (Exception $e) {
return "Mailer Error: " . $mail->ErrorInfo;
}
}
?>

0 Comments