利用php中mail函数发送带有附件的邮件

mail函数,发送邮件

语法: mail(to,subject,message,headers,parameters)

to 规定邮件的接收者
subject 规定邮件的主题。该参数不能包含任何换行字符
message 规定要发送的消息
headers 规定额外的报头,比如 From, Cc 以及 Bcc
parameters 规定 sendmail 程序的额外参数。
碰到的主要问题是乱码问题,刚开始是某些客户端接收邮件时好(比如QQ邮箱,估计带自动那个识别编码)的有些不foxmail、ipad显示乱码,解决方式正确的设置这个mail的headers就行了,下面是我使用的完美的无乱码的例子。

 在PHP中配置php.ini文件过程分为两个步骤:

1.先找到你放置所有PHP,Apache,MySQL文件的地方,在PHP文件夹里你可以发现有一个文件:php.ini,打开后,找到mail function地方,将原来的配置代码改为如下(仅对windows系统):

[mail function]
; For Win32 only.
SMTP =smtp.sohu.com   
mtp_port=25

; For Win32 only.
sendmail_from = 填上你的电子邮件全称。

   此处为以sohu的邮件服务器设置,如果你用163的邮箱,则设置为:smtp.163.com

2.在C盘搜索php.ini,选择不是快捷方式的那一个php.ini,应该在C/WINDOWS里面的,打开它,如上面一样修改它,保存。

设置完后,记得重启Apache服务器,然后mail()函数就可以用了。

 代码如下 复制代码

<?php
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "rn";

$headers .= "Content-type:text/html; charset=utf-8";

mail($to,$subject,$message,$headers);
?>

上面函数不可以带附件了,下面我们升级一下

 

 代码如下 复制代码

<?php

class Mail {
private $topic;
private $toaddr;
private $fromaddr;
private $cc;
private $content;
private $attach;
private $header;
private $domain;//邮箱域
private $msg;

private $filename;
private $filemime;
private $filestr;

private $boundary;
private $uniqid;

private $eol; //每行末尾所加的换行符类型

function __construct(){
$this->getEOT();//生成结尾换行符
$this->getUniq_id();
$this->header='';
$this->attach='';
$this->cc='';
$this->msg='';

}

public function getFromaddr() {
return $this->fromaddr;
}

public function setFromaddr($fromaddr) {
$this->fromaddr = $fromaddr;
}

public function getTopic() {
return $this->topic;
}

public function getToaddr() {
return $this->toaddr;
}

public function getCc() {
return $this->cc;
}

public function getContent() {
return $this->content;
}

public function getAttach() {
return $this->attach;
}

public function setTopic($topic) {
$this->topic = mb_convert_encoding(trim($topic),'UTF-8','auto');
}

public function setToaddr($toaddr) {
$this->toaddr = trim($toaddr);
}

public function setCc($cc) {
$this->cc = trim($cc);
}

public function setContent($content) {
$this->content = mb_convert_encoding(trim($content),'UTF-8','auto');
}

public function setAttach($attach) {
$this->attach = trim($attach);
}

public function getDomain() {
return $this->domain;
}

public function setDomain($domain) {
$this->domain = $domain;//输入的值为'@domain.com'
}

/*
* 根据系统类型设置换行符
*/
private function getEOT() {
if (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'WIN' )) {
$this->eol = "rn";
} elseif (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'MAC' )) {
$this->eol= "r";
} else {
$this->eol = "n";
}
}

private function getBoundary(){

$this->boundary= '--'.substr(md5(time().rand(1000,2000)),0,16);

}

private function getUniq_id(){

$this->uniqid= md5(microtime().time().rand(1,100));

}

