首页 > 解决方案 > php邮件功能工作PHPMailer不工作

问题描述

我的网站上的邮件功能和 PHPMailer 包有问题。

起初我尝试使用邮件功能发送电子邮件:

mail('myemail@gmail.com', 'Test Mail - Raw', 'Test message.');

邮件已成功发送,但未在我的收件箱或垃圾邮件中收到。

经过大量搜索后,我在邮件功能中添加了一个标题:

mail('myemail@gmail.com', 'Test Mail - Without Reply-To', 'Test message', "From: mahdyaslami <myemail@gmail.com>");

最后我在垃圾邮件中发现我发送的邮件带有黄色警告。

我想使用 PHPMailer 包,因为我想附加文件并添加此代码。

protected function send($from, $to, $subject, $body, $altBody, $attachmentPath): bool
{
    // Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->SMTPSecure = 'ssl';
    $mail->isSMTP();
    $mail->isSendmail();

    $mail->setFrom($from);
    $mail->addReplyTo($from, $from);
    $mail->addAddress($to);
    $mail->isHTML(true);

    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AltBody = $altBody;

    if ($attachmentPath) {
        $mail->addAttachment($attachmentPath);
    }

    $mail->send();
    return true;
}

但我有第一个问题,我的邮件已成功发送,但我无法在收件箱或垃圾邮件中收到邮件。

标签: phpemailphpmailer

解决方案


推荐阅读