Why can't PHPMailer send emails?
PHPMailer is a useful PHP class for sending emails. It supports sending mail using smtp server, and also supports Sendmail, qmail, Postfix, Imail, Exchange, Mercury, Courier and other mail servers. The SMTP server also supports verification and multiple SMTP sending (but I’m not sure what it’s used for). Email sending can include multiple TO, CC, BCC and REPLY-TO, supports both text and HTML email formats, can automatically wrap lines, and supports Attachments and pictures in various formats, custom email headers and other basic email functions.
Since PHP only contains one mail function, PHPMailer is a great enhancement to it, and I believe it can meet the needs of many people, haha. It mainly includes two class files: class.phpmailer.php for implementing the mail sending function and class.smtp.php for smtp implementation. Then there are files that can achieve various error outputs, as well as very detailed documentation.
PHPMailer cannot connect to the SMTP server, and it has nothing to do with changing the SMTP case.
(2011-10-22 12:17:35)
Reprint▼
Label: php phpmailer Miscellaneous talk |
Category: Default category |
PHPmailer cannot send emails, prompting error Error: Could not connect to SMTP host
There are two previous articles on the blog, "PHPMailer::Cannot connect to SMTP server" "Two common reasons why PHPMailer cannot connect to SMTP server"
One is for reprinting, the other is for taking notes, but it ends up misleading people. Not everyone can solve the problem.
Friends wrote to me asking for help, and I was also anxious. Although it was solved later, I still couldn’t figure it out, so I calmed down and looked at it again
PHPMailer cannot connect to the SMTP server. Why? First check it with the code:
function Get_host($host){ //Resolve domain name
$Get_host=gethostbyname($host);
echo "Trying to connect $host ...
rn ";
if(!$Get_host){
$str= "Parse failed (1)
";
}elseif($Get_host==$host){
$str= "Parse failed (2): Possibly an invalid hostname
";
}else{
echo "The domain name is resolved to $Get_host...
rn";
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...
rn";
$fp = pfsockopen($host,25,&$errno,&$errstr,60); }
else
exit('The server does not support the Fsockopen function');
if(!$fp){
echo "Code name: $errno,
nError reason: $errstr
";
}else{
echo "SMTP server connection ok!
rn";
fwrite($fp, "");
$out0= fgets($fp, 128);
#echo $out0;
if (strncmp($out0,"220",3)==0){ // Determine the three-character content
echo '220 SMTP server response is 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");
//Transport 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 great PHP class for sending mail. Error handling focuses on problems during the session with the SMTP server, such as error prompts for incorrect authentication and empty recipients. However, error prompts for the process of connecting to SMTP start with "Could not connect to SMTP host" In a word, it has led to many problems that have not been solved. What is even more ridiculous is that it has led to the spread of some useful but unreasonable methods in the world. It can be seen that everything has a certain fate.
Okay, no more talking nonsense.
If you want to understand the reason why Could not connect to SMTP host, you must understand the steps to connect to the service
A complete and effective SMTP sending process should include: resolving the domain name, connecting to the SMTP server, verifying the identity, determining the recipient and the content of the letter, and sending
The above PHP code is to separate these steps, find out the reason, and then find a method. The echoed results may be as follows:
1. Parsing failed (2): It may be an invalid host name
The domain name cannot be resolved. Might be a DNS level issue. Contact the administrator or change service provider
2. The server does not support Fsockopen, try the pFsockopen function
If the connection to the server using the pfsockopen function is successful, modify $this->smtp_conn = fsockopen( in class.smtp.php to $this->smtp_conn = pfsockopen(. Return PHPmailer to normal use
3. Server-side error
Successfully established a connection with the remote host, but the other party did not install the SMTP protocol and sent a 220 response code, indicating that there may be a problem with the SMTP server
4. 220 SMTP server response is normal
Well, whether it is the fsockopen function or the pfsockopen function, it has been connected to the remote SMTP server normally. If you are unable to send emails using PHPmailer, I strongly suggest you change your account and try again
5. Other error reports, such as this
Warning: fsockopen(): unable to connect to smtp163.com:25
You have absolutely reason to believe that the firewall is responsible! In this case, if you cannot contact the administrator to change the firewall rules, you can try the method in "PHPMailer::Cannot connect to SMTP server",
Search
function IsSMTP() {
$this->Mailer = 'smtp';
}
Change to:
function IsSMTP() {
$this->Mailer = 'SMTP';
}
As my title says, "PHPMailer cannot connect to the SMTP server, and it has nothing to do with changing the SMTP case." Of course, I can't play tricks on you in a bad way, but sometimes it really works. The success rate of cure depends on your character
Let’s analyze the reasons.
This code is probably around line 286 of class.phpmailer.php. This function must be called first when using the PHPmailer class to declare the method of sending mail
Trace this->Mailer to about line 400 of class.smtp.php
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 of all, smtp is definitely not equal to SMTP! I have forgotten this basic principle.
Therefore, if the above conditions are not met, PHPmailer will execute $result = $this->MailSend($header, $body);this sentence
Let’s trace the MailSend() function at around 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(',', $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;
}
注意$rt = @mail( 这是用PHP内置的mail函数发信啊!
来自W3School的mail发信实例
$to = "somebody@example.com"; //这里改成你的邮箱地址
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: dongfangtianyu@qq.com" . "rn" .
mail($to,$subject,$txt,$headers);
?>
如果在你的服务器上运行这脚本能够收到邮件,那么你完全可以用修改SMTP大小写的方法。不过,毕竟不大好用
以上就介绍了PHPMailer为什么不能发送邮件,包括了PHPMailer发送邮件方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
