简体   繁体   中英

phpgmailer stopped working suddenly

I used phpgmailer to send emails and it was working smoothly. Today I tested my project and it's not working now.

   <?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';

I suggest you download PHPMailer and try this code:

    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";
    }

@goose is right, remove the first $mail->Send() and leave the one in the if statement. If the From address is the same as the username email, then you don't need that either as it will take it from your gmail account.

Try that and see if it works.

EDIT: try adding the following;

    $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';     

Try that and if there are errors, then it should give you more detail.

EDIT2: If that doesn't work then I suggest trying PHPMailer instead of PHPGmailer - and follow the tutorial here: http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

Please, make sure:

  • $mail->Username is equal to $mail->From , and is correct
  • $mail->Password is correct
  • $mail->FromName does not contain "<" and ">" characters; just try $mail->FromName = 'Test';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM