php发送电子邮件_使用PHP发送简单的电子邮件

php发送电子邮件

For low-traffic websites, it is reasonable to be informed by eMail when a site’s form is submitted. In principle, sending eMail with PHP couldn’t be simpler: it is a single function, mail, that has just three required arguments: to, subject and message.

对于低流量网站,在提交网站表格时通过电子邮件通知是合理的。 原则上讲,使用PHP发送电子邮件非常简单:它是单个函数mail ,只有三个必需的参数: tosubjectmessage

<?php
	mail(toaddress@domain.com, 'Subject Line', 'The body of the eMail message’); 
?>

This works for the simplest of messages. But before we proceed, there are several important points to note:

这适用于最简单的消息。 但是在我们继续之前,有几点要注意:

  1. mail relies on a mail server being installed and operational. This is not the default for most of the self-installed server options I recommended earlier. You may want to test your uses of mail from your actual hosting provider.

    mail依赖于已安装且可运行的邮件服务器。 对于我之前推荐的大多数自安装服务器选项,这不是默认设置。 您可能需要测试实际托管服务提供商对mail的使用情况。

  2. Many hosting providers will restrict or complicate the use of the mail function in order to avoid poorly written pages being used as spam gateways.  It is your responsibility to investigate any hurdles that may be unique to your hosting setup.

    许多托管服务提供商会限制或复杂化mail功能,以避免将写得不好的页面用作垃圾邮件网关。 您有责任调查可能对于您的托管设置而言唯一的任何障碍。

    When testing your eMail routine, make sure you can receive sent eMails promptly and correctly. Certain eMail services update their inbox less frequently; others may have over-zealous spam filters. In my experience, gMail is the best option.

    在测试电子邮件例程时,请确保您可以及时正确地接收已发送的电子邮件。 某些电子邮件服务更新收件箱的频率较低; 其他人可能拥有过度狂热的垃圾邮件过滤器。 以我的经验,gMail是最好的选择。

  3. If your form is submitted more than a dozen times a day, you will need to look at a different way of handling the incoming data (e.g. direct insertion of the information into a database).

    如果您的表单每天提交超过12次,那么您将需要研究处理传入数据的另一种方式(例如,将信息直接插入数据库中)。

    Use of the mail function is not the way to handle eMail campaigns. For that, you’ll likely want to use a service such as MailChimp.

    使用mail功能不是处理电子邮件活动的方法。 为此,您可能需要使用MailChimp之类的服务。

  4. Mail sent with the mail function is shown as being sent from the eMail address of the server administrator. Changing this, and a lot more besides, is the remit of a fourth, optional value, headers.

    使用mail功能发送的mail显示为从服务器管理员的电子邮件地址发送。 除此以外,还有其他许多更改,就是第四个可选值headers的权限。

First, the arguments that we have so far can easily be substituted with variables from a form:

首先,到目前为止,我们可以很容易地用形式变量来替换参数:

<?php
$to = 'youremailaddress@domain.com';
$subject = 'New form completion';
$message = $_POST['comment'];
mail($to,$subject,$message);
?>

(This, of course, assumes that the form information has been validated first – you don’t want to receive an eMail every time the form is filled out incorrectly).

(当然,这假定表单信息已首先通过验证 -您不希望每次表单填写不正确时都收到一封电子邮件)。

Because the message portion can become very long, it is common to join components together to form the entire message, using a special form of concatenation, along with escaped line returns (note the use of periods and spaces immediately after the second use of the $message variable):

因为消息部分可能会变得很长,所以通常使用特殊的串联形式将组件连接在一起以形成完整的消息,以及转义的换行符(请注意在第二次使用$message之后立即使用句点和空格) $message变量):

<?php
$message = 'New message from '.$_POST[firstname].' '.$_POST[lastname].': \r\n';
$message .= $_POST['comment'];
?>

The result, sent in the body of the eMail, would include the supplied first and last name of the person who filled out the form, together with their message:

在电子邮件正文中发送的结果将包括填写表格的人的名字和姓氏,以及他们的信息:

New message from Harry Belafonte:

Harry Belafonte的新讯息:

Day day light an me wan’ go home

白天,我要回家了

For anything more complex than this: sending a "rich" HTML eMail, or showing the eMail as being sent from the person submitting the form, rather than the eMail administrator, we need to modify the eMail headers.

对于任何比这更复杂的事情:发送“丰富的” HTML电子邮件,或显示电子邮件是从提交表单的人(而不是电子邮件管理员)发送的,我们需要修改电子邮件标题

翻译自: https://thenewcode.com/331/Sending-A-Simple-eMail-With-PHP

php发送电子邮件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值