我编写了一个社交应用程序,用户可以在其中注册和登录。
为此,我必须发送电子邮件确认和其他类似的邮件。
我想知道,是否编写了手动代码(通过cronjob每小时运行一次)是否足够,还是应该使用mailchimp或其他大量电子邮件发送网站?我不想让我的电子邮件成为垃圾邮件。目前不是,但恐怕将来可能会改变。
我写了以下内容:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Select maximum 50 unsent mails
$qry = $db->prepare('SELECT q.*, u.*, c.* FROM emailQueuer AS q INNER JOIN userData AS u ON u.id = q.userId INNER JOIN emailConfirmations AS c ON c.userId = q.userId WHERE q.sendingStatus = 0 AND q.sentStatus = 0 AND q.retryAttempts < 5 LIMIT 0, 50');
$qry->execute();
if ($qry->rowCount() > 0) {
$mails = $qry->fetchAll();
for( $i = 0 ; $i < count($mails) ; $i++ ){
// BASIC DATA
$email = $mails[$i]["email"];
$userId = $mails[$i]["userId"];
// ...ETC
$subject = "Test";
$contentJSON = $mails[$i]["content"];
$content = json_decode($contentJSON, true);
$patterns = $content["patterns"];
$patterns []= "/{email-address}/";
$patterns []= "/{user-id}/";
ob_start();
include(ROOT_DIR . '/resources/email-sheets/testsheet.html');
$output = ob_get_contents();
ob_end_clean();
$mailHTML = preg_replace($patterns, $replacements, $output);
$mailPLAIN = $subject;
$qry = $db->prepare('UPDATE emailQueuer SET sendingStatus = 1, retryAttempts = (retryAttempts + 1) WHERE id = :id');
$qry->bindParam(':id', $mails[$i]["id"], PDO::PARAM_INT);
$qry->execute();
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'mail.privateemail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply@mysocialappsite.com';
$mail->Password = 'XXXXXX';
$mail->setFrom('noreply@mysocialappsite.com', 'mysocialappsite');
$mail->addReplyTo('contact@mysocialappsite.com', 'Contact');
$mail->addAddress($email, ($firstName . ' ' . $lastName));
$mail->Subject = $subject;
$mail->msgHTML($mailHTML, __DIR__);
$mail->AltBody = $mailPLAIN;
$mail->CharSet = 'UTF-8';
try {
$mail->send();
// Success
$qry = $db->prepare('UPDATE emailQueuer SET sendingStatus = 0, sentStatus = 1, time = :time WHERE id = :id');
$qry->bindParam(':id', $mails[$i]["id"], PDO::PARAM_INT);
$qry->bindParam(':time', Time(), PDO::PARAM_INT);
$qry->execute();
} catch (Exception $e) {
// Error
echo 'Mailer Error: ' . $mail->ErrorInfo;
$qry = $db->prepare('UPDATE emailQueuer SET sendingStatus = 0, time = :time WHERE id = :id');
$qry->bindParam(':id', $mails[$i]["id"], PDO::PARAM_INT);
$qry->bindParam(':time', Time(), PDO::PARAM_INT);
$qry->execute();
//Reset the connection to abort sending this message
//The loop will continue trying to send to the rest of the list
$mail->smtp->reset();
}
//Clear all addresses and attachments for the next iteration
$mail->clearAddresses();
$mail->clearAttachments();
}
}
我使用SSL连接,电子邮件提供商是namecheap.com (站点提供商为DigitalOcean)
您对此有何看法?