Home > Article > Backend Development > PHP test successful email sending case, php email sending case_PHP tutorial
The role of the mail() function: Connect to the mail server and use the smtp protocol to interact with the server And send mail.
Note:
1. The mail function does not support the esmtp protocol, that is, it can only be cast directly and cannot be logged 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 send to aseoev@163.com If you want to send a letter, we need to---
1) Query the address of 163 mail server
2) Write the address into php.ini
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'));
But to use the mail function that comes with PHP to send emails, we need to install a sendmail component in linux before it can be used otherwise.
If you don’t have this sendmail component, we can use the phpmailer function to operate. The example code is as follows:
<?php require('./PHPMailer/class.phpmailer.php'); $phpmailer = new PHPMailer(); $phpmailer->IsSMTP(); $phpmailer->Host = 'smtp.163.com'; $phpmailer->SMTPAuth = true; $phpmailer->Username = ''; $phpmailer->Password = ''; $phpmailer->CharSet = 'utf-8'; $phpmailer->From = ''; $phpmailer->FromName = ''; $phpmailer->Subject = ''; $phpmailer->Body = ''; $phpmailer->AddAddress('never_kiss@163.com','Aseoe'); echo $phpmailer->send()?'发送成功':'发送失败'; ?>
There is no content on it. Let’s look at the one with content. The code is as follows:
<?php /** 用PHPMailer类来发信 步骤: 0: 引入 1: 实例化 2: 配置属性 3: 调用发送 **/ require('./PHPMailer/class.phpmailer.php'); $phpmailer = new PHPMailer(); /* 设置phpmailer发信用的方式 可用用win下mail()函数来发 可以用linux下sendmail,qmail组件来发 可以利用smtp协议登陆到某个账户上,来发 */ $phpmailer->IsSMTP(); // 用smtp协议来发 $phpmailer->Host = 'smtp.163.com'; $phpmailer->SMTPAuth = true; $phpmailer->Username = ''; //发送邮箱的账号(用163邮箱发信的账号) $phpmailer->Password = ''; //发送邮箱的密码 // 可以发信了 $phpmailer->CharSet='utf-8'; $phpmailer->From = 'never_4ill@163.com'; $phpmailer->FromName = 'neverkill'; $phpmailer->Subject = 'Superstart Aseoe'; $phpmailer->Body = '帮客之家(http://www.bkjia.com 专注前端开发与编程设计.'; //设置收信人 $phpmailer->AddAddress('never_4ill@163.com','neverkill'); // 添加一个抄送 $phpmailer->AddCC('1234567','Aseoe'); // 发信 echo $phpmailer->send()?'ok':'fail';
Add a method using the above example:
Directly decompress the phpmailer compressed package and put it in the root directory to run it. Directly put the file into the local wamp root directory and run 02.php to send the email (prerequisite is that the php file is executable) - (If not, create it in the root directory. Repeat the operation once for a folder) http://localhost/02.php.
The above is a successful case of sending emails in php. I hope it will be helpful to everyone's learning.