Table of Contents
Two ways to send emails in PHP, two ways to send emails
Home Backend Development PHP Tutorial Two ways to send emails in php, two ways to send emails_PHP tutorial

Two ways to send emails in php, two ways to send emails_PHP tutorial

Jul 12, 2016 am 09:06 AM
php send email mail

Two ways to send emails in PHP, two ways to send emails

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

<&#63;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 the reason: A local SMTP server is required, so I changed the code:

<&#63;php 
$to = "test@163.com";//收件人 
$subject = "Test";//邮件主题 
$message = "This is a test mail!";//邮件正文 
ini_set('SMTP','smtp.163.com');//发件SMTP服务器 
ini_set('smtp_port',25);//发件SMTP服务器端口 
ini_set('sendmail_from',"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 needed. 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 email 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.

However, 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, and 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. is powerful: can support emails in plain text and HTML formats; each field can Set the encoding, and the correct configuration will not cause Chinese garbled characters; attachments and so on 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:

<&#63;php 
 
// Pear Mail 扩展 
require_once('Mail.php'); 
require_once('Mail/mime.php'); 
require_once('Net/SMTP.php'); 
 
$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('receiver@163.com'); 
 
// 发件人显示信息 
$from = "Name <username@163.com>"; 
 
// 收件人显示信息 
$to = implode(',',$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['text_charset'] = 'utf-8'; 
$param['html_charset'] = 'utf-8'; 
$param['head_charset'] = 'utf-8'; 
$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 'Email sending failed: ' . $mail->getMessage()."\n"; 
} 
else{ 
  //发送成功 
  echo "success!\n"; 
} 
Copy after login

If you find the SMTP classes on the Internet, they are highly encapsulated, so they will be 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 example 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.

The above are two methods of sending emails using PHP. I have my own thinking process. I hope it will be helpful to everyone's learning.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1065583.htmlTechArticleTwo ways to send emails using PHP. Two ways to send emails. The main content of this article is to use PHP to send emails. Sending emails can be summarized as the following two methods: 1. Use PHP built-in...
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles