php 发送邮件的简单示例包含纯文本格式、html格式、添加附件

PHP发送邮件,包含纯文本格式、html格式和添加附件,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。/ * PHP:发送电子邮件(文本/ HTML /附件)
 
电子邮件是当今最受欢迎的互联网服务。每天都会发送和发送大量电子邮件。本教程的目标是演示如何使用PHP生成和发送电子邮件
 
因此,您希望从程序发送自动电子邮件。这可以直接响应用户的操作,例如注册您的网站,或在设定的时间重复发生的活动,例如每月简报。有时,电子邮件包含文件附件,包括纯文本和HTML部分,等等。要了解如何发送电子邮件中可能存在的每个变体,我们将从简单示例开始,然后转移到更复杂的变体。
 
    *发送简单文本电子邮件
    *发送HTML电子邮件
    *发送带附件的电子邮件
 
请注意,要使用PHP发送电子邮件,您需要一个有权使用的有效电子邮件服务器:对于Unix机器,这通常是Sendmail;对于Windows机器,您必须在PHP.ini文件中设置SMTP指令以指向您的电子邮件服务器。
 
发送简单文本电子邮件
 
首先让我们考虑如何发送简单的文本电子邮件PHP包含用于发送电子邮件的mail()函数,该函数包含三个基本参数和两个可选参数。这些参数依次是要发送到的电子邮件地址,电子邮件主题,要发送的消息,要包含的其他标头以及最后一个Sendmail程序的附加参数。如果消息发送成功,mail()函数返回True,否则返回False。看看这个例子:
经测试代码如下:

/**
 * 发送邮件,包含纯文本格式、html格式和添加附件
 *
 * @param 
 * @arrange (编程之家) jb51.cc
 **/
<?PHP
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = Hello World!\n\nThis is my first mail.;
//define the headers we want passed. Note that they are separated with 
 
$headers = From: webmaster@example.com
Reply-To: webmaster@example.com;
//send the email
$mail_sent = @mail( $to,$subject,$message,$headers );
//if the message is sent successfully print Mail sent. Otherwise print Mail Failed 
echo $mail_sent ? Mail sent : Mail Failed;
?>
 
如您所见,发送电子邮件非常容易。 您可以通过将其地址(以逗号分隔)添加到$ to变量或添加cc:或bcc:headers来添加更多接收器。 如果您没有收到测试邮件,您可能错误地安装了PHP,或者可能没有发送电子邮件的权限。
 
回到顶部
 
发送HTML电子邮件
 
下一步是检查如何发送HTML电子邮件。 但是,某些邮件客户端无法理解HTML电子邮件。 因此,最好使用多部分构造发送任何HTML电子邮件,其中一部分包含电子邮件的纯文本版本,另一部分是HTML。 如果您的客户关闭了HTML电子邮件,他们仍会收到一封精美的电子邮件,即使他们没有获得所有HTML标记。 看看这个例子:
<?PHP
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r',time()));
//define the headers we want passed. Note that they are separated with 
 
$headers = From: webmaster@example.com
Reply-To: webmaster@example.com;
//add boundary string and mime type specification
$headers .= 
Content-Type: multipart/alternative; boundary=\PHP-alt-.$random_hash.\;
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?PHP echo $random_hash; ?> 
Content-Type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7bit
 
Hello World!!! 
This is simple text email message. 
 
--PHP-alt-<?PHP echo $random_hash; ?> 
Content-Type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
 
--PHP-alt-<?PHP echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to,$headers );
//if the message is sent successfully print Mail sent. Otherwise print Mail Failed 
echo $mail_sent ? Mail sent : Mail Failed;
?>
 
在前面的示例中,我们添加一个额外的Content-type标头:multipart / alternative和标记电子邮件不同区域的边界字符串。 请注意,消息本身的内容类型作为邮件头发送,而消息的各个部分的内容类型嵌入在消息本身中。 这样,邮件客户端可以决定要显示邮件的哪个部分。
 
使用附件发送电子邮件
 
我们将考虑的最后一个变体是带附件的电子邮件。 要发送带附件的电子邮件,我们需要使用multipart / mixed MIME类型,指定混合类型将包含在电子邮件中。 此外,我们希望使用multipart / alternative MIME类型来发送电子邮件的纯文本和HTML版本。 看看这个例子:
<?PHP
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r',time()));
//define the headers we want passed. Note that they are separated with 
 
$headers = From: webmaster@example.com
Reply-To: webmaster@example.com;
//add boundary string and mime type specification
$headers .= 
Content-Type: multipart/mixed; boundary=\PHP-mixed-.$random_hash.\;
//read the atachment file contents into a string,//encode it with MIME base64,//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?PHP echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary=PHP-alt-<?PHP echo $random_hash; ?>
 
--PHP-alt-<?PHP echo $random_hash; ?> 
Content-Type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7bit
 
Hello World!!!
This is simple text email message.
 
--PHP-alt-<?PHP echo $random_hash; ?> 
Content-Type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
 
--PHP-alt-<?PHP echo $random_hash; ?>--
 
--PHP-mixed-<?PHP echo $random_hash; ?> 
Content-Type: application/zip; name=attachment.zip 
Content-transfer-encoding: base64 
Content-disposition: attachment 
 
<?PHP echo $attachment; ?>
--PHP-mixed-<?PHP echo $random_hash; ?>--
 
<?PHP
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to,$headers );
//if the message is sent successfully print Mail sent. Otherwise print Mail Failed
echo $mail_sent ? Mail sent : Mail Failed;
?>
 
/***   代码来自编程之家 jb51.cc(jb51.cc)   ***/
如您所见,发送带附件的电子邮件很容易实现。
在前面的示例中,我们有多部分/混合MIME类型,在其中我们有多部分/替代MIME类型,指定电子邮件的两个版本。 要在我们的消息中包含附件,我们将指定文件中的数据读入字符串,使用base64对其进行编码,将其拆分为较小的块以确保它与MIME规范匹配,然后将其作为附件包含。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...