最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php 邮箱发送附件实例程序
时间:2022-07-02 10:31:20 编辑:袖梨 来源:一聚教程网
class AttachmentMail {
private $from = '[email protected]';
private $from_name = 'Your Name';
private $reply_to = '[email protected]';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';
public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
$this -> to = $to;
$this -> subject = $subject;
$this -> message = $message;
$this -> attachment = $attachment;
$this -> attachment_filename = $attachment_filename;
}
public function mail() {
if (!empty($this -> attachment)) {
$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
$path = dirname($this -> attachment);
$mailto = $this -> to;
$from_mail = $this -> from;
$from_name = $this -> from_name;
$replyto = $this -> reply_to;
$subject = $this -> subject;
$message = $this -> message;
$file = $path.'/'.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail."> ";
$header .= "Reply-To: ".$replyto." ";
$header .= "MIME-Version: 1.0 ";
$header .= "Content-Type: multipart/mixed; boundary="".$uid."" ";
$header .= "This is a multi-part message in MIME format. ";
$header .= "--".$uid." ";
$header .= "Content-type:text/plain; charset=iso-8859-1 ";
$header .= "Content-Transfer-Encoding: 7bit ";
$header .= $message." ";
$header .= "--".$uid." ";
$header .= "Content-Type: application/octet-stream; name="".$filename."" "; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64 ";
$header .= "Content-Disposition: attachment; filename="".$filename."" ";
$header .= $content." ";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
return true;
} else {
return false;
}
} else {
$header = "From: ".($this -> from_name)." <".($this -> from)."> ";
$header .= "Reply-To: ".($this -> reply_to)." ";
if (mail($this -> to, $this -> subject, $this -> message, $header)) {
return true;
} else {
return false;
}
}
}
}
?>
调用方法
require ('Email-Attachment.php');
$sendit = new AttachmentEmail('[email protected]', 'Merry Christmas!', 'Hi', '/home/racker/gift.jpg');
$sendit -> mail();
?>
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21