PHPMailer cannot send emails. PHPMailer sends emails _ PHP Tutorial

Source: Internet
Author: User
PHPMailer cannot send emails. PHPMailer can send emails. PHPMailer cannot send emails, and PHPMailer cannot connect to the SMTP server. it has nothing to do with modifying the SMTP case (2011-10-22:17:35). reprinted the authorization label: phpphpphpmailer PHPMailer cannot send emails. PHPMailer can send emails.

PHPMailerYou cannot connect to the SMTP server. it is irrelevant to modifying SMTP case sensitivity.

(12:17:35)

Reprinted token

Php

Phpmailer

Miscellaneous

Category: default category

PHPmailer cannot send emails, and the Error "Error: cocould not connect to SMTP host" is displayed.

There were two articles in the blog: PHPMailer: Cannot connect to the SMTP server. PHPMailer cannot connect to the SMTP server.
One is reprinted, and the other is NOTES. as a result, it is wrong, and not everyone can solve the problem.
A friend sent me a letter for help. I am also in a hurry. Although it was solved later, I still had to try again.

PHPMailer cannot connect to the SMTP server. why? Check with the code:

Function Get_host ($ host) {// resolve the domain name
$ Get_host = gethostbyname ($ host );
Echo "try to connect $ host...
\ R \ n ";
If (! $ Get_host ){
$ Str = "resolution failed (1 )";
} Elseif ($ Get_host = $ host ){
$ Str = "resolution failed (2): It may be an invalid host name ";
} Else {
Echo "domain name resolved to $ Get_host...
\ R \ n ";
Open_host ($ host );}
Echo $ str;
}

