使用Gmail SMTP通过PHP发送电子邮件 [英] Using Gmail SMTP to send email with PHP
问题描述
我有一个问题,我一直在研究大约一周,但找不到答案。作为这一切的前言,我在网上搜索了各种各样的东西。这个问题有很多答案,但似乎没有人帮助我。
我对PHP有点新鲜,以及很多我所要求的东西(在过去几个月里一直使用它)。让我找到问题的根源:
我在学校网络中,在我的宿舍里设置了自己的服务器。我正在创建一个网站,我需要验证用户的电子邮件,但基本的PHP邮件()函数不起作用。我被告知我需要使用SMTP。所以我决定使用Gmail SMTP最简单便宜的方式。出于这个原因,我在Gmail上创建了一个名为verify.impressions@gmail.com的帐户。这里是代码。
echo开始邮件发送;
require_once(pear / share / pear / Mail.php);
echo1;
$ from =PersonA`< someone@gmail.com`>; $ to =`< someoneElse@email.com`>; $ subject =激活您的帐户; $ body =嘿;
$ host =ssl://smtp.gmail.com; $ port =465; //也尝试过587 $ username =someone@gmail.com; $ password =password;
echo2;
$ headers = array('From'=> $ from,'To'=> $ to,'Subject'=> $ subject);
echo3;
$ 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);
回显4;
error_reporting(E_ALL);
回显5; (PEAR :: isError($ smtp)){die(Error:。$ smtp-> getMessage());
if }
echo6;
$ mail = $ smtp->发送($ to,$ headers,$ body)或死(Something bad happened);
echo7;
$ b $ if(PEAR :: isError($ mail)){echo($ mail-> getMessage();} else {echo(Message successfully sent!);}
echomail ;
所以基本上这段代码就停在了这一行:
$ mail = $ smtp-> send($ to,%headers,$);
pre>
我尝试了打印错误,但是我现在不知道该怎么做,感谢您的提示和帮助!
解决方案使用这个类: 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 =新的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在这里生产
$ smtp-> html_debug = 1; //同
$ 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,\\\
\\\
它只是让你知道你的SMTP类工作正常。@ nn.nBye.\\\
));
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:
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 verify.impressions@gmail.com for this reason. Here is the code.
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.";
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!!
解决方案Use this class: 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 = '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通过PHP发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!