I am attempting to simplify a custom Wordpress PHPMailer code to send emails to a single address vs using SMTP / G Suite>
- Rewrite code to replace SMTP, retaining current recipient fields, etc.
- Use SSL
- Note: Switched servers, SMTP was with GoDaddy., trying to avoid setting up on A2 Hosting
require_once($_SERVER["DOCUMENT_ROOT"] . '/wp-blog-header.php');
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
require ABSPATH . '/vendor/autoload.php';
function send_mail($email, $subject, $heading, $member_name, $body) {
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
// server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password code';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// recipients
$mail->setFrom(SUPPORT_EMAIL, 'Pearls of Wisdom');
$mail->addAddress($email, $member_name);
$mail->addReplyTo('[email protected]', 'Pearls of Wisdom');
// 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 = $subject;
$mail->Body = $body;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
return true;
} catch (Exception $e) {
return false;
}
}