编程语言
首页 > 编程语言> > 使用Gmail SMTP通过PHP发送电子邮件

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

作者:互联网

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

我对PHP以及我所要求的很多东西(在过去的几个月中使用它)有些陌生.让我了解问题的根源:

我在学校网络中,在宿舍中设置了自己的服务器.我正在创建一个网站,需要在该网站上验证用户的电子邮件,但是基本的PHP mail()函数不起作用.有人告诉我,我将需要使用SMTP.因此,我认为最简单,最便宜的方法是使用Gmail SMTP.因此,我在Gmail上创建了一个名为verify.impressions@gmail.com的帐户.这是代码.

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

echo "1";

$from = "PersonA `<someone@gmail.com`>";   $to = "`<someoneElse@email.com`>";   $subject = "Activate your account";   $body = "Hey";  

$host = "ssl://smtp.gmail.com";   $port = "465"; //also tried 587   $username = "someone@gmail.com";   $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.";

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

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

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

解决方法:

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

我使用的示例代码:

require("smtp/smtp.php");
require("sasl/sasl.php");
$from = 'youraddress@gmail.com';
$to = 'some@email.com';

$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com";
$smtp->host_port='465';
$smtp->user='youraddress@gmail.com';
$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"));

标签:gmail,smtp,pear,php
来源: https://codeday.me/bug/20191208/2091830.html