首页 > 解决方案 > Swift Mailer 不工作并且没有给出错误信息

问题描述

我尝试通过 Swift-Mailer 发送电子邮件。根据文档,它应该像这样工作,但我既没有收到邮件也没有收到错误消息。

这是 PHP Swift 文件的代码:

<?php
    require_once '/composer/vendor/autoload.php';

    // Create the Transport
    $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
    ->setUsername('*******@gmail.com')
    ->setPassword('password')
    ;

    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);

    // Create a message
    $message = (new Swift_Message('Wonderful Subject'))
    ->setFrom(['john@doe.com' => 'John Doe'])
    ->setTo(['infinity.community.work@gmail.com', 'other@domain.org' => 'A name'])
    ->setBody('Here is the message itself')
    ;

    // Send the message
    $result = $mailer->send($message);
?>

这是composer.json:

{
    "require": {
        "swiftmailer/swiftmailer": "^6.0"
    }
}

标签: phpgmailswiftmailer

解决方案


再次检查此语法并确保电子邮件或密码拼写正确:

$trp = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
        ->setUsername('*********@gmail.com')
        ->setPassword('password');

$mailer = Swift_Mailer::newInstance($trp);

$message = Swift_Message::newInstance('Wonderful Subject')
    ->setFrom(['john@doe.com' => 'John Doe'])
    ->setTo(['infinity.community.work@gmail.com', 'other@domain.org' => 'A name'])
    ->setBody('Here is the message itself');

$mailer->send($message);

编辑:与 SwiftMailer 5.4版成功合作


推荐阅读