简体   繁体   English

需要帮助,使用Gmail SMTP在PHP中发送电子邮件

[英]Need help sending email in PHP using Gmail SMTP

So I'm trying to follow the instructions here 因此,我尝试按照此处的说明进行操作

http://www.9lessons.info/2011/07/send-mail-using-smtp-and-php.html http://www.9lessons.info/2011/07/send-mail-using-smtp-and-php.html

to use PHP to send out an email using my gmail account. 使用PHP使用我的gmail帐户发送电子邮件。 But when I try to send a test email I get in the web browser this message: Error: could not connect to the host "smtp.gmail.com" 但是,当我尝试发送测试电子邮件时,我在Web浏览器中收到以下消息:错误:无法连接到主机“ smtp.gmail.com”

Here is are my two files (with my personal data edited) 这是我的两个文件(编辑了我的个人数据)

Thanks for any help 谢谢你的帮助

<?php
print "1";
require("smtp.php");
require("sasl.php"); //SASL authentication
$from="support@yourwebsite.com"; 
$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com"; // Or IP address
$smtp->host_port=465;
$smtp->ssl=0;
$smtp->start_tls=1;
$smtp->localhost="localhost";
$smtp->direct_delivery=0;
$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host="";
$smtp->user="john@gmail.com"; // SMTP Username
$smtp->realm="";
$smtp->password="House22"; // SMTP Password
$smtp->workstation="";
$smtp->authentication_mechanism="";
print "3";
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: $subject",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"$message"))
{
print "Message sent to $to OK."; 
}
else
print "Cound not seend the message to $to.\nError: ".$smtp->error;
print "4";
?>

==============================

<?php
$to="ralph@website.com";
$fn="First Name";
$ln="Last Name";
$name=$fn.' '.$ln;
$from="test@grove.net";
$subject = "Welcome to Website";
$message = "Dear $name, 
Your Welcome Message.
Thanks
www.website.com
";
include('smtpwork.php');
?>

as per Gmail policy, You cannot use plain password (house22) , you have to use encrypted password(Gh45515HGGujKkkHk5554) , for that you need two way authentication should be enable on your gmail account. 根据Gmail政策,您不能使用plain password (house22) ,而必须使用encrypted password(Gh45515HGGujKkkHk5554) ,因为需要在Gmail帐户上启用两种身份验证。 and after that, create Application Specific Password and then use that password for your SMTP, I hope that will work for you. 之后,创建“应用程序专用密码”,然后将该密码用于SMTP,希望对您有用。 here is the link1 Link2 这是link1 Link2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM