【发布时间】:2020-02-28 03:42:21
【问题描述】:
在这篇文章之前我已经进行了一些搜索,但我似乎仍然无法让它工作。我正在尝试使用 PHPMailer 设置一个 cron 作业,以每隔一段时间发送一封电子邮件。如果我手动运行下面的脚本,但它在 cron 作业调度程序中不起作用,则它确实有效。
对于此示例 - 我将其设置为每分钟运行一次。我认为它必须与“vendor/autoload.php”做一些事情并且它的路径没有正确加载?出于安全原因以及此帖子的收件人,我没有使用 api 密钥添加我的 SMTP 凭据。
这是我的 PHPMailer 代码:
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
// Server settings
// $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = ''; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
// Recipients
$mail->setFrom('email@email.com', '');
$mail->addAddress('email@email.com', ''); // Add a recipient
$mail->addReplyTo('email@email.com', '');
// $mail->addCC('cc@example.com');
// $mail->addBCC('');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'PHPMailer email';
// $mail->Body = 'This is the HTML message body <b>in bold!</b>';
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->msgHTML(file_get_contents('email.html'), __DIR__); // Use this if not using the above code
// ********* PHP-MAILER ********* //
$mail->send();
echo 'Email sent!';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
如果有人可以帮助我,我将不胜感激!
【问题讨论】: