首页 > 编程 > PHP > 正文

如何使用php中smtp发送支持附件的邮件

2020-03-22 17:11:30
字体:
来源:转载
供稿:网友
这篇文章主要介绍了php使用smtp发送支持附件的邮件示例,需要有smtp服务器,代码经过多次实战使用,需要的朋友可以参考下

轻量级PHP邮件发送,需要有smtp服务器,代码经过多次实战使用,现在把代码分享给大家

 ?php邮件发送smtp服务联结smtp服务器,进行邮件发送,版权所有,不能复制@author:jackbrown;@qq: 610269963 @time:2011-8-20;@version:1.0.3;html' target='_blank'>class smtp{ /*邮件用户名*/ public $mailUser = MAIL_USER; /*邮件密码*/ public $mailPwd = MAIL_PWD; /*邮件服务器地址*/ public $server = MAIL_SMTP_HOST; /*邮件端口*/ public $port = MAIL_SMTP_PORT; public $timeout = MAIL_TIMEOUT; /*邮件编码*/ public $charset = MAIL_CHARSET; /*邮件发送者email,用于显示给接收者*/ public $senderMail = MAIL_SENDER; /*发用者名称*/ public $senderName = MAIL_SENDER_NAME; /*是否使用ssl安全操作*/ public $useSSL = IN_SSL; /*是否显示错误信息*/ public $showError = MAIL_SHOW_ERR; public $needLogin = MAIL_NEED_LOGIN; /*附件数组*/ public $attachMent = array(); public $failed = false; private static $smtpCon; private $stop = /r/n  private $status = 0;
