我试图从PHP页面通过GMail的SMTP服务器发送电子邮件,但是我收到此错误:
身份验证失败[SMTP:SMTP服务器不支持身份验证(代码:250,响应:mx.google.com为您服务,[98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]
有人可以帮忙吗?这是我的代码:
"; $to = "Ramona Recipient"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "smtp.gmail.com"; $port = "587"; $username = "testtest@gmail.com"; $password = "testtest"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo(" " . $mail->getMessage() . "
"); } else { echo("Message successfully sent!
"); } ?>
小智.. 353
// Pear Mail Library require_once "Mail.php"; $from = ''; $to = ' '; $subject = 'Hi!'; $body = "Hi,\n\nHow are you?"; $headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject ); $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'johndoe@gmail.com', 'password' => 'passwordxxx' )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo(' ' . $mail->getMessage() . '
'); } else { echo('Message successfully sent!
'); }
什么是`Mail.php` ?? 我从哪里获得这个文件? (138认同)
他正在使用[Pear Mail包](http://pear.php.net/package/Mail/docs). (79认同)
谁能请给我一个链接,我可以获得Mail.php文件.因为我尝试了它,它不会工作谢谢 (18认同)
上面这个例子中的@符号在哪里?我在那里看不到一个人! (11认同)
我相信myaccount.gmail.com与电子邮件标准中的myaccount@gmail.com相同. (6认同)
如果指定了服务器,则不需要包含@gmail.只需输入用户名的"myaccount"即可. (3认同)
有没有办法加密密码? (3认同)
我已经尝试了所有这些线程答案,但对于不具有@ gmail.com的GSuite电子邮件,则无济于事。我已经查看并尝试了其他站点的许多操作,但没有任何效果。我认为问题是GSuite。如果有人遇到同样的问题并且可以提出解决方案或指向某个网站,那将是不错的选择。我在使用公司GSuite的gmail实例上。 (3认同)
@Xavi:当我使用那个梨邮件包时,它要求PEAR.php文件 (2认同)
我无法使用上面的代码发送邮件.我发现[Mail.php](http://pear.php.net/package/Mail/download) [here](http://pear.php.net/package/Mail/download)但它包含一个文件`mail`没有任何扩展名.有人可以帮忙吗? (2认同)
此代码不起作用,它会出现此错误:"SMTP:从服务器收到的响应代码无效(代码:535,响应:验证数据不正确)]" (2认同)
安装后需要在Ubuntu上使用此代码.
I am getting `"authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 p80sm2675348pfk.50 - gsmtp)]"`. (2认同)
shasi kanth.. 103
使用Swift邮件程序,通过Gmail凭据发送邮件非常简单:
setUsername('GMAIL_USERNAME') ->setPassword('GMAIL_PASSWORD'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance('Test Subject') ->setFrom(array('abc@example.com' => 'ABC')) ->setTo(array('xyz@test.com')) ->setBody('This is a test mail.'); $result = $mailer->send($message); ?>
我同意,swift邮件是一个邮件解决方案,比弄乱梨更容易.不要忘记启用PHP的php_openssl扩展. (7认同)
对不起我的愚蠢啊.您必须打开您的Gmail帐户,因为有一封电子邮件告诉您启用"安全性较低的应用".然后它现在正在工作heheh (3认同)
这只是"改为第一个",只是改变了GMAIL_USERNAME,GMAIL_PASSWORD以及From和To地址.没有其他解决方案适合我.谢谢. (2认同)
crb.. 54
您的代码似乎没有使用TLS(SSL),这是向Google发送邮件(以及使用端口465或587)所必需的.
您可以通过设置来完成此操作
$host = "ssl://smtp.gmail.com";
您的代码看起来很可疑,就像这个在主机名方案中引用ssl://的示例一样.
// Pear Mail Library require_once "Mail.php"; $from = ''; $to = ' '; $subject = 'Hi!'; $body = "Hi,\n\nHow are you?"; $headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject ); $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'johndoe@gmail.com', 'password' => 'passwordxxx' )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo(' ' . $mail->getMessage() . '
'); } else { echo('Message successfully sent!
'); }
使用Swift邮件程序,通过Gmail凭据发送邮件非常简单:
setUsername('GMAIL_USERNAME') ->setPassword('GMAIL_PASSWORD'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance('Test Subject') ->setFrom(array('abc@example.com' => 'ABC')) ->setTo(array('xyz@test.com')) ->setBody('This is a test mail.'); $result = $mailer->send($message); ?>
您的代码似乎没有使用TLS(SSL),这是向Google发送邮件(以及使用端口465或587)所必需的.
您可以通过设置来完成此操作
$host = "ssl://smtp.gmail.com";
您的代码看起来很可疑,就像这个在主机名方案中引用ssl://的示例一样.
我不推荐Pear Mail.它自2010年以来一直没有更新.还阅读源文件; 源代码几乎已过时,以PHP 4风格编写,并发布了许多错误/错误(谷歌).我正在使用Swift Mailer.
Swift Mailer集成到用PHP 5编写的任何Web应用程序中,提供灵活而优雅的面向对象的方法来发送具有多种功能的电子邮件.
使用SMTP,sendmail,postfix或您自己的自定义传输实现发送电子邮件.
支持需要用户名和密码和/或加密的服务器.
在不剥离请求数据内容的情况下防止标头注入攻击.
发送符合MIME的HTML /多部分电子邮件.
使用事件驱动的插件来自定义库.
处理内存使用率低的大附件和内嵌/嵌入式图像.
它是一个免费的开源您可以下载Swift Mailer并上传到您的服务器.(功能列表从所有者网站复制).
Gmail SSL/SMTP和Swift Mailer的工作示例就在这里......
// Swift Mailer Library require_once '../path/to/lib/swift_required.php'; // Mail Transport $transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465) ->setUsername('username@gmail.com') // Your Gmail Username ->setPassword('my_secure_gmail_password'); // Your Gmail Password // Mailer $mailer = Swift_Mailer::newInstance($transport); // Create a message $message = Swift_Message::newInstance('Wonderful Subject Here') ->setFrom(array('sender@example.com' => 'Sender Name')) // can be $_POST['email'] etc... ->setTo(array('receiver@example.com' => 'Receiver Name')) // your email / multiple supported. ->setBody('Here is the message itself. It can be text orHTML
.', 'text/html'); // Send the message if ($mailer->send($message)) { echo 'Mail sent successfully.'; } else { echo 'I am sure, your configuration are not correct. :('; }
我希望这有帮助.快乐的编码...... :)
IsSMTP(); // telling the class to use SMTP //$mail->Host = "ssl://smtp.gmail.com"; // SMTP server $mail->SMTPDebug = 1; // enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "user@gmail.com"; // GMAIL username $mail->Password = "password"; // GMAIL password $mail->SetFrom('contact@prsps.in', 'PRSPS'); //$mail->AddReplyTo("user2@gmail.com', 'First Last"); $mail->Subject = "PRSPS password"; //$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $address = "user2@yahoo.co.in"; $mail->AddAddress($address, "user2"); //$mail->AddAttachment("images/phpmailer.gif"); // attachment //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?>
SwiftMailer可以使用外部服务器发送电子邮件.
这是一个显示如何使用Gmail服务器的示例:
require_once "lib/Swift.php"; require_once "lib/Swift/Connection/SMTP.php"; //Connect to localhost on port 25 $swift =& new Swift(new Swift_Connection_SMTP("localhost")); //Connect to an IP address on a non-standard port $swift =& new Swift(new Swift_Connection_SMTP("217.147.94.117", 419)); //Connect to Gmail (PHP5) $swift = new Swift(new Swift_Connection_SMTP( "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
问题中列出的代码需要进行两处更改
$host = "ssl://smtp.gmail.com"; $port = "465";
SSL连接需要端口465.
通过Gmail使用phpMailer库发送邮件请从Github下载库文件
isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; // use // $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 587; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'tls'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = "username@gmail.com"; //Password to use for SMTP authentication $mail->Password = "yourpassword"; //Set who the message is to be sent from $mail->setFrom('from@example.com', 'First Last'); //Set an alternative reply-to address $mail->addReplyTo('replyto@example.com', 'First Last'); //Set who the message is to be sent to $mail->addAddress('whoto@example.com', 'John Doe'); //Set the subject line $mail->Subject = 'PHPMailer GMail SMTP test'; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //Attach an image file $mail->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }
我也有这个问题。我设置了正确的设置,并启用了安全性较低的应用程序,但仍然无法正常工作。最后,我启用了此https://accounts.google.com/UnlockCaptcha,它对我有用。我希望这可以帮助别人。