An error occurred while sending emails cyclically by phpmailer.

Source: Internet
Author: User
Tags word wrap
Phpmailer is used to send emails cyclically. the account and password are correct and the test passes.
1. a single message is sent successfully.
2. However, when writing a circular email, only the first sending is successful, and all subsequent sending Fails. check the Log that cocould not authenticate has not passed the verification.
Why?
try {$mail = new PHPMailer();$mail->IsSMTP();   // set mailer to use SMTP$mail->SMTPAuth = true;     // turn on SMTP authentication$mail->SMTPDebug  = 0;$mail->Host = "smtp.126.com";  // specify main and backup server$mail->Port = 25;$mail->Username = "sent@126.com";  // SMTP username$mail->Password = "******"; // SMTP password$mail->From = $mail->Username;$mail->FromName = "myname";$mail->AddAddress("receive@163.com", "toname");$mail->WordWrap = 50;         // set word wrap to 50 characters$mail->IsHTML(true);          // set email format to HTML$mail->Subject = "Here is the subject";$mail->Body    = "This is the HTML message body in bold!";$mail->AltBody = "This is the body in plain text for non-HTML mail clients";if(!$mail->Send()){echo "Mailer Error: ".$mail->ErrorInfo;return false;}else {return true;}                       } catch (phpmailerException $e) {echo "Send mail failed: ".$e->errorMessage();return false;}


Reply to discussion (solution)

If a single success is successful, the program will be okay, because you are using 126 of the email server, and it will certainly not allow you to repeatedly send emails, so you can send another email and let the program sleep for a few seconds.

Generally, emails like qq, 163, and 126 won't allow you to send them continuously. if you want to send them cyclically, you can sleep for several seconds, however, every time an email is sent to sleep for a few seconds, the efficiency is certainly not high. you can also send it to multiple users at the same time, and then sleep for several seconds.

After sleep is added, the test does not take effect. Which of the following is an example of how to send a cyclic test?

for($i=0; $i<10; $i++){SendEmail();sleep(20);}

 CharSet = "UTF-8"; $ mailer-> ContentType = 'text/html '; $ mailer-> IsSMTP (); $ mailer-> SMTPDebug = 0; $ mailer-> SMTPAuth = true; $ mailer-> SMTPSecure = 'SSL '; $ mailer-> Host = 'smtp .163.com'; $ mailer-> Port = '2016 '; $ mailer-> Username = ''; // sender's email address $ mailer-> Password = 'XXX'; // sender's Password $ mailer-> SetFrom ('',''); $ mailer-> AddAddress ($ sendmail); $ mailer-> Subject = $ title; $ mailer-> MsgHTML ($ remark); for ($ I = 0; $ I <10; $ I ++) {$ maile R-> send (); sleep (3) ;}?>

I have tried it.

Sorry, I have not made it clear.
What I want to achieve is that the email is sent only when the user clicks, the email is always the same, but the recipient's email address is different.
(1) when a user clicks to send an email to "zhang san", the program starts to send the email automatically and returns whether the email is sent successfully.
(2) the user clicks "Li Si" again to send the email.
During the test, the sending failure rate is very high. The debugging result is: SMTP-> ERROR: AUTH not accepted from server: 503 Error: already authenticated (because the sending failure is always one, it may have been verified last time)
Find a solution .....

Sorry, I have not made it clear.
What I want to achieve is that the email is sent only when the user clicks, the email is always the same, but the recipient's email address is different.
(1) when a user clicks to send an email to "zhang san", the program starts to send the email automatically and returns whether the email is sent successfully.
(2) the user clicks "Li Si" again to send the email.
During the test, the sending failure rate is very high. The debugging result is: SMTP-> ERROR: AUTH not accepted from server: 503 Error: already authenticated (because the sending failure is always one, it may have been verified last time)
Find a solution .....


Send the corresponding parameter to the sender. the sender is fixed, that is, change the recipient,
Do you have a phpemail class and do not know what to do?
$ Sendmail = ''; // recipient Zhang San, Li Si, Wang 'er, Ma zi $ title = 'I want to mail'; $ remark = 'This is the Mail content '; $ mailer = new PHPMailer (); $ mailer-> CharSet = "UTF-8"; $ mailer-> ContentType = 'text/html '; $ mailer-> IsSMTP (); $ mailer-> SMTPDebug = 0; $ mailer-> SMTPAuth = true; $ mailer-> SMTPSecure = 'SSL '; $ mailer-> Host = 'smtp .163.com '; $ mailer-> Port = '000000'; $ mailer-> Username = ''; // sender's email address (fixed) $ mailer-> Password = 'XXX '; // sender password (fixed) $ mailer-> SetFrom ('',''); recipient Zhang San, Li Si, Wang Er, Ma zi $ mailer-> AddAddress ($ sendmail ); $ mailer-> Subject = $ title; $ mailer-> MsgHTML ($ remark );

What I wrote is to pass in parameters and then send them ....
(1) I write the email as a function, assuming SendEmail ($ address );
(2) assume that the external call is

for($i = 0; $i< 10; $i++){        SendEmail($address);        sleep(3);    }

Only the first one can be sent out .....

How do you define SendEmail? Function SendEmail ($ sendmail, $ title, $ remark) {include "phpemail/phpemail. class. php "; $ sendmail =''; // recipient Zhang San, Li Si, Wang 'er, Ma zi $ title = 'I want to mail'; $ remark = 'This is the Mail content '; $ mailer = new PHPMailer (); $ mailer-> CharSet = "UTF-8"; $ mailer-> ContentType = 'text/html '; $ mailer-> IsSMTP (); $ mailer-> SMTPDebug = 0; $ mailer-> SMTPAuth = true; $ mailer-> SMTPSecure = 'SSL '; $ mailer-> Host = 'smtp .163.com '; $ mailer-> Port = '000000'; $ mailer-> Username = 'XXX'; // sender's email address (fixed) $ mailer-> Password = 'XXX '; // sender password (fixed) $ mailer-> SetFrom ($ sendemail, $ sendemail); recipient Zhang San, Li Si, Wang 'er, Ma zi $ mailer-> AddAddress ($ sendmail ); $ mailer-> Subject = $ title; $ mailer-> MsgHTML ($ remark); for ($ I = 0; $ I <10; $ I ++) {$ mailer-> send (); sleep (3 );}}

This is the case.

function SendEmail($address,$toname,$info){try {$mail = new PHPMailer();$mail->IsSMTP();   // set mailer to use SMTP$mail->SMTPAuth = true;     // turn on SMTP authentication$mail->SMTPDebug  = 0;$mail->Host = "smtp.126.com";  // specify main and backup server$mail->Port = 25;$mail->Username = "sent@126.com";  // SMTP username$mail->Password = "******"; // SMTP password$mail->From = $mail->Username;$mail->FromName = "myname";$mail->AddAddress($address, $toname);$mail->WordWrap = 50;         // set word wrap to 50 characters$mail->IsHTML(true);          // set email format to HTML$mail->Subject = "Here is the subject";$mail->Body    = $info;$mail->AltBody = "This is the body in plain text for non-HTML mail clients";if(!$mail->Send()){echo "Mailer Error: ".$mail->ErrorInfo;return false;}else {return true;}                       } catch (phpmailerException $e) {echo "Send mail failed: ".$e->errorMessage();return false;}}


In another file, it is called cyclically for sending:
For ($ I = 0; $ I <10; $ I ++) {// The email address $ address, recipient name $ toname, sent content $ info SendEmail ($ address, $ toname, $ info); sleep (3 );}


Only the first message is successfully sent, and the subsequent messages cannot be verified.

Do you want to do this?
Send the email to Michael Jacob and send the email 10 times?
Send Li Si, Mail 10 times?
If you want to do this, why don't you directly pass the parameters according to the method I wrote above?

Function SendEmail ($ sendmail, $ title, $ remark) {include "phpemail/phpemail. class. php "; $ sendmail =''; // recipient Zhang San, Li Si, Wang 'er, Ma zi $ title = 'I want to mail'; $ remark = 'This is the Mail content '; $ mailer = new PHPMailer (); $ mailer-> CharSet = "UTF-8"; $ mailer-> ContentType = 'text/html '; $ mailer-> IsSMTP (); $ mailer-> SMTPDebug = 0; $ mailer-> SMTPAuth = true; $ mailer-> SMTPSecure = 'SSL '; $ mailer-> Host = 'smtp .163.com '; $ mailer-> Port = '000000'; $ mailer-> Username = 'XXX'; // sender's email address (fixed) $ mailer-> Password = 'XXX '; // sender password (fixed) $ mailer-> SetFrom ($ sendemail, $ sendemail); recipient Zhang San, Li Si, Wang 'er, Ma zi $ mailer-> AddAddress ($ sendmail ); $ mailer-> Subject = $ title; $ mailer-> MsgHTML ($ remark); for ($ I = 0; $ I <10; $ I ++) {$ mailer-> send (); sleep (3 );}}


Call:
SendEmail ($ sendmail, $ title, $ remark );

No, the SendEmail function sends only once
SendEmail was called outside, and 10 times were called.

That is, send one to Michael and four to Michael Lee ,......, A total of 10 emails were sent

No, the SendEmail function sends only once
SendEmail was called outside, and 10 times were called.

That is, send one to Michael and four to Michael Lee ,......, A total of 10 emails were sent



(1) when a user clicks to send an email to "zhang san", the program starts to send the email automatically and returns whether the email is sent successfully.
(2) the user clicks "Li Si" again to send the email.


According to your needs, if you click Who and then send emails to whom, what do you need to do for 10 cycles?
It is not true to directly call the sendemail (xx, xx, xx) encapsulation method.

Yes. The email is sent to the user who clicked, but the email fails to be sent when three or four people clicked.

To test the cause of sending failure, I wrote a loop call to see what went wrong. The result loop can only be successful for the first time, and all subsequent results fail .....

Do you want to do this?
Send the email to Michael Jacob and send the email 10 times?
Send Li Si, Mail 10 times?
If you want to do this, why don't you directly pass the parameters according to the method I wrote above?

Function SendEmail ($ sendmail, $ title, $ remark) {include "phpemail/phpemail. class. php "; $ sendmail =''; // recipient Zhang San, Li Si, Wang 'er, Ma zi $ title = 'I want to mail'; $ remark = 'This is the Mail content '; $ mailer = new PHPMailer (); $ mailer-> CharSet = "UTF-8"; $ mailer-> ContentType = 'text/html '; $ mailer-> IsSMTP (); $ mailer-> SMTPDebug = 0; $ mailer-> SMTPAuth = true; $ mailer-> SMTPSecure = 'SSL '; $ mailer-> Host = 'smtp .163.com '; $ mailer-> Port = '000000'; $ mailer-> Username = 'XXX'; // sender's email address (fixed) $ mailer-> Password = 'XXX '; // sender password (fixed) $ mailer-> SetFrom ($ sendemail, $ sendemail); recipient Zhang San, Li Si, Wang 'er, Ma zi $ mailer-> AddAddress ($ sendmail ); $ mailer-> Subject = $ title; $ mailer-> MsgHTML ($ remark); for ($ I = 0; $ I <10; $ I ++) {$ mailer-> send (); sleep (3 );}}


Call:
SendEmail ($ sendmail, $ title, $ remark );



Layout the interface. As I said, test it.

(1) I used your code to comment out $ mailer-> SMTPSecure = 'SSL '. this sentence can connect to the mailbox server successfully.
(2) the same problem, result:
Click Send to Lee San and return success;
Click Zhang San to return success;
......
Fourth failure
Fifth failure
......

On average, two failed messages are sent for 5 Messages. The reason is the same as what I did before.

Generally, handle possible errors.

If (! $ Mailer-> Send () {echo "failed to Send the email.

"; Echo" error cause: ". $ mail-> ErrorInfo; exit; // What if the condition is not exited but re-entered ?}

Be patient when debugging code!
Someone else can only give you one idea, right? you have to verify it.

(1) I used your code to comment out $ mailer-> SMTPSecure = 'SSL '. this sentence can connect to the mailbox server successfully.
(2) the same problem, result:
Click Send to Lee San and return success;
Click Zhang San to return success;
......
Fourth failure
Fifth failure
......

On average, two failed messages are sent for 5 Messages. The reason is the same as what I did before.


Print the error message and analyze it by yourself.

Close once after each sending.
View the phpmailer attributes.
Use the Smtpclose (); method.

Close once after each sending.
View the phpmailer attributes.
Use the Smtpclose (); method.



OK


Close once after each sending.
View the phpmailer attributes.
Use the Smtpclose (); method.



OK


There may also be repeated email classes.
Change the include statement in the cyclic function SendEmail to include_once.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.