使用 Gmail SMTP 通过 PHP 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5759419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:24:05  来源:igfitidea点击:

Using Gmail SMTP to send email with PHP

phpsmtpgmailpear

提问by ingshtrom

I have a problem I have been working on for about a week and can't find an answer. As a preface to this all, I have searched the internet for all sorts of things. There are a lot of answers for this problem, but none seem to be helping me.

我有一个问题,我已经解决了大约一个星期,但找不到答案。作为这一切的序言,我在互联网上搜索了各种各样的东西。这个问题有很多答案,但似乎没有一个能帮助我。

I am somewhat new to PHP and a lot of the stuff I am asking for (been using it over the past few months). Let me get to the base of the problem:

我对 PHP 和我要求的很多东西有点陌生(过去几个月一直在使用它)。让我来看看问题的根源:

I am on a school network with my own server set up in my dorm room. I am creating a website where I need to verify a user's email, but the basic PHP mail() function does not work. I have been told that I will need to use SMTP. So I decided the easiest and cheapest way was with Gmail SMTP. I created an account on Gmail called [email protected] for this reason. Here is the code.

我在学校网络上,在我的宿舍里设置了我自己的服务器。我正在创建一个需要验证用户电子邮件的网站,但基本的 PHP mail() 函数不起作用。有人告诉我,我需要使用 SMTP。所以我决定最简单和最便宜的方法是使用 Gmail SMTP。为此,我在 Gmail 上创建了一个名为 [email protected] 的帐户。这是代码。

echo "starting mail sending";
             require_once("pear/share/pear/Mail.php");

echo "1";

$from = "PersonA `<[email protected]`>";   $to = "`<[email protected]`>";   $subject = "Activate your account";   $body = "Hey";  

$host = "ssl://smtp.gmail.com";   $port = "465"; //also tried 587   $username = "[email protected]";   $password = "password";  

echo "2";

$headers = array ('From' => $from,    'To' => $to,    'Subject' => $subject);

echo "3";

$mailer_params['host'] = $host;   $mailer_params['port'] = $port;   $mailer_params['auth'] = true;   $mailer_params['username'] = $username;   $mailer_params['password'] = $password;                                              
                 $smtp = Mail::factory('smtp', $mailer_params);

echo "4";

error_reporting(E_ALL);

echo "5";

if (PEAR::isError($smtp)) {   die("Error : " . $smtp->getMessage()); }

echo "6";

$mail = $smtp->send($to, $headers, $body) or die("Something bad happened"); 

echo "7";

if (PEAR::isError($mail)) {echo($mail->getMessage();} else {echo(Message successfully sent!);}
                 echo "mail sent hopefully.";

So basically the code just stops at the line:

所以基本上代码只是停在这一行:

$mail = $smtp->send($to, %headers, $);

I have tried printing errors, but I just have no idea what to do now. Any tips and help is appreciated. Thanks!!

我试过打印错误,但我现在不知道该怎么办。任何提示和帮助表示赞赏。谢谢!!

采纳答案by Jorj

Use this class: http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

使用这个类:http: //www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

The sample code I use:

我使用的示例代码:

require("smtp/smtp.php");
require("sasl/sasl.php");
$from = '[email protected]';
$to = '[email protected]';

$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com";
$smtp->host_port='465';
$smtp->user='[email protected]';
$smtp->password='XXXXXXXXX';
$smtp->ssl=1;
$smtp->debug=1;       //0 here in production
$smtp->html_debug=1; //same

$smtp->SendMessage($from,array($to),array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"));

回答by Arihant Nahata

I always use this to send mails using gmail as SMTP server.
Very Simple to configure

我总是使用来使用 gmail 作为 SMTP 服务器发送邮件。
配置非常简单

回答by Devraj

If you are you using Linux I recommend setting up postfix on your local machine and they asking that to relay the email forwards via an external SMTP service, in your case Gmail.

如果您使用的是 Linux,我建议您在本地计算机上设置 postfix,他们要求通过外部 SMTP 服务转发电子邮件,在您的情况下为 Gmail。

http://bookmarks.honewatson.com/2008/04/20/postfix-gmail-smtp-relay/

http://bookmarks.honewatson.com/2008/04/20/postfix-gmail-smtp-relay/

The reason is that your PHP script can timeout incase there's a delay contact Gmail. So you would use Postfix to queue the email on the local server, let the PHP script execution die and trust Postfix to send the email via Gmail.

原因是您的 PHP 脚本可能会超时,以防延迟联系 Gmail。因此,您将使用 Postfix 在本地服务器上对电子邮件进行排队,让 PHP 脚本执行终止并信任 Postfix 通过 Gmail 发送电子邮件。

If you are using Windows, I am sure you can find an equivalent SMTP relay application (should be built as a rough guess).

如果你使用的是 Windows,我相信你可以找到一个等效的 SMTP 中继应用程序(应该是一个粗略的猜测)。

回答by cweiske

Many public networks block connections on the SMTP port to remote machines to stop spammers inside their network.

许多公共网络会阻止 SMTP 端口上与远程机器的连接,以阻止其网络内的垃圾邮件发送者。

You have two choices:

你有两个选择:

  1. Find a smtp server that uses a different port than 25
  2. Use the official SMTP server of your school's network. There is always one since people need to send mails out.
  1. 查找使用不同于 25 端口的 smtp 服务器
  2. 使用您学校网络的官方 SMTP 服务器。总是有一个,因为人们需要发送邮件。