private function outputCommonHeader(){
$this->header .= 'From: '.$this->fromaddr.$this->eol;
//$this->header .= 'To: '.$this->toaddr.$this->eol;
//$this->header .= 'Subject: '.$this->topic.$this->eol;
$this->header .= 'Message-ID: <'.$this->uniqid.$this->domain.'>'.$this->eol;
$this->header .= 'MIME-Version: 1.0'.$this->eol;
$this->header .= 'Reply-To: '.$this->fromaddr.$this->eol;
$this->header .= 'Return-Path: '.$this->fromaddr.$this->eol;
$this->header .= 'X-Mailer: Xmail System'.$this->eol;
$this->header .= 'Content-Disposition: inline'.$this->eol;
}

private function mime_content_type ( $f )
{
$temp = trim ( exec ('file -bi ' . escapeshellarg ( $f ) ) ) ;
$temp = preg_replace('/s+/',' ',$temp);
$temp = explode(' ',$temp);
return $temp[0];
}//判断文件的mime类型

/*
* 只带有抄送
*/
private function mailWithCC(){
$this->header .= 'Cc: '.$this->cc.$this->eol;
$this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol;
$this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol;
$this->msg = $this->content;
if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}
}
/*
* $filedir需要是绝对地址
*/
private function attachmentToBase64($filedir){
$this->filename = basename($filedir);
@$fopen = fopen($filedir,'r');
$str = fread($fopen,filesize($filedir));
$str = base64_encode($str);
$this->filestr = $str;
}

/*
* 只带有附件
*/
private function mailWithAttach(){
$this->attachmentToBase64($this->attach);
$this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol;
$this->msg .= $this->eol.$this->boundary.$this->eol;
$this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol;
$this->msg .= 'Content-Disposition: inline'.$this->eol;
$this->msg .= $this->eol.$this->content.$this->eol;
$this->msg .= $this->boundary.$this->eol;
$this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol;
$this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol;
$this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol;
$this->msg .= $this->eol.$this->filestr.$this->eol;
$this->msg .= $this->eol.$this->boundary.'--';

if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}
}

/*
* 带有附件和抄送
*/
private function mailAll(){

$this->attachmentToBase64($this->attach);
$this->header .= 'Cc: '.$this->cc.$this->eol;
$this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol;
$this->msg .= $this->eol.$this->boundary.$this->eol;
$this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol;
$this->msg .= 'Content-Disposition: inline'.$this->eol;
$this->msg .= $this->eol.$this->content.$this->eol;
$this->msg .= $this->boundary.$this->eol;
$this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol;
$this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol;
$this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol;
$this->msg .= $this->eol.$this->filestr.$this->eol;
$this->msg .= $this->eol.$this->boundary.'--';

if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}

}
/*
* 不带抄送和附件
*/
private function mailSimple(){
$this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol;
$this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol;
$this->msg = $this->content;
if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}
}

public function send(){

if(empty($this->attach)&&empty($this->cc)){
$this->outputCommonHeader();
return $this->mailSimple();

}else if(empty($this->attach)){
$this->outputCommonHeader();
return $this->mailWithCC();

}else if(empty($this->cc)){
$this->outputCommonHeader();
$this->getBoundary(); //有附件就生成boundary
return $this->mailWithAttach();

}else if(!empty($this->toaddr)&&!empty($this->topic)&&!empty($this->cc)&&!empty($this->content)&&!empty($this->attach)){
$this->outputCommonHeader();
$this->getBoundary(); //有附件就生成boundary
return $this->mailAll();
}
}
}

示例代码,有些变量需要上下文环境:

 代码如下 复制代码

$m = new Mail();
$m->setToaddr($this->temp['receipt_address']);
$m->setTopic($this->temp['mail_title']);
$m->setContent($this->temp['mail_content']);
$m->setFromaddr($_SESSION['user']['name'].' <'.$_SESSION['user']['name'].'@'.SystemDomain.'>');
$m->setDomain('@'.SystemDomain);
$m->setCc($this->temp['cc_address']);
$m->setAttach(PATH.'/temp/'.$this->temp['attachment_file']);
$m->send();