$msg = base64_encode($msg); $msg = str_replace($this- stop . . , $this- stop . .. , $msg); $msg = substr($msg, 0, 1) == . ? . . $msg : $msg; if($this- attachMent!=null){ $headers = $this- mimeHeader($msg,$to,$title); $this- sendSmtpMsg($headers,false); }else{ $headers = $this- mailHeader($to,$title); $this- sendSmtpMsg($headers,false); $this- sendSmtpMsg( ,false); $this- sendSmtpMsg($msg,false); $this- sendSmtpMsg( . //发送结束标识符 if($this- status != 250 ){ $this- failed = true; $this- error($this- readSmtpMsg()); return false; return true; 关闭邮件连接 public function close(){ $this- sendSmtpMsg( Quite @socket_close(self::$smtpCon); 添加普通邮件头信息 protected function mailHeader($to,$title){ $headers = array(); $headers[] = Date: .$this- gmtime( D j M Y H:i:s ). .date( O if(!is_array($to)){ $headers[] = To: . =? .$this- charset. ?B? .base64_encode($this- getMailUser($to)). ?= .$to. }else{ foreach($to as $k= $e){ $headers[] = To: . =? .$this- charset. ?B? .base64_encode($this- getMailUser($e)). ?= .$e. $headers[] = From: =? .$this- charset. ?B? .base64_encode($this- senderName). ?= .$this- senderMail. $headers[] = Subject: =? .$this- charset. ?B? .base64_encode($title). ?= $headers[] = Content-type: text/html; charset= .$this- charset. format=flowed $headers[] = Content-Transfer-Encoding: base64 $headers = str_replace($this- stop . . , $this- stop . .. , trim(implode($this- stop, $headers))); return $headers; 带付件的头部信息 protected function mimeHeader($msg,$to,$title){ if($this- attachMent!=null){ $headers = array(); $boundary = ----= .uniqid(); $headers[] = Date: .$this- gmtime( D j M Y H:i:s ). .date( O if(!is_array($to)){ $headers[] = To: . =? .$this- charset. ?B? .base64_encode($this- getMailUser($to)). ?= .$to. }else{ foreach($to as $k= $e){ $headers[] = To: . =? .$this- charset. ?B? .base64_encode($this- getMailUser($e)). ?= .$e. $headers[] = From: =? .$this- charset. ?B? .base64_encode($this- senderName). ?= .$this- senderMail. $headers[] = Subject: =? .$this- charset. ?B? .base64_encode($title). ?= $headers[] = Mime-Version: 1.0 $headers[] = Content-Type: multipart/mixed;boundary= .$boundary. .$this- stop; $headers[]= -- .$boundary; $headers[]= Content-Type: text/html;charset= .$this- charset. $headers[]= Content-Transfer-Encoding: base64 .$this- stop; $headers[] = $headers[]= $msg.$this- stop; foreach($this- attachMent as $k= $filename){ $f = @fopen($filename, r $mimetype = $this- getMimeType(realpath($filename)); $mimetype = $mimetype == ? application/octet-stream : $mimetype; $attachment = @fread($f, filesize($filename)); $attachment = base64_encode($attachment); $attachment = chunk_split($attachment); $headers[] = -- . $boundary; $headers[] = Content-type: .$mimetype. name=/ =? .$this- charset. ?B? . base64_encode(basename($filename)). ?= $headers[] = Content-disposition: attachment; name=/ =? .$this- charset. ?B? . base64_encode(basename($filename)). ?= $headers[] = Content-Transfer-Encoding: base64 .$this- stop; $headers[] = $attachment.$this- stop;
$headers[] = -- . $boundary . -- $headers = str_replace($this- stop . . , $this- stop . .. , trim(implode($this- stop, $headers))); return $headers; 获取返回状态 protected function getStatus(){ $this- status = substr($this- readSmtpMsg(),0,3);
$mimes = array( chm = application/octet-stream , ppt = application/vnd.ms-powerpoint , xls = application/vnd.ms-excel , doc = application/msword , exe = application/octet-stream , rar = application/octet-stream , js = javascrīpt/js , css = text/css , hqx = application/mac-binhex40 , bin = application/octet-stream , oda = application/oda , pdf = application/pdf , ai = application/postsrcipt , eps = application/postsrcipt , es = application/postsrcipt , rtf = application/rtf , mif = application/x-mif , csh = application/x-csh , dvi = application/x-dvi , hdf = application/x-hdf , nc = application/x-netcdf , cdf = application/x-netcdf , latex = application/x-latex , ts = application/x-troll-ts , src = application/x-wais-source , zip = application/zip , bcpio = application/x-bcpio , cpio = application/x-cpio , gtar = application/x-gtar , shar = application/x-shar , sv4cpio = application/x-sv4cpio , sv4crc = application/x-sv4crc , tar = application/x-tar , ustar = application/x-ustar , man = application/x-troff-man , sh = application/x-sh , tcl = application/x-tcl , tex = application/x-tex , texi = application/x-texinfo , texinfo = application/x-texinfo , t = application/x-troff , tr = application/x-troff , roff = application/x-troff , shar = application/x-shar , me = application/x-troll-me , ts = application/x-troll-ts , gif = image/gif , jpeg = image/pjpeg , jpg = image/pjpeg , jpe = image/pjpeg , ras = image/x-cmu-raster , pbm = image/x-portable-bitmap , ppm = image/x-portable-pixmap , xbm = image/x-xbitmap , xwd = image/x-xwindowdump , ief = image/ief , tif = image/tiff , tiff = image/tiff , pnm = image/x-portable-anymap , pgm = image/x-portable-graymap , rgb = image/x-rgb , xpm = image/x-xpixmap , txt = text/plain , c = text/plain , cc = text/plain , h = text/plain , html = text/html , htm = text/html , htl = text/html , rtx = text/richtext , etx = text/x-setext , tsv = text/tab-separated-values , mpeg = video/mpeg , mpg = video/mpeg , mpe = video/mpeg , avi = video/x-msvideo , qt = video/quicktime , mov = video/quicktime , moov = video/quicktime , movie = video/x-sgi-movie , au = audio/basic , snd = audio/basic , wav = audio/x-wav , aif = audio/x-aiff , aiff = audio/x-aiff , aifc = audio/x-aiff , swf = application/x-shockwave-flash , myz = application/myz $ext = substr(strrchr($file, . ),1); $type = $mimes[$ext];
if($this- status!= 334 ){ $this- error( 邮件登录用户名可能不正确! .$this- readSmtpMsg()); return false; $this- sendSmtpMsg(base64_encode($this- mailPwd)); if($this- status != 235 ){ $this- error( 邮件登录密码可能不正确! return false; return true; private function getMailUser($to){ $temp = explode( @ ,$to); return $temp[0]; 异常报告 private function error($exception){ if($this- showError == false){ file_put_contents( mail_log.txt ,$exception,FILE_APPEND); return; if(class_exists( error ) is_object($GLOBALS[ error ])){ $GLOBALS[ error ]- showErrorStr($exception, javascript: ,false); }else{ throw new Exception($exception);// 使用示例 ini_set( memory_limit , 128M set_time_limit(120);define( MAIL_SENDER_NAME , 楚贤 define( MAIL_SMTP_HOST , smtp.ym.163.com define( MAIL_USER , admin@myxxxx.com define( MAIL_SENDER , admin@myxxxx.com define( MAIL_PWD , xxxx define( MAIL_SMTP_PORT ,25);define( IN_SSL ,false);define( MAIL_TIMEOUT ,10);define( MAIL_CHARSET , utf-8 date_default_timezone_set( PRC $m = new smtp();$msg = 有用户登录服务器@ .date( Y-m-d H:i:s //$m- attachMent = array( hehe.php , common.php if($m- sendMail(array( 610269963@qq.com ),$msg, 88服务器登录提示 )){ echo 发送成功! $m- close();?

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP !

相关推荐:

使用laravel sms 构建短信验证码发送校验的功能

如何解决PHP中Fatal error session_start()的错误

关于PHP的autoLoad自动加载机制的分析

以上就是如何使用php中smtp发送支持附件的邮件的详细内容,PHP教程

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表