PHPMailer cannot send emails. PHPMailer can send emails.

Source: Internet
Author: User

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... <br> \ r \ n ";
If (! $ Get_host ){
$ Str = "resolution failed (1) <HR> ";
} Elseif ($ Get_host = $ host ){
$ Str = "resolution failed (2): it may be an invalid host name <HR> ";
} Else {
Echo "domain name resolution is $ Get_host... <br> \ 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... <br> \ r \ n ";
$ Fp = pfsockopen ($ host, 25, & $ errno, & $ errstr, 60 );}
Else
Exit ('server does not support Fsockopen function ');

If (! $ Fp ){
Echo "code: $ errno, <br> \ n error cause: $ errstr <HR> ";
} Else {
Echo "SMTP server connection OK! <Br> \ 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 <HR> ';
} Else {
Echo 'server error <HR> ';}
}
}
// 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

<? Php
$ 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?

 

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.