首页 > 代码库 > phpmail的使用

phpmail的使用

    在一些活动的表单提交过程中我们可能需要提交一些信息并邮件到相应的服务器平台,对于在pc端的前端提交一般我们使用mailto属性,简洁适应,但是在移动端可能存在微信里面不支持mailto属性的问题,所以一般改在后台发送邮件到企业邮箱服务器里面。
1.在前端发送邮件:

url = "mailto:rsvp<[url=mailto:rsvp@lbs-comm.com]rsvp@lbs-comm.com[/url]>?cc=listco@cnfol.hk&subject=港股盛世投資峰會在線報名&"; url += "body=" + "姓名: " + _name + "<br>" +     "公司或機構名稱:" + _company + "<br>" +     "您的職位:" + _job + "<br>" +     "手機:" + _phone + "<br>" +     "Emali:" + _email + "<br>" +     "微信(如有):" + _weixin; url = encodeURI(url); window.location.href = http://www.mamicode.com/url;>

  2.后台php发送邮件:
这里需要引用php自带的一个邮件类:phpmail

/*  *  活动栏目页,报名参加,发送邮件接口  */public function applyevent() {     $name = $this->input->post_get(‘name‘);     $company = $this->input->post_get(‘company‘);     $job = $this->input->post_get(‘job‘);     $phone = $this->input->post_get(‘phone‘);     $email = $this->input->post_get(‘email‘);     $weixin = $this->input->post_get(‘weixin‘);     if (! preg_match(‘/^([\x{4e00}-\x{9fa5}]|([a-zA-Z]+\s?)+){1,50}$/u‘,$name)){         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘姓名不符合规范‘));     }     if (! preg_match(‘/^([\x{4e00}-\x{9fa5}]|([a-zA-Z]+\s?)+){1,100}$/u‘,$company)){         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘公司不符合规范‘));     }     if (! preg_match(‘/^([\x{4e00}-\x{9fa5}]|([a-zA-Z]+\s?)+){1,50}$/u‘,$job)){         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘职位不符合规范‘));     }     if ( preg_match(‘/^(1(([34578][0-9])|(47)|[8][0126789]))\d{8}$/u‘,$phone)){         //$phone = ‘0086‘.$phone;     } elseif ( preg_match(‘/^[1-9]\d{7}$/u‘,$phone)){         //$phone = ‘00852‘.$phone;     } else {         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘手机不符合规范‘));     }     if (! preg_match(‘/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/u‘,$email)){         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘邮箱不符合规范‘));     }      if ($weixin && !preg_match(‘/^[a-zA-Z0-9]{1,50}$/u‘,$weixin)) {         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘微信号不符合规范‘));     }      //防刷3秒, 10次     //$this->_ajaxRequestCheck(strtolower(__FUNCTION__), 10, 3);      $info[‘subject‘] = ‘港股盛世投资峰会在线报名‘;     $info[‘content‘] = "姓 名 : $name <br>                         公 司 : $company <br>                         职 位 : $job <br>                         手 机 : $phone <br>                         邮 箱 : $email <br>                         微 信 : $weixin";     $info[‘from‘] = ‘香港站活动栏目后台‘;     $info[‘mailto‘] = "rsvp@lbs-comm.com,[url=mailto:listco@cnfol.hk]listco@cnfol.hk[/url]";     //$info[‘mailto‘] = "jiangzh@cnfol.net,[url=mailto:liuwei@cnfol.net]liuwei@cnfol.net[/url],[url=mailto:hongxx@cnfol.net]hongxx@cnfol.net[/url]";     if (send_mail($info)) {         $this->returnJson(array(‘flag‘ => ‘1‘, ‘msg‘ => ‘发送成功‘));     } else {         $this->returnJson(array(‘flag‘ => ‘0‘, ‘msg‘ => ‘发送失败‘));     } } 

引入的phpmail这个类:

class CI_Sendmail {           public function send($info, $action=‘post‘) {         $url     = ‘http://mail.api.cnfol.net/index.php‘;         $ydUrl     = ‘http://mail.api.cnfol.net/index_yd.php‘;         $key     = ‘da2f00b38ed9273b974f254b7ba27571‘;         $emails  = $info[‘mailto‘];//用户邮件地址         $subject = trim($info[‘subject‘]);//邮件标题         $content = trim($info[‘content‘]);//邮件内容         $charset = (isset($info[‘charset‘]) && $info[‘charset‘]) ? $info[‘charset‘] : ‘utf8‘;//编码         $from    = (isset($info[‘from‘]) && $info[‘from‘]) ? $info[‘from‘] : ‘passport‘;//来源系统         $isyd    = (isset($info[‘isyd‘]) && $info[‘isyd‘]) ? 1 : 0; //是否异地登录                   if($subject<>‘‘ && $content<>‘‘) {             $emails = str_replace(‘,‘, ‘,‘, $emails);//全角转半角             $emails = explode(‘,‘, $emails);             $emailto = ‘‘;             foreach($emails as $v) {                 if(!empty($v) && preg_match(‘/^[\w\-\.]+\@[\w\-\.]+[A-Za-z]{2,}$/‘, $v)) {                     $emailto .= trim($v).‘,‘;                 } else {                     @error_log(date(‘H:i:s‘).PHP_EOL.__FILE__.‘//‘.__LINE__.PHP_EOL.‘发送邮件,邮箱:‘.$v.‘格式不符合要求‘.PHP_EOL, 3, LOG_PATH . ‘/sendmail‘.date(‘Ymd‘).‘.log‘);                 }             }             $emailto = substr($emailto, 0, -1);                           if($emailto<>‘‘) {                                   $info[‘key‘]      = $key;                 $info[‘content‘]  = $content;                 $info[‘mailto‘]   = $emailto;                 $info[‘subject‘]  = $subject;                 $info[‘Original‘] = $from;                 $info[‘charset‘]  = $charset;                 $info[‘smtpID‘]   = rand(24,26);                    $sendUrl = $isyd ? $ydUrl : $url;                 $result = curl_post($sendUrl, $info);                 @error_log(date(‘H:i:s‘). ‘|sendUrl|‘ . $sendUrl . PHP_EOL . ‘|info|‘. print_r($info, true). PHP_EOL . ‘|rs|‘ . print_r($result,true) . PHP_EOL, 3, LOG_PATH . ‘/sendmail‘.date(‘Ymd‘).‘.log‘);                                                     if($result == ‘errid=0&msg=发送成功‘) {                     return true;                 }                 else                {                     return false;                 }             }         }         else        {             return false;         }     }                     }// END Sendmail Class

在前端页面html页面的处理:

_sendMail({             name: _name,             company: _company,             job: _job,             phone: _phone,             email: _email,             weixin: _weixin         });     }     var _sendMail = function (info) {         var mailto = "http://logon.cnfol.hk/mail/applyevent";         $.ajax({             url: mailto,             type: "POST",             data: {                 name: info.name,                 company: info.company,                 job: info.job,                 phone: info.phone,                 email: info.email,                 weixin: info.weixin             },             dataType: "jsonp",             jsonp: ‘callback‘,             beforeSend:function () {                 $(‘.mailto‘).attr(‘onclick‘,‘javascript:void();‘);             },             success: function (data) {                 if (data.flag == 1) {                     alert(‘发送成功‘);                     $(‘form‘)[0].reset();                     $(‘.mailto‘).attr(‘onclick‘,"javascript:alert(‘您已成功参与本次报名!‘);");                 } else {                     alert(data.msg);                     $(‘.mailto‘).attr(‘onclick‘,‘sendMail()‘);                 }             },             error: function (data) {                 alert(‘短信发送出现故障‘);                 $(‘.mailto‘).attr(‘onclick‘,‘sendMail()‘);             }         });     } 

 

phpmail的使用