在使用PHpmailer通过我的网站发送邮件时获得SMTP错误


Getting SMTP error while using PHpmailer to send mail through my website

我试图通过PHP Mailer类从我的网站发送电子邮件。代码如下,但当我试图发送电子邮件时,我得到这两个错误:

SMTP错误:无法连接到SMTP主机。消息无法发送。

Mailer Error: SMTP Error: Could not connect to SMTP host.

怎么了?请帮忙吗?

<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_myemail@host.com";  // SMTP username
$mail->Password = "password"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("myemail@host.com", "namit pathak");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo "Message has been sent";
?>

您需要在这一行包含您的电子邮件的用户名和密码,目前您使用的是原始虚拟设置:

$mail->Username = "send_from_myemail@host.com";  // SMTP username
$mail->Password = "password";

当您必须配置本地SMTP服务器时,您只能将主机设置为本地主机

$mail->Host = "localhost";

如果你有谷歌账号,那就试试吧

$mail->Host = "smtp.google.com";