I need to send one mail using php code. The mail contails HTML Part, Text , and two attachments (xls) file. How do i set headers and message body . For me Only one is working at a time either mail body or attachment. Please help
1条回答 默认 最新
- duanhaodi4809 2012-09-18 07:20关注
You can try this function, i found here , you can get some other code also if you google
<?php function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $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 different content types here $header .= "Content-Transfer-Encoding: base64 "; $header .= "Content-Disposition: attachment; filename=\"".$filename."\" "; $header .= $content." "; $header .= "--".$uid."--"; if (mail($mailto, $subject, "", $header)) { echo "mail send ... OK"; // or use booleans here } else { echo "mail send ... ERROR!"; } } ?>
And the Usage
$my_file = "somefile.zip"; $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/"; $my_name = "Olaf Lederer"; $my_mail = "my@mail.com"; $my_replyto = "my_reply_to@mail.net"; $my_subject = "This is a mail with attachment."; $my_message = "Hallo, do you like this script? I hope it will help. gr. Olaf";
Function Call:
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
解决 无用评论 打赏 举报
悬赏问题
- ¥15 WPF 大屏看板表格背景图片设置
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示