如何使用SMTP Gmail设置从phpmailer发送邮件

8oomwypt  于 2023-06-20  发布在  PHP
关注(0)|答案(2)|浏览(183)

我用PHP创建了一个联系人表单,并使用PHPmailer类发送邮件,使用SMTP Gmail设置。该表单对于其他邮件ID工作正常,但当我试图更改设置以通过SMTP Gmail发送邮件时,我收到错误消息“SMTP错误:无法连接到SMTP主机。SMTP ->错误:无法连接到服务器:拒绝连接(111)"..下面是我使用的代码…

require 'PHPMailer/class.phpmailer.php';
 $mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'venkateshm2280@gmail.com';
$mail->Password = 'mmuxhssfdrqnsowp'; // Use the App Password you generated
$mail->SMTPSecure = 'tls';
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->AddAddress('venkateshm2280@gmail.com', 'HR');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->AddAttachment($path);
$mail->Subject = 'Application for Programmer Registration';
$mail->Body = $message;
$mail->SMTPDebug = 2;

使用SMTP Gmail发送邮件的设置是否存在任何不匹配。

3ks5zfa0

3ks5zfa01#

@venkat我不能发表评论,这就是为什么我把这个作为答案。你的代码在本地工作吗?如果它是工作,那么正如你提到的,请联系去爸爸客户支持.我曾经面临过类似的问题之一的项目。他们启用一些设置和邮件开始工作。
对于TLS加密,端口号为587,对于SSL加密,端口号为465
为了更好地理解,请查看链接https://kinsta.com/blog/gmail-smtp-server/
我正在使用下面的代码,它正在为我工作。

require("./PHPMailer-master/src/PHPMailer.php");
require("./PHPMailer-master/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "venkateshm2280@gmail.com";
$mail->Password = "mmuxhssfdrqnsowp";
$mail->SetFrom("venkateshm2280@gmail.com");
$mail->Subject = "Application for Programmer Registration";
$mail->Body = $message;
$mail->AddAddress("venkateshm2280@gmail.com", "HR");
$mail->AddAttachment( $path , 'filename' );

$headers = "From: Sender\n";
$headers .= 'Content-Type:text/calendar; Content-Disposition: inline; charset=utf-8;\r\n';
$headers .= "Content-Type: text/plain;charset=\"utf-8\"\r\n"; #EDIT: TYPO

if (!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}
pokxtpni

pokxtpni2#

试试下面的代码。

require_once 'PHPMailer/class-phpmailer.php';
$mail = new PHPMailer();

$options["ssl"] = array(
    "verify_peer" => false,
    "verify_peer_name" => false,
    "allow_self_signed" => true,
);
$mail->SMTPDebug = false;
$mail->isSMTP();
$mail->Host = 'tls://smtp.gmail.com'; // This line was missing.
$mail->SMTPAuth = true;
$mail->Username = 'venkateshm2280@gmail.com';
$mail->Password = 'mmuxhssfdrqnsowp';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->smtpConnect($options); // try with this line.
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->AddAddress('venkateshm2280@gmail.com');
$mail->isHTML(true);
$mail->Subject = 'Application for Programmer Registration';
$mail->Body = $message;
if (!$mail->send()) {
    $finalResponse = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} else {
    $finalResponse = 'success';
}

相关问题