• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 发送带附件邮件示例_php实例

php 搞代码 3年前 (2022-01-26) 50次浏览 已收录 0个评论

emailclass.php

 <BR><? <BR>class CMailFile { <br><br>var $subject; <BR>var $addr_to; <BR>var $text_body; <BR>var $text_encoded; <BR>var $mime_headers; <BR>var $mime_boundary = "--==================_846811060==_"; <BR>var $smtp_headers; <br><br>function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype = "application/octet-stream",$mime_filename = false) { <BR>$this->subject = $subject; <BR>$this->addr_to = $to; <BR>$this->smtp_headers = $this->write_smtpheaders($from); <BR>$this->text_body = $this->write_body($msg); <BR>$this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename); <BR>$this->mime_headers = $this->write_mimeheaders($filename, $mime_filename); <BR>} <br><br>function attach_file($filename,$downfilename,$mimetype,$mime_filename) { <BR>$encoded = $this->encode_file($filename); <BR>if ($mime_filename) $filename = $mime_filename; <BR>$out = "--" . $this->mime_boundary . "\n"; <BR>$out = $out . "Content-type: " . $mimetype . "; name=\"$filename\";\n"; <BR>$out = $out . "Content-Transfer-Encoding: base64\n"; <BR>$out = $out . "Content-disposition: attachment; filename=\"$downfilename\"\n\n"; <BR>$out = $out . $encoded . "\n"; <BR>$out = $out . "--" . $this->mime_boundary . "--" . "\n"; <BR>return $out; <BR>} <br><br>function encode_file($sourcefile) { <BR>if (is_readable($sourcefile)) { <BR>$fd = fopen($sourcefile, "r"); <BR>$contents = fread($fd, filesize($sourcefile)); <BR>$encoded = chunk_split(base64_encode($contents)); <BR>fclose($fd); <BR>} <BR>return $encoded; <BR>} <br><br>function sendfile() { <BR>$headers = $this->smtp_headers . $this->mime_headers; <BR>$message = $this->text_body . $this->text_encoded; <BR><div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>mail($this->addr_to,$this->subject,$message,$headers); <BR>} <br><br>function write_body($msgtext) { <BR>$out = "--" . $this->mime_boundary . "\n"; <BR>$out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n"; <BR>$out = $out . $msgtext . "\n"; <BR>return $out; <BR>} <br><br>function write_mimeheaders($filename, $mime_filename) { <BR>if ($mime_filename) $filename = $mime_filename; <BR>$out = "MIME-version: 1.0\n"; <BR>$out = $out . "Content-type: multipart/mixed; "; <BR>$out = $out . "boundary=\"$this->mime_boundary\"\n"; <BR>$out = $out . "Content-transfer-encoding: 7BIT\n"; <BR>$out = $out . "X-attachments: $filename;\n\n"; <BR>return $out; <BR>} <br><br>function write_smtpheaders($addr_from) { <BR>$out = "From: $addr_from\n"; <BR>$out = $out . "Reply-To: $addr_from\n"; <BR>$out = $out . "X-Mailer: PHP3\n"; <BR>$out = $out . "X-Sender: $addr_from\n"; <BR>return $out; <BR>} <BR>} <br><br>/*用法 - 例如:mimetype 为 "image/gif" <BR>$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype); <BR>$mailfile->sendfile(); <br><br>$subject -- 主题 <BR>$sendto -- 收信人地址 <BR>$replyto -- 回复地址 <BR>$message -- 信件内容 <BR>$filename -- 附件文件名 <BR>$downfilename -- 下載的文件名 <BR>$mimetype -- mime类型 <BR>*/ <BR>?> <BR>


Demo

 <BR><?php <BR>require_once('emailclass.php'); <br><br>//发送邮件 <br><br>//主題 <BR>$subject = "test send email"; <br><br>//收件人 <BR>$sendto = '[email protected]'; <br><br>//發件人 <BR>$replyto = '[email protected]'; <br><br>//內容 <BR>$message = "test send email content"; <br><br>//附件 <BR>$filename = 'test.jpg'; <br><br>//附件類別 <BR>$mimetype = "image/jpeg"; <br><br>$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype); <BR>$mailfile->sendfile(); <BR>?> <BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 发送带附件邮件示例_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址