PHP email sending case

墨辰丷
Release: 2023-03-30 10:00:02
Original
1449 people have browsed it

This article mainly introduces PHP email sending cases. Interested friends can refer to it. I hope it will be helpful to everyone.

The role of the mail() function: Connect to the mail server, use the smtp protocol, interact with the server and send mail.

Note:

1. The mail function does not support the esmtp protocol, that is, it can only cast directly and cannot log in.

2. From the above article, we can only send directly to the final receiving server address. And this address is specified in PHP.ini, so we want to use the mail() function to aseoev@ If 163.com sends an email, we need to ---

1) Query the address of the 163 mail server

2) Write the address to php. ini to

php example code is as follows:

SMTP = 163mx02.mxmail.netease.com 
sendmail_from = wusong@192.168.1.100 
var_dump(mail('12345678@qq.com','from php mail function','very intresting'));
Copy after login

But to use the mail function that comes with php to send emails, we need to install one in linux Only sendmail component can be used, otherwise it cannot be used.

If you don’t have this sendmail component, we can use the phpmailer function to operate it. The example code is as follows:

<?php 
 
 require(&#39;./PHPMailer/class.phpmailer.php&#39;); 
 
 $phpmailer = new PHPMailer(); 
 
 $phpmailer->IsSMTP(); 
 
 $phpmailer->Host = &#39;smtp.163.com&#39;; 
 $phpmailer->SMTPAuth = true; 
 $phpmailer->Username = &#39;&#39;; 
 $phpmailer->Password = &#39;&#39;; 
 
 $phpmailer->CharSet = &#39;utf-8&#39;; 
 $phpmailer->From = &#39;&#39;; 
 $phpmailer->FromName = &#39;&#39;; 
 $phpmailer->Subject = &#39;&#39;; 
 $phpmailer->Body = &#39;&#39;; 
 
 $phpmailer->AddAddress(&#39;never_kiss@163.com&#39;,&#39;Aseoe&#39;); 
 
 echo $phpmailer->send()?&#39;发送成功&#39;:&#39;发送失败&#39;; 
 
?>
Copy after login

There is no content above. Let’s take a look at the code with content. As follows:

<?php 
 
/** 
用PHPMailer类来发信 


步骤: 
0: 引入 
1: 实例化 
2: 配置属性 
3: 调用发送 
**/ 
require(&#39;./PHPMailer/class.phpmailer.php&#39;); 
$phpmailer = new PHPMailer(); 
 
/* 
设置phpmailer发信用的方式 
可用用win下mail()函数来发 
可以用linux下sendmail,qmail组件来发 
可以利用smtp协议登陆到某个账户上,来发 
*/ 
$phpmailer->IsSMTP(); // 用smtp协议来发 
$phpmailer->Host = &#39;smtp.163.com&#39;; 
$phpmailer->SMTPAuth = true; 
$phpmailer->Username = &#39;&#39;; //发送邮箱的账号(用163邮箱发信的账号) 
$phpmailer->Password = &#39;&#39;; //发送邮箱的密码 
// 可以发信了 
$phpmailer->CharSet=&#39;utf-8&#39;; 
$phpmailer->From = &#39;never_4ill@163.com&#39;; 
$phpmailer->FromName = &#39;neverkill&#39;; 
$phpmailer->Subject = &#39;Superstart Aseoe&#39;; 
$phpmailer->Body = &#39;脚本之家(http://www.jb51.net 专注前端开发与编程设计.&#39;; 
//设置收信人 
$phpmailer->AddAddress(&#39;never_4ill@163.com&#39;,&#39;neverkill&#39;); 
// 添加一个抄送 
$phpmailer->AddCC(&#39;1234567&#39;,&#39;Aseoe&#39;); 
// 发信 
echo $phpmailer->send()?&#39;ok&#39;:&#39;fail&#39;;
Copy after login

Add a method using the above example:

Directly decompress the phpmailer compressed package and put it in the root directory to run, and put the file directly into the local wamp In the root directory, run 02.php to send the email (assuming the php file is executable) - (if not, create a folder in the root directory and repeat the operation) http://localhost/02.php.

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

Related recommendations:

php uses ffmpeg to implement the method of adding text subtitles to videos

php The definition of parse_str() function and Detailed explanation of usage examples

php method of randomly selecting values ​​from arrays and simple examples

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template