1

我曾经phpgmailer发送电子邮件,它工作顺利。今天我测试了我的项目,但它现在无法正常工作。

   <?php                  
    require_once('class.phpgmailer.php');
    $mail = new PHPGMailer();
    $mail->Username = 'username@gmail.com';
    $mail->Password = '********';
    $mail->From = 'username@gmail.com';
    $mail->FromName = "<blah>";
    $mail->Subject = 'something';
    $mail->AddAddress('xyz@gmail.com');


    $mail->Body =  "Hello Sir"."\n"."     
    Your Password is : ."."";
    $mail->Host = 'smtp.gmail.com'; 
    $mail->Port = 465;
    $mail->Send();

    if(!$mail->Send())
            {

       echo 'Message could not be sent.' ;
       echo 'Mailer Error: ' . $mail->ErrorInfo;

       exit;
            }
       echo 'Message has been sent';
4

3 回答 3

1

我建议您下载PHPMailer并尝试以下代码:

    require("phpmailer/class.phpmailer.php");

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host     = "ssl://smtp.gmail.com";
    $mail->Port     = 465;
    $mail->Username = "from@gmail.com";
    $mail->Password = "****";
    $mail->FromName = "Sender name";
    $mail->Subject  = "test";
    $mail->Body     = "Test body";
    $mail->AddAddress('sender@mail.com');
    if(!$mail->Send()){
        echo "Mailer Error: " . $mail->ErrorInfo;
    }else{
        echo "Message has been sent";
    }
于 2012-09-16T14:12:49.627 回答
0

请确保:

  • $mail->Username等于$mail->From,并且是正确的
  • $mail->Password是正确的
  • $mail->FromName不包含“<”和“>”字符;试试看嘛$mail->FromName = 'Test';
于 2012-09-16T14:34:05.640 回答
0

@goose 是对的,删除第一个$mail->Send()并将其留在 if 语句中。如果From地址与用户名电子邮件相同,那么您也不需要它,因为它会从您的 gmail 帐户中获取。

试试看它是否有效。

编辑:尝试添加以下内容;

    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = 'email@gmail.com';  
    $mail->Password = 'password';     

试试看,如果有错误,那么它应该会给你更多的细节。

EDIT2:如果这不起作用,那么我建议尝试 PHPMailer 而不是 PHPGmailer - 并按照此处的教程进行操作:http ://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp -with-phpmailer-and-gmail/

于 2012-09-16T13:56:00.457 回答