为什么我在swift mail php

时间:2016-06-23 12:38:44

标签: php email

我已经使用这些参数配置了SwiftMailer但是当我尝试发送邮件时,我遇到了错误。

代码:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls')
    ->setUsername('mail@gmail.com')
    ->setPassword('password');

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

Swift::init('swiftmailer_configurator');
// Create the message
$message = Swift_Message::newInstance()
    // Give the message a subject
    ->setSubject($sujet)
    // Set the From address with an associative array
    ->setFrom(array('Mail@mail.com' => 'Site Mail'))
    // Set the To addresses with an associative array
    ->setTo(array($mail))
    // Give it a body
    ->setBody($texte)
    // And optionally an alternative body
    ->addPart("<q>$texte</q>", 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo "Sent";
} else {
    echo "Failed";
}

结果:

  

致命错误:未捕获异常'Swift_TransportException'   消息'in   C:\ WAMP \ WWW \邮件\ swiftmailer \ LIB \类\雨燕\传输\ AbstractSmtpTransport.php   在第383行(!)Swift_TransportException:预期的响应代码250   但得到代码“535”,消息“535-5.7.8用户名和密码没有   公认。在535 5.7.8了解更多信息   https://support.google.com/mail/answer/14257 e69sm88886wma.2 - gsmtp“   在   C:\ WAMP \ WWW \邮件\ swiftmailer \ LIB \类\雨燕\传输\ AbstractSmtpTransport.php   在第383行

2 个答案:

答案 0 :(得分:1)

正如错误消息所示,

  

535-5.7.8不接受用户名和密码

您需要授权将Gmail用于外部应用:转到https://www.google.com/settings/security/lesssecureapps,然后将Access for less secure apps更改为enabled

如果它仍然不起作用,您可以按照this page上的建议进行操作。

答案 1 :(得分:0)

好的,我通过使用我的亲邮件解决了我的问题而不是我的个人邮件