Home Backend Development PHP Tutorial The idea of ​​​​implementing email sending in php

The idea of ​​​​implementing email sending in php

Jun 06, 2018 pm 04:51 PM
php send email

This article mainly introduces the idea of ​​​​implementing email sending in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The main content of this article is to use PHP to send emails, which is summarized in the following two methods:

1. Use PHP’s built-in mail() function

<?php 
$to = "test@163.com"; //收件人 
$subject = "Test"; //主题 
$message = "This is a test mail!"; //正文 
mail($to,$subject,$message);
Copy after login

The result is an error, as follows:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP " and "smtp_port" setting in php.ini or use ini_set() inD:/www/Zend/email/email.php on line 10

Analysis of reasons: You need a local SMTP server, so you changed the code:

<?php 
$to = "test@163.com";//收件人 
$subject = "Test";//邮件主题 
$message = "This is a test mail!";//邮件正文 
ini_set(&#39;SMTP&#39;,&#39;smtp.163.com&#39;);//发件SMTP服务器 
ini_set(&#39;smtp_port&#39;,25);//发件SMTP服务器端口 
ini_set(&#39;sendmail_from&#39;,"admin@163.com");//发件人邮箱 
mail($to,$subject,$message);
Copy after login

The result is still wrong:

Warning: mail() [function.mail]: SMTP server response : 553 authentication is required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S2 1301220947 inD:/www/Zend/email/email.php on line 9

Analysis reason: Verification information is required, how to write verification information? ? Where to configure it? After referring to some technical articles with these questions, I came to the conclusion that using the mail() function to send emails requires a mail server that can send letters without SMTP authentication. But today's SMTP mail servers basically require authentication, so if you want to use it to send emails, you can only set up a local SMTP server that does not require authentication. Building method: Just use the IIS that comes with Windows, or download other SMTP server software from the Internet.

Conclusion: To use the mail() function to send emails, you must have an SMTP server that does not require authentication. In this case, the configuration work will be a little more, but it will be easier to use, just a few lines of code.

2. Use the mail class that encapsulates the SMTP protocol

This method is relatively common, especially for the majority of students who do not have a server and purchase a virtual host online. The first method is not practical, so you should use the SMTP protocol to send emails yourself.

But to complete this work, you need to have a certain understanding of the SMTP protocol. Students who like to do everything by themselves can write one by themselves. Students who like to use it as a tool can download it from the Internet. There are many .

However, I recommend using the Mail class in the PEAR extension. It has powerful functions: can support emails in plain text and HTML formats; Encoding can be set for fields, and correct configuration will not cause Chinese garbled characters; attachments, etc. can be supported.

You can use the pear install Mail command on the server to quickly install it. Students who do not have sufficient server permissions can also directly download the PHP source code of the class and include it.

Note: Mail class depends on Net/SMTP.php and Mail/mime.php, which need to be downloaded together and included together when using them.

Let me give an example of how to send emails in the Mail class. The methods of using other SMTP mail classes on the Internet are similar. You can refer to:

<?php 
 
// Pear Mail 扩展 
require_once(&#39;Mail.php&#39;); 
require_once(&#39;Mail/mime.php&#39;); 
require_once(&#39;Net/SMTP.php&#39;); 
 
$smtpinfo = array(); 
$smtpinfo["host"] = "smtp.163.com";//SMTP服务器 
$smtpinfo["port"] = "25"; //SMTP服务器端口 
$smtpinfo["username"] = "username@163.com"; //发件人邮箱 
$smtpinfo["password"] = "password";//发件人邮箱密码 
$smtpinfo["timeout"] = 10;//网络超时时间,秒 
$smtpinfo["auth"] = true;//登录验证 
//$smtpinfo["debug"] = true;//调试模式 
 
// 收件人列表 
$mailAddr = array(&#39;receiver@163.com&#39;); 
 
// 发件人显示信息 
$from = "Name <username@163.com>"; 
 
// 收件人显示信息 
$to = implode(&#39;,&#39;,$mailAddr); 
 
// 邮件标题 
$subject = "这是一封测试邮件"; 
 
// 邮件正文 
$content = "<h3>随便写点什么</h3>"; 
 
// 邮件正文类型,格式和编码 
$contentType = "text/html; charset=utf-8"; 
 
//换行符号 Linux: \n Windows: \r\n 
$crlf = "\n"; 
$mime = new Mail_mime($crlf); 
$mime->setHTMLBody($content); 
 
$param[&#39;text_charset&#39;] = &#39;utf-8&#39;; 
$param[&#39;html_charset&#39;] = &#39;utf-8&#39;; 
$param[&#39;head_charset&#39;] = &#39;utf-8&#39;; 
$body = $mime->get($param); 
 
$headers = array(); 
$headers["From"] = $from; 
$headers["To"] = $to; 
$headers["Subject"] = $subject; 
$headers["Content-Type"] = $contentType; 
$headers = $mime->headers($headers); 
 
$smtp =& Mail::factory("smtp", $smtpinfo); 
 
 
$mail = $smtp->send($mailAddr, $headers, $body); 
$smtp->disconnect(); 
 
if (PEAR::isError($mail)) { 
  //发送失败 
  echo &#39;Email sending failed: &#39; . $mail->getMessage()."\n"; 
} 
else{ 
  //发送成功 
  echo "success!\n"; 
}
Copy after login

If the SMTP classes found on the Internet are all It is highly encapsulated, so it is simpler to use than the above, but the usage methods are relatively similar.

Conclusion: You don’t need to install any software to send emails in this way. You only need to include a PHP class and write a few more lines of configuration code. That’s it. And there are many sample codes on the Internet. In many cases, you only need to copy them and modify a few parameters to use them, so it is very convenient and it is recommended to use this method.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of function examples of PHP download files

PHP method to generate color QR codes based on QRCODE

PHP implements login verification code function and calling method

The above is the detailed content of The idea of ​​​​implementing email sending in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles