I'm trying to send email using my gmail account but i am getting an error
我正在尝试使用我的Gmail帐户发送电子邮件,但我收到了错误消息
"SMTP Error: Could not connect to SMTP host".
“SMTP错误:无法连接到SMTP主机”。
I have tried port 587,465 25 but still its not working.
我尝试过端口587,465 25,但仍然无法正常工作。
<?php
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->SMTPSecure = "tls";
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "rhlsngh302@gmail.com"; // Your full Gmail address
$mail->Password = "*********"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("rhlsngh302@gmail.com", "RahulName"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" />
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" />
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
How can I solve this problem?
我怎么解决这个问题?
3 个解决方案
#1
First of all, while initiating PHPMailer send in the parameter true
like,
首先,在启动PHPMailer时发送参数为true,如
$mailer = new PHPMailer(true);
This will help you to catch and handle exceptions.
这将帮助您捕获和处理异常。
Secondly, try this
其次,试试这个
$mailer->SMTPSecure = 'ssl'; // instead of tls
#2
TLS on port 465 will not work - that port expects SSL. You should change it to port 587. You should not change to ssl for encryption as it's been deprecated since 1998.
端口465上的TLS将无法工作 - 该端口需要SSL。您应该将其更改为端口587.您不应该更改为ssl进行加密,因为它自1998年以来已被弃用。
It would also help if you read the docs and base your code on an up-to-date example.
如果您阅读文档并将代码基于最新的示例,它也会有所帮助。
#3
Try:
$PHPMailer->SMTPOptions = array (
'ssl' => array (
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
works for me!
适合我!
#1
First of all, while initiating PHPMailer send in the parameter true
like,
首先,在启动PHPMailer时发送参数为true,如
$mailer = new PHPMailer(true);
This will help you to catch and handle exceptions.
这将帮助您捕获和处理异常。
Secondly, try this
其次,试试这个
$mailer->SMTPSecure = 'ssl'; // instead of tls
#2
TLS on port 465 will not work - that port expects SSL. You should change it to port 587. You should not change to ssl for encryption as it's been deprecated since 1998.
端口465上的TLS将无法工作 - 该端口需要SSL。您应该将其更改为端口587.您不应该更改为ssl进行加密,因为它自1998年以来已被弃用。
It would also help if you read the docs and base your code on an up-to-date example.
如果您阅读文档并将代码基于最新的示例,它也会有所帮助。
#3
Try:
$PHPMailer->SMTPOptions = array (
'ssl' => array (
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
works for me!
适合我!