≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

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

人气:673 时间:2018-09-20

这篇文章主要为大家详细介绍了php 发送邮件的简单示例(包含纯文本格式、html格式、添加附件),具有一定的参考价值,可以用来参考一下。

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

/**
 * 发送邮件,包含纯文本格式、html格式和添加附件
 *
 * @param 
 * @arrange (512.笔记) www.q1010.com
 **/
<?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, $subject, $message, $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, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
 
/***   代码来自四海网(www.q1010.com)   ***/
如您所见,发送带附件的电子邮件很容易实现。
在前面的示例中,我们有多部分/混合MIME类型,在其中我们有多部分/替代MIME类型,指定电子邮件的两个版本。 要在我们的消息中包含附件,我们将指定文件中的数据读入字符串,使用base64对其进行编码,将其拆分为较小的块以确保它与MIME规范匹配,然后将其作为附件包含。

本文来自:http://www.q1010.com/173/476-0.html

注:关于php 发送邮件的简单示例(包含纯文本格式、html格式、添加附件)的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:邮件

您可能感兴趣的文章

  • php 正则表达式检查邮件地址是否正确的简单示例
  • php 通过joomla jmail 类使用 gmail smtp 账号发送邮件的简单示例
  • php 从电子邮件抽取一个字符串的实现方法
  • PHP 邮件地址验证示例
  • PHP 电子邮件地址编码示例
  • PHP 邮件自动发送的简单示例
  • PHP 实现查看邮件是否已被阅读
上一篇:php 简单实用的分页函数的完整代码
下一篇:php 检测客户端是否iphone、ipod的简单示例
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。