2015-07-19 115 views
0

我想通过PHP中的PHP Mailer类从我的网站发送电子邮件。 的代码如下,但是当我试图发送电子邮件,我得到这两个错误:使用PHpmailer通过我的网站发送邮件时出现SMTP错误

SMTP Error: Could not connect to SMTP host. Message could not be sent.

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: [email protected] 
// pass: password 
$mail->Username = "[email protected]"; // 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("[email protected]", "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"; 
?> 

回答

0

您需要包括你的电子邮件的在这条线上的用户名和密码,您当前使用的原始虚拟设置:

$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = "password"; 
0

你只把主机为localhost你必须配置一个时间你本地SMTP服务器

$mail->Host = "localhost"; 

,如果您有谷歌账号的手段尝试

$mail->Host = "smtp.google.com"; 
相关问题