php - 从 PHP 页面使用 GMail SMTP 服务器发送电子邮件

标签 php email smtp gmail

我正在尝试从 PHP 页面通过 GMail 的 SMTP 服务器发送电子邮件,但出现此错误:

authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: mx.google.com at your service, [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]

有人可以帮忙吗?这是我的代码:

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <ramona@microsoft.com>";
$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("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

最佳答案

// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$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('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

关于php - 从 PHP 页面使用 GMail SMTP 服务器发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/712392/

相关文章:

Python/Flask HTML 表单发送电子邮件

PHP 正则表达式 : Matching Ands - & and &amp;

php - FacebookAds SDK 光标无法在/reportstats 端点上工作

email - 如何在 docker/redmine 容器实例上配置 'smtp ' 设置?

c# - .Net 的缓冲电子邮件记录器(最好是 NLog 目标)?

email - 为什么 postfix 中的 smtpd_recipient_limit 和其他电子邮件相关选项不起作用?

php - 稍后在 php 中回显时,mysql 查询中的 "Ordering by"不起作用?

php - Wordpress 删除机器人元标记 noindex

iphone - iOS 5从应用程序发送电子邮件的最简单方法

PHP mail() 函数返回 true,但不发送邮件