Home Backend Development PHP Tutorial 在PHP中施用SMTP发送邮件

在PHP中施用SMTP发送邮件

Jun 13, 2016 pm 01:11 PM
com mail smtp

在PHP中使用SMTP发送邮件

今天一个朋友问我如何在PHP中发送邮件,遂我让他去百度谷歌一搜一大把。良久之后我问他问题解决与否,他很失落地表示网上的教程都是一个模板,按照他们的来会有一些问题,所以我整理了一下,准备重新把这个方法总结一下,以便留作将来参考。

?

?

1.什么是SMTP

SMTP的全称是Simple?Mail?Transfer?Protocol,即简单邮件传输协议,简单来理解,这个玩意就是拿来发送邮件的,至于具体怎么去实现的,完全不需要深究,因为我只需要它的这个功能而已。

?

?

2.使用相应的软件来实现SMTP

我选择了hMailServer,个人感觉这个软件还不错,具体下载地址可以到这里寻

找最新版本

http://www.hmailserver.com/index.php?page=download

?

?

3.配置hMailServer

软件下好了,至于安装那都是千篇一律了,安装好之后进行登录,重点讲配置这一块。

?

?

?

首先,看看Status中的服务是否启动了,启动了之后进行下一步

?

?

?

接着找到Domain,选择右边框体的Add

?

?

?

?

接着,选中General选项卡,在Domain中写入你想要的域名,比如我使用benzlee.com。这里有个误区,很多人问,在这里不应该填写163.com或者sina.com或者tom.com这些邮箱吗?就在这一点中,很多教程都对大家产生了误导――我现在是用自己的电脑作为服务器发送邮件,为什么还要写上他们的域名?我当然可以自定义自己喜欢的域名,跟什么163.com什么sina.com完全没有任何关系。

?

?

?

?

然后,选中选项卡Limits,按照下图所示进行数据设置,这里主要是对发送邮件大小的一些限制,可以根据自己的需要调节

?

?

?

?

接下来,该要创建自己的邮件帐户了,也就是说,你使用该帐户形式发送邮件。选择benzlee.com下的Accounts,点击Add,添加自己的帐户信息

?

?

?

?

General选项卡的Address写入自己想要的帐户名,比如我设定为benzlee,输入自己的密码,我默认为空(这个密码是随意定的,除非你要认证该帐户),设定Maximum?size?1000,这样,帐户就创建好了

?

?

?

?

然后,选择Setting->Protocols,将IMAP前面的勾去掉

?

?

?

接着,选择SMTP,切换到Delivery?of?e-mail选项卡,根据图中进行设置

?

?

?

然后,选择Advanced->Auto-ban,把Enabled前面的勾去掉

?

?

?

?

然后切换到IP?RangesMy?Computer,进行如下设置

?

?

?

?

最后切换到Internet下,进行如下设置

?

?

?

?

搞定,hMailServer配置完毕。进入下一步

?

4.修改php.ini的内容,修改内容如下:

?1)修改SMTP?=?localhost

?2)修改sendmail_from?=?benzlee@benzlee.com,这里特别注意要去掉前面的分号,否则该句子算是被注释。这个句子是指该e-mail是由谁发送出去的,benzlee@benzlee.com就是刚才自己配置的邮件帐户。

?3)修改完成之后千万记得重启下Apache,否则就没有效果了。

?

5.自己写一个mail.php程序,可以使用如下代码:

?

$content?=?"This?is?a?mail?from?benzlee";???????//此处为邮件内容

$content?=?wordwrap($content,70);?????//?如果一行大于?70?个字符,使用?wordwrap()

mail("example@example.com","This?Is?My?SMTP",$content);????//调用PHP自带的mail函数,第一个参数为发送对象,第二个为邮件标题,第三个为邮件内容。

?>

?

example@example.com就是你想发送邮件的信箱地址,可以改成自己常用的信箱进行验证。

?

6.最后调用这个写好的php文件就能发送邮件了,大功告成。

没有收到自己的邮件?别纠结,去垃圾邮件箱看看你就知道了。。

最后。转载请注明出处,道理你懂的。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

What is the difference between comcn and com What is the difference between comcn and com May 12, 2023 pm 04:08 PM

The difference between comcn and com: 1. There are differences between comcn and com in terms of meaning, but there is no difference in access speed; 2. comcn is an international domain name and is a global top-level domain name for use by commercial institutions, while cn is a Chinese company domain name , domestic commercial institutions, domestic domain names, only enterprises can register; 3. The search priority is that cn will search for .cn first. After finding the .cn server, the .cn server will then search for .com; 4. cn is provided by cnnic China Internet Center Management, com's management organization is abroad.

How to disable smtp email function in php How to disable smtp email function in php Mar 22, 2023 pm 03:22 PM

PHP is a powerful programming language that is widely used in the field of Web development. The SMTP email function is also an important part of PHP development. However, in some cases, you may want to disable SMTP mail functionality, and this article will explain how to do this.

PHP Mail Usage Guide: Simple and easy-to-understand tutorial for sending emails PHP Mail Usage Guide: Simple and easy-to-understand tutorial for sending emails Mar 28, 2024 pm 12:12 PM

PHP is a scripting language widely used for developing web applications. It provides many functions for handling email sending. This article will introduce you to how to use the email sending function in PHP and provide specific code examples. 1. Preparation Before using PHP to send emails, you first need to ensure that your server has been configured to send emails. Generally speaking, you need an SMTP server to send emails. You can use the SMTP server provided by your email provider, such as Gmail's SMTP

How to use PHP to implement email communication based on SMTP protocol How to use PHP to implement email communication based on SMTP protocol Jul 30, 2023 pm 04:45 PM

How to use PHP to implement email communication based on SMTP protocol. With the popularity of the Internet, email has become an indispensable part of people's daily life and work. In PHP, we can use the SMTP (SimpleMailTransferProtocol) protocol to send and receive emails. This article will introduce how to use PHP to implement email communication based on the SMTP protocol, and come with relevant code examples. To reference the SMTP class library, we need to use the SMTP protocol.

How to use Java's built-in mail API to implement email sending function How to use Java's built-in mail API to implement email sending function May 11, 2023 pm 12:49 PM

Requirements: To send emails, use the jdk native API - java.mail to implement the email function. The following code can send emails, taking qq mailbox as an example packagecom.example.demo.emailInfo; importjavax.mail.Message; importjavax.mail.MessagingException; importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.Inte

How to send mail using SMTP in Go? How to send mail using SMTP in Go? May 11, 2023 pm 05:00 PM

With the fast-paced development of modern society, email has become an easy-to-use and universally accepted method of communication. As more and more applications require sending email notifications to users, using programs to send emails has become an important and necessary task. As a fast, simple and highly concurrency programming language, Go language can easily implement the function of sending emails. In this article, we will introduce how to send mail using SMTP in Go. SMTP is a standard protocol used for email transmission. In Go

How to send emails using PHP using Simple Mail Transfer Protocol (SMTP) How to send emails using PHP using Simple Mail Transfer Protocol (SMTP) May 22, 2023 pm 12:40 PM

With the development of the Internet, email has become one of the important ways for people to communicate in daily life. For some website developers, sending emails on the website is also particularly critical. As a server-side scripting language, PHP naturally needs to provide a way to send emails. This article will introduce how PHP uses Simple Mail Transfer Protocol (SMTP) to send emails. Introduction to SMTP Simple Mail Transfer Protocol (SimpleMailTransferProtocol, SMTP for short) is used to

See all articles