开发大刘 潜水
  • 3发帖数
  • 3主题数
  • 0关注数
  • 0粉丝
开启左侧

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

[复制链接]
开发大刘 发表于 2021-8-14 21:52:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
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.
我盼望我能记得我发送的第一封电子邮件。不幸的是,事实是我已经把电子邮件视为理所固然;例如,我很轻易发送比打电话更多的电子邮件。因为新奇感早已消失,我对第一封电子邮件的记忆就像第一次打电话一样消失了;但我猜疑其时我的惊奇感是否不够完整。对于一个以前从未见过电子邮件的人来说,第一条消息可能很神奇。
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 systemsSMTP = smtp.my.isp.netsendmail_from = [email protected] here’s how it might look on a Linux server with sendmail:[mail function]; Setup for Linux systemssendmail_path = /usr/sbin/sendmail -tsendmail_from = [email protected] 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 [email protected] 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.
上述行将向发送电子邮件[email protected]以“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,并且您有一个发送电子邮件并进行适当错误检查的公式:
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 [email protected] — 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.
请注意,仅仅因为可以发送电子邮件并不保证它会到达目的地。电子邮件地点可能有效(即格式正确),但现实上不存在。例如,您可以成功地向不存在的用户发送消息。[email protected]也就是说,邮件函数将返回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:
当您想向多个收件人发送邮件时,您可以在第一个参数中逐个列出他们的电子邮件地点,并用逗号分隔。例如:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

猜你喜欢
在线客服邮箱
wxcy#wkgb.net

邮箱地址#换为@

Powered by 创意电子 ©2018-现在 专注资源实战分享源码下载站联盟商城