2011-10-31 152 views
0

我正在使用此PHP代码通过Gmail帐户发送电子邮件。但它显示smtp错误。 我用Google搜索了它。但没有找到任何具体的答案。谁会请帮忙找到解决方案?phpmailer SMTP错误

我已经在我的网络服务器托管于该代码

错误: “SMTP错误:无法连接到SMTP主机邮件发送失败 邮件错误:SMTP错误:无法连接到SMTP host.SMTP错误:无法连接到SMTP主机。“

代码:

<?php 
require("class.phpmailer.php"); 

$email = $_POST['email_address']; 

$username = '[email protected]'; //your gmail address 
$password = 'password'; //password 


send_mail_to_subscriber(); 


function send_mail_to_subscriber() 
{ 

    global $username,$password,$email; 
    $mailer = new PHPMailer(); 
    $mailer->IsSMTP(); 
    $mailer->Host = 'ssl://smtp.gmail.com:465'; 
    $mailer->SMTPAuth = TRUE; 

    $mailer->Username = $username; 
    $mailer->Password = $password; 
    $mailer->From = $username; 
    $mailer->FromName = $username; 

    $mailer->Body = 'this is a message'; 
    $mailer->Subject = 'this is a subject'; 
    $mailer->AddAddress($email); 
    if(!$mailer->Send()) 
    { 
     echo "Message was not sent<br/ >"; 
     echo "Mailer Error: " . $mailer->ErrorInfo; 
    } 
    else 
    { 
     echo "Download link has been sent to your email address"; 
    } 
} 


?> 

回答

2

更改您的主机,并添加以下内容:

$mailer->SMTPSecure = 'ssl'; 
$mailer->Host = 'smtp.gmail.com'; 
$mailer->Port = 465; 
+0

我编辑过。仍然没有结果。你可以查看这个http://maruf10.tk/mail/ – qmaruf

+0

你的TCP是否打开为465? – djdy

+0

其实我并不确定。有什么方法可以检查? – qmaruf

0
$mail->SMTPDebug = 3;  // Enable verbose debug output 
    $mail->Mailer  = "smtp"; 
    $mail->setCharset = "UTF-8"; 
    $mail->isSMTP(); 
    $mail->SMTPDebug = 0;  // Set mailer to use SMTP 
    $mail->Host   = 'smtp.gmail.com'; //Specify main and backup SMTPservers 
    $mail->SMTPAuth  = true;   // Enable SMTP authentication 
    $mail->Username  = 'gmail user name';  // SMTP username 
    $mail->Password  = 'gmail password';     // SMTP password 
    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
    $mail->Port   = 'Your Outgoing server port number';// TCP port to connect to 
    $mail->From   = 'from address '; 
    $mail->FromName  = ' from name'; 

来识别问题,更改值$ MAIL-> SMTPDebug = 1,2.3.4 。

相关问题