优点:使用方便就一个简单的函数
缺点:需要php.ini支持该函数,如果某些服务器不支持而又不能改环境那就不行了而且总是不稳定,发的有时能收到有时不能

时间: 2024-08-26 19:24:27

利用php中mail函数发送带有附件的邮件的相关文章

利用php中mail函数发送HTML邮件实例

例  代码如下 复制代码 <?php function send_mail($from, $to, $subject, $message) {     if ($from == "")     {         $from = '回忆未来 <webmaster@s135.com>';//发件人地址     }     $headers = 'MIME-Version: 1.0' . "rn";     $headers .= 'Content-t

php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法_php技巧

最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的.最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在.   1.使用 PEAR 的 Mail 类 PEAR 的 Mail 类可以让你选择使用 sendmail 或者 SMTP 方式发信,这样的包装好的接口很好用,你没有必要去自己重新发明轮子. 2.关于headers 中subject 的乱码 不要把任何除了 iso-8859-1 编码之外的

php中mail函数发送文件失败解决办法

之前的那台服务器转移过来后,发现网站用mail()发送邮件发不了.但是发现sendmail明明已经安装. 如果没有安装sendmail可以执行  代码如下 复制代码 yum install sendmail 对了,主机名要设置一个域名格式的.例如:sangsir.com 不然重启sendmail的时候会特别久才启动起来.(否则要等很久)    代码如下 复制代码 hostname sangsir.com 接着看看sendmail是否正常运行    代码如下 复制代码 service sendma

【主机】sendEmail发送带有附件的邮件

需求:     从数据库获取数据并保存为CSV的文件,并把数据文件通过sendEmail以附件的形式发送到指定邮箱.     这里主要利用sendEmail的: -m 邮件内容  -a 邮件附件 这两个参数来完成此工作. shell 脚本: #!/bin/sh #function: get data from database and keep them in csv mode  #then send the log and the csv file to var $TO #author: ya

ubuntu下发送带有附件的邮件的例子

一台服务器检查程序需要将检查结果自动发送给指定邮箱,想当然的使用mail命令,结果在测试时发现有错误: mail: cannot send message: process exited with a non-zero status 其没有提到明确的问题,但从其中提出的log查看,倒是发现了线索. 有两个log都记录了对应的信息,即syslog和mail.err.mail.err中的信息更明确: postfix/sendmail[27115]: fatal: open /etc/postfix/

用vbs发送带附件的邮件_vbs

function Send_mail(You_Account,You_Password,Send_Email,Send_Email2,Send_Topic,Send_Body,Send_Attachment)  'code by NetPatch 'VBS发送邮件参数说明 'You_Account:你的邮件帐号 'You_Password:你的邮件密码 'Send_Email: 主要邮件地址 'Send_Email2: 备用邮件地址 'Send_Topic: 邮件主题 'Send_Body:  

php mail函数发送电子邮件(可带附件)

(可带附件<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"  代码如下 复制代码 "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equi

java中javamail发送带附件的邮件实现方法_java

本文实例讲述了java中javamail发送带附件的邮件实现方法.分享给大家供大家参考.具体分析如下: JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输,JavaMail是可选包,因此如果需要使用的话你需要首先从java官网上下载.目前最新版本是JavaMail1.5.0,下面我们来看看javamail发送带附件的邮件实例 mail.java 代码: 复制代码 代码如下: package mail;

Codeigniter实现发送带附件的邮件

 这篇文章主要介绍了Codeigniter实现发送带附件的邮件的方法,涉及Codeigniter中attach方法的使用技巧,非常具有实用价值,需要的朋友可以参考下     本文实例讲述了Codeigniter实现发送带附件的邮件的方法.分享给大家供大家参考.具体分析如下: attach() 方法允许你的发邮件时带上附件,下面是演示代码 代码如下: $this->load->library('email'); $this->email->from('w3@w3mentor.com'