Home > Backend Development > PHP Tutorial > PHP uses SMTP class to implement email sending code example

PHP uses SMTP class to implement email sending code example

怪我咯
Release: 2023-03-14 06:44:01
Original
1818 people have browsed it

SMTP (Simple Mail Transfer Protocol) is a simple mail transfer protocol. It is a set of rules for transmitting mail from a source address to a destination address. It controls the transfer method of letters. The SMTP protocol belongs to the TCP/IP protocol suite, which helps each computer find the next destination when sending or relaying letters. Through the server specified by the SMTP protocol, the E-mail can be sent to the recipient's server. The whole process only takes a few minutes. The SMTP server is an outgoing email server that follows the SMTP protocol and is used to send or relay outgoing emails.

It uses the reliable data transmission service provided by TCP to transmit the email message from the sender's mail server to the recipient's mail server. Like most application layer protocols, SMTP has two ends: a client that executes on the sender's mail server and a server that executes on the recipient's mail server. The client and server sides of SMTP run simultaneously on each mail server. When a mail server sends mail messages to other mail servers, it is running as an SMTP client.

There are many similarities between the SMTP protocol and the etiquette that people use for face-to-face interactions. First, the SMTP client running on the sending mail server host initiates the establishment of a TCP connection to the SMTP server port number 25 running on the receiving mail server host. If the receiving mail server is not currently working, the SMTP client waits for a period of time before trying to establish the connection. The SMTP client and server first perform some application layer handshake operations. Just as people introduce themselves before changing hands, SMTP clients and servers introduce themselves before sending information. During this SMTP handshake phase, the SMTP client indicates the email addresses of the sender and recipient to the server. After introducing themselves to each other, the client sends an email message.

This article mainly introduces how PHP uses the SMTP class to easily send emails. The method of sending emails using the SMTP class is actually very simple. Friends in need can refer to it.

The following is the core code:

<?php
 require_once "email.class.php";
 //******************** 配置信息 ********************************
 $smtpserver = "smtp.126.com";//SMTP服务器
 $smtpserverport =25;//SMTP服务器端口
 $smtpusermail = "new2008oh@126.com";//SMTP服务器的用户邮箱
 $smtpemailto = $_POST[&#39;toemail&#39;];//发送给谁
 $smtpuser = "new2008oh";//SMTP服务器的用户帐号
 $smtppass = "您的邮箱密码";//SMTP服务器的用户密码
 $mailtitle = $_POST[&#39;title&#39;];//邮件主题
 $mailcontent = "<h1>".$_POST[&#39;content&#39;]."</h1>";//邮件内容
 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
 //************************ 配置信息 ****************************
 $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
 $smtp->debug = false;//是否显示发送的调试信息
 $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

 echo "<p style=&#39;width:300px; margin:36px auto;&#39;>";
 if($state==""){
 echo "对不起,邮件发送失败!请检查邮箱填写是否有误。";
 echo "<a href=&#39;index.html&#39;>点此返回</a>";
 exit();
 }
 echo "恭喜!邮件发送成功!!";
 echo "<a href=&#39;index.html&#39;>点此返回</a>";
 echo "</p>";
?>
Copy after login

The above is the detailed content of PHP uses SMTP class to implement email sending code example. 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