Phpmailer Why can't I send a message

Source: Internet
Author: User
Tags php send mail php tutorial response code
Phpmailer is a useful class for sending mail to PHP. It supports the use of SMTP servers to send mail, while supporting Sendmail, QMail, Postfix, Imail, Exchange, Mercury, Courier and other mail servers. SMTP server also supports authentication, multi-SMTP send (but not very clear what is used). Mail send can include multiple to, CC, BCC and reply-to, support text and HTML two kinds of message format, can be wrapped automatically, support various formats of attachments and pictures, Custom message first-class basic mail functionality.
Because PHP contains only a mail function, so phpmailer is a big enhancement, I believe it can meet the needs of many people, hehe. It consists of two classes of files: class.smtp.php for implementing the class.phpmailer.php and SMTP implementations of the Send mail feature. Then there are files that can implement a variety of error outputs, as well as very detailed documentation.

Phpmailer cannot connect to an SMTP server, and modifying SMTP case does not matter

(2011-10-22 12:17:35)

Reproduced

Php

Phpmailer

Zatan

Category: Default categories

Phpmailer Unable to send message, prompting error error:could not connect to SMTP host

Blog before the two articles, "Phpmailer:: Cannot connect to the SMTP server" "Phpmailer can not connect to the SMTP server two common reasons"
One for the reprint, one for notes, the result fraught, not everyone can solve the problem.
I was anxious to have a friend write for help. Although later resolved, but I still no avail, calm down and looked at

Phpmailer cannot connect to the SMTP server, why exactly? First check with code:

function Get_host ($host) {//Resolve domain name
$Get _host=gethostbyname ($host);
echo "Trying to connect $host ...
\ r \ n ";
if (! $Get _host) {
$str = "Parse failed (1)";
}elseif ($Get _host== $host) {
$str = "Parse failed (2): May be an invalid hostname";
}else{
echo "domain name resolved to $Get _host ...
\ r \ n ";
Open_host ($host);}
Echo $str;
}

Function Open_host ($host) {//Connection host

if (function_exists (' Fsockopen ')) {
$fp = Fsockopen ($host,25,& $errno,& $errstr, 60);
ElseIf (function_exists (' Pfsockopen ')) {
echo "Server does not support Fsockopen, try Pfsockopen function ...
\ r \ n ";
$fp = Pfsockopen ($host,25,& $errno,& $errstr, 60); }
Else
Exit (' server does not support fsockopen functions ');

if (! $fp) {
echo "Code: $errno,
\ n Error reason: $errstr ";
}else{
echo "SMTP Server Connection ok!
\ r \ n ";
Fwrite ($FP, "");
$out 0= fgets ($fp, 128);
#echo $out 0;
if (strncmp ($out 0, "220", 3) ==0) {//judgment three-bit character content
echo ' over SMTP server response OK ';
}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");

Dispatching scripts
# $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 send mail class, processing errors are focused on and the SMTP server session during the problem, such as authentication is not correct, the recipient is empty error prompt, but for the connection to the SMTP process error prompt to the "Could not connect to SMTP Host "A word, leading to a lot of problems can not be solved, more ridiculous is to lead to some useful but can not speak the way of truth spread in the world, visible, everything has a destiny."

OK, don't say a word of mouth.
To figure out why the could not connect to SMTP host, naturally understand the steps to connect the service
A complete and valid SMTP sending process should include resolving domain names, connecting to an SMTP server, verifying identities, determining recipient and letter content, sending

The PHP code above is to separate these steps, find out why, and then look for ways. The echo results are as follows:

1. Parsing failure (2): May be an invalid hostname
Indicates that the domain name cannot be resolved. There may be a DNS-level issue. Contact an administrator or change a service provider

2, the server does not support Fsockopen, try to Pfsockopen function
If the server is successfully connected with the Pfsockopen function, modify the class.smtp.php $this->smtp_conn = Fsockopen (For $this->smtp_conn = Pfsockopen (. Make the Phpmailer return to normal use

3. Server-side Error
Successful connection to remote host, but no SMTP protocol is installed to send 220 response code, indicating that there may be a problem with the SMTP server

4. SMTP server response is normal

Well, whether it's the Fsockopen function or the Pfsockopen function, it's already connected to the remote SMTP server. If it is not possible to use Phpmailer letter, I strongly recommend you to change an account to try again

5, other error, such as this

Warning:fsockopen (): Unable to connect to smtp163.com:25
You have every reason to believe it's a firewall! In this case, if you cannot contact the administrator to change the firewall rules, you can try the "Phpmailer:: Cannot connect to the SMTP server" method,
Search
function Issmtp () {
$this->mailer = ' smtp ';
}

Change to:
function Issmtp () {
$this->mailer = ' SMTP ';
}

As my headline says, "Phpmailer cannot connect to an SMTP server, and it does not matter if you modify SMTP casing." Of course, I can not be bad fun to tease you, but sometimes really effective, cure success rate of how much depends on your character

To analyze why.
This code is probably about class.phpmailer.php286 line. This function is called first when using the Phpmailer class to declare the way mail is sent

Trace This->mailer to class.smtp.php 400 rows or so

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, the above conditions are not satisfied Phpmailer will execute $result = $this->mailsend ($header, $body); This sentence

Then follow the MailSend () function around the class.phpmailer.php 460 line:

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 (', ', $to);

$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 letter from the PHP built-in mail function!)


Mail sending instance from W3school

$to = "somebody@example.com"; Change it to your email address.
$subject = "My subject";
$txt = "Hello world!";
$headers = "from:dongfangtianyu@qq.com". "\ r \ n".
Mail ($to, $subject, $txt, $headers);
?>


If you run this script on your server to receive mail, you can use the method of modifying the SMTP case. But, after all, not very useful

The above describes why Phpmailer can not send mail, including Phpmailer send the content of the message, I hope the PHP tutorial interested in a friend helpful.

  • 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.