回顾使用PHP原生发送电子邮件(一)

2022-10-11 22:01:47 149 0
魁首哥

I wish I could remember the very first email message I ever sent. Unfortunately, the truth is that I’ve come to take email for granted; for instance, I easily send more email messages than I make phone calls. Because the novelty has long since worn off, the memory of that first email is as lost to me as my first phone call; but I doubt my sense of wonder was any less complete at the time. To someone who has never seen email in action before, that first message can be magical.

我希望我能记得我发送的第一封电子邮件。不幸的是,事实是我已经把电子邮件视为理所当然;例如,我很容易发送比打电话更多的电子邮件。因为新奇感早已消失,我对第一封电子邮件的记忆就像第一次打电话一样消失了;但我怀疑当时我的惊奇感是否不够完整。对于一个以前从未见过电子邮件的人来说,第一条消息可能很神奇。

回顾使用PHP原生发送电子邮件(一)

In this article, I’ll try to recapture some of that magic for those of you who have never created a Web site that sends email messages. We’ll see how the PHP server-side scripting language may be set up to send email, and explore how to send complex message types such as HTML email or emails with file attachments.

在这篇文章中,我将尝试为那些从未创建过发送电子邮件的网站的人重温其中的一些魔力。我们将了解如何设置PHP服务器端脚本语言来发送电子邮件,并探索如何发送复杂的消息类型,如HTML电子邮件或带有文件附件的电子邮件。

PHP Email Setup

PHP电子邮件步骤

Before we can send email with PHP, we need to set it up to do so, just as you need to set up your email program before it can send messages. Configuration for sending email in PHP is done with the php.ini file, so open up your Web server’s php.ini in whichever editor you normally use.

在我们用PHP发送电子邮件之前,我们需要对它进行设置,就像您需要在它发送消息之前设置电子邮件程序一样。用PHP发送电子邮件的配置是通过PHP.ini文件完成的,所以在您通常使用的编辑器中打开Web服务器的PHP.ini。

If you don’t run your own server, but instead have a PHP-equipped Web host, you can safely assume that everything in this section has been done for you, and skip ahead.

如果您没有运行自己的服务器,而是有一个配备了PHP的Web主机,那么您可以放心地假设本节中的所有内容都已为您完成,然后跳过。

In the section entitled [mail function] in the php.ini file, you’ll find three settings: SMTP, sendmail_from, and sendmail_path. If your server runs on a Windows machine, you’ll want to set the SMTP option to point to your SMTP server (or your ISP’s SMTP server, if you’re setting up PHP on your home machine). If instead you’re setting up PHP on a Linux (or other Unix-based OS) server, you’ll want to set the sendmail_path option to point to the sendmail program on your server, passing it the -t option. You can use the SMTP option in Linux instead if you don’t have sendmail set up.

在php.ini文件中标题为[mail function]的部分中,您将找到三个设置:SMTP、sendmail_from和sendmail_path。如果您的服务器在Windows计算机上运行,则需要将SMTP选项设置为指向您的SMTP服务器(或ISP的SMTP服务器,如果您在家庭计算机上设置PHP)。如果要在Linux(或其他基于Unix的操作系统)服务器上设置PHP,则需要将sendmail_path选项设置为指向服务器上的sendmail程序,并将-t选项传递给它。如果没有设置sendmail,则可以在Linux中使用SMTP选项。

In either case, you’ll want to set the sendmail_from option to your email address, or whichever address you’d like to appear as the default ‘from’ address for emails sent from PHP scripts.

在任何一种情况下,您都需要将sendmail_from选项设置为您的电子邮件地址,或者您希望显示为从PHP脚本发送的电子邮件的默认“from”地址的任何地址。

Here’s how the section might look on a typical Windows server, or on a Linux server without sendmail:

以下是该部分在典型Windows服务器上的外观,或在没有sendmail的Linux服务器上的展示:

 [mail function]
; Setup for Windows systems
SMTP = smtp.my.isp.net
sendmail_from = me@myserver.com
And here’s how it might look on a Linux server with sendmail:

[mail function]
; Setup for Linux systems
sendmail_path = /usr/sbin/sendmail -t
sendmail_from = me@myserver.com
With those settings in place, restart your Web server and you’re ready to go!  

Sending Plain Email

发送普通电子邮件

It doesn’t come much easier than the procedure to send plain text email in PHP. In fact, you can do it in just one line in a PHP script:

用PHP发送纯文本电子邮件的过程并不容易。事实上,您可以在PHP脚本的一行中完成:

   

The above line would send an email message to recipient@some.net with ‘Subject‘ as the subject line and ‘Your message here.‘ as the message body. As you can see, PHP’s mail function makes sending email exceedingly simple, but there are a few advanced tricks we can use to get more out of this simple function.

上述行将向发送电子邮件recipient@some.net以“Subject”作为标题,以“Your message here.”作为邮件正文。正如您所看到的,PHP的邮件函数使发送电子邮件变得非常简单,但是我们可以使用一些高级技巧来从这个简单的函数中获得更多信息。

First of all, if the mail system you configured in your php.ini file rejects a message you try to send (for example, if the ‘to’ address is not a valid email address), this function will display an error message in the user’s browser. Like most PHP functions, however, error messages may be suppressed by preceding the function name with an @ symbol. Combine this with the fact that the mail function returns either true or false depending on whether the email was accepted by the mail sending system, and you have a formula to send email with appropriate error checking:

首先,如果您在php.ini文件中配置的邮件系统拒绝您尝试发送的邮件(例如,如果“to”地址不是有效的电子邮件地址),此函数将在用户浏览器中显示错误消息。但是,与大多数PHP函数一样,可以通过在函数名前面加上@符号来抑制错误消息。再加上邮件函数根据邮件发送系统是否接受邮件而返回true或false,并且您有一个发送电子邮件并进行适当错误检查的公式:

 Mail sent successfully.

'); } else { echo('

Mail could not be sent.

'); } ?>

Note that just because an email message could be sent doesn’t guarantee it will arrive at its destination. An email address can be valid (i.e. correctly formed) but not actually exist. For instance, you can successfully send a message to nonexistant.user@hotmail.com — that is, the mail function will return true — but the message will bounce because no such user exists. PHP provides no built-in means of detecting when this happens.

请注意,仅仅因为可以发送电子邮件并不保证它会到达目的地。电子邮件地址可能有效(即格式正确),但实际上不存在。例如,您可以成功地向不存在的用户发送消息。user@hotmail.com-也就是说,邮件函数将返回true-但邮件将反弹,因为不存在这样的用户。PHP没有提供检测何时发生这种情况的内置方法。

When you want to send a message to multiple recipients, you can just list their email addresses one after another, separated by commas, in the first parameter. For example:

当您想向多个收件人发送邮件时,您可以在第一个参数中逐个列出他们的电子邮件地址,并用逗号分隔。例如:

   

收藏
分享
海报
0 条评论
149
上一篇:php常用函数(四) 下一篇:PHP的APNS消息推送实现

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码