Function Open_host ($ host) {// connect to the host

If (function_exists ('fsockopen ')){
$ Fp = fsockopen ($ host, 25, & $ errno, & $ errstr, 60 );
Elseif (function_exists ('pfsockopen ')){
Echo "the server does not support Fsockopen. try the pFsockopen function...
\ R \ n ";
$ Fp = pfsockopen ($ host, 25, & $ errno, & $ errstr, 60 );}
Else
Exit ('server does not support Fsockopen function ');

If (! $ Fp ){
Echo "Code: $ errno,
\ N error cause: $ errstr ";
} Else {
Echo "SMTP server connection OK!
\ R \ n ";
Fwrite ($ fp ,"");
$ Out0 = fgets ($ fp, 128 );
# Echo $ out0;
If (strncmp ($ out0, "220", 3) = 0) {// Determine the three-character content
Echo '220 SMTP server response normal ';
} Else {
Echo 'server-side error ';}
}
}
// SMTP server address
$ Site = array ("smtp.163.com", "smtp.sina.cn", "smtp.sina.com", "smtp.qqq.com", "smtp.126.com ");

// Dispatch script
# $ Host = "smtp.163.com ";
# Echo Get_host ($ host );


For ($ I = 0; $ I <= 4; $ I ++)
{
$ Host = $ site [$ I];
Echo Get_host ($ host );
}



PHPmailer is a very good PHP mail class. to handle errors, it focuses on problems in the session with the SMTP server, such as incorrect authentication and error messages with a blank recipient, however, the error message "cocould not connect to smtp host" indicates that many problems cannot be solved, what's even more ridiculous is that some useful but unreasonable methods are passed on to the world. it can be seen that everything in the dark has a fixed number.

Okay, let's stop talking.
To find out why cocould not connect to SMTP host, you must understand the procedure for connecting to the service.
A complete and valid SMTP sending process should include: resolving domain names, connecting to the SMTP server, verifying identity, determining recipient and email content, and sending

The above section of PHP code is to separate these steps, find out the reasons, and then find a method. The echo results are as follows:

1Failed to parse (2): It may be an invalid host name.
The domain name cannot be resolved. It may be a DNS-level issue. Contact the administrator or change the service provider

2The server does not support Fsockopen. try the pFsockopen function.
If the pfsockopen function is successfully used to connect to the server, modify $ this-> smtp_conn = fsockopen ($ this-> smtp_conn = pfsockopen (for class. smtp. php (. Restore PHPmailer to normal use

3Server errors
Successfully established a connection with the remote host, but the other party did not install the SMTP protocol to send a 220 response code, which indicates that the SMTP server may be faulty.

4220 The SMTP server responds normally.

Okay, whether it's the fsockopen function or the pfsockopen function, it's already connected to a remote SMTP server. If you cannot use PHPmailer to send emails, I strongly recommend that you try again with another account.

5Other errors, such

Warning: fsockopen (): unable to connect to smtp163.com: 25
You absolutely have a reason to believe that the firewall is a ghost! In this case, if you cannot contact the administrator to change the firewall rules, you can try the method in PHPMailer: Cannot connect to the SMTP server,
Search
Function IsSMTP (){
$ This-> Mailer = 'smtp ';
}

Changed:
Function IsSMTP (){
$ This-> Mailer = 'smtp ';
}

As my title says, "PHPMailer cannot connect to the SMTP server, and it has nothing to do with modifying SMTP case ". Of course, it is impossible for me to treat you with evil taste, but sometimes it is really effective. the success rate of healing depends on your character.

Let's analyze the cause.
The code is about 6 lines in class. phpmailer. php286. This function must be called first when using the PHPmailer class to declare the mail sending method.

Tracking this-> Mailer to class. smtp. php: about 400 lines

Switch ($ this-> Mailer ){
Case 'sendmail ':
$ Result = $ this-> SendmailSend ($ header, $ body );
Break;
Case 'smtp ':
$ Result = $ this-> SmtpSend ($ header, $ body );
Break;
Case 'mail ':
$ Result = $ this-> MailSend ($ header, $ body );
Break;
Default:
$ Result = $ this-> MailSend ($ header, $ body );
Break;

First, smtp is definitely not equal to SMTP! I will forget this basic principle.
Therefore, if none of the above conditions meet PHPmailer$ Result = $ this-> MailSend ($ header, $ body );This sentence

The MailSend () function is followed in line 460 of class. phpmailer. php:

Function MailSend ($ header, $ body ){
$ To = '';
For ($ I = 0; $ I <count ($ this-> to); $ I ++ ){
If ($ I! = 0) {$ to. = ',';}
$ To. = $ this-> AddrFormat ($ this-> to [$ I]);
}

$ ToArr = split (',', $ );

$ Params = sprintf ("-oi-f % s", $ this-> Sender );
If ($ this-> Sender! = ''& Strlen (ini_get ('safe _ mode') <1 ){
$ Old_from = ini_get ('sendmail _ from ');
Ini_set ('sendmail _ from', $ this-> Sender );
If ($ this-> SingleTo === true & count ($ toArr)> 1 ){
Foreach ($ toArr as $ key => $ val ){
$ Rt = @ mail ($ val, $ this-> EncodeHeader ($ this-> SecureHeader ($ this-> Subject), $ body, $ header, $ params );
}
} Else {
$ Rt = @ mail ($ to, $ this-> EncodeHeader ($ this-> SecureHeader ($ this-> Subject), $ body, $ header, $ params );
}
} Else {
If ($ this-> SingleTo === true & count ($ toArr)> 1 ){
Foreach ($ toArr as $ key => $ val ){
$ Rt = @ mail ($ val, $ this-> EncodeHeader ($ this-> SecureHeader ($ this-> Subject), $ body, $ header, $ params );
}
} Else {
$ Rt = @ mail ($ to, $ this-> EncodeHeader ($ this-> SecureHeader ($ this-> Subject), $ body, $ header );
}
}

If (isset ($ old_from )){
Ini_set ('sendmail _ from', $ old_from );
}

If (! $ Rt ){
$ This-> SetError ($ this-> Lang ('instantiate '));
Return false;
}

Return true;
}


Note:$ Rt = @ mail (This is a mail function built in PHP!


Mail sending instance from W3School

$ To = "somebody@example.com"; // Here is your email address
$ Subject = "My subject ";
$ Txt = "Hello world! ";
$ Headers = "From: dongfangtianyu@qq.com". "\ r \ n ".
Mail ($ to, $ subject, $ txt, $ headers );
?>


If you can run this script on your server to receive an email, you can modify the SMTP case. However, it is not easy to use

.


If you want to use the mail function to send emails, you need to modify and set php. ini. that is to say, your service provider is not the best choice.
If everything related to mail () has been set on the server, PHPmailer can successfully send emails by using mail. No longer dependent on the fsockopen function

This is why, when the firewall is disabled, you can use PHPmailer to send emails by modifying the smtp case, because the e-mail is sent on behalf of the local smtp server.
Dear friend, do you understand?

PHPMailer cannot connect to the SMTP server. it has nothing to do with modifying the SMTP case (12:17:35). reprinted the authorization label: php phpmailer...

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.