phpmail的使用

來源:互聯網
上載者:User

標籤:asc   格式   phone   公司   姓名   put   match   err   list   

    在一些活動的表單提交過程中我們可能需要提交一些資訊並郵件到相應的伺服器平台,對於在pc端的前端提交一般我們使用mailto屬性,簡潔適應,但是在移動端可能存在裡面不支援mailto屬性的問題,所以一般改在後台發送郵件到企業郵箱伺服器裡面。
1.在前端發送郵件:

url = "mailto:rsvp<[url=mailto:[email protected]][email protected][/url]>[email protected]&subject=港股盛世投資峰會在線報名&"; url += "body=" + "姓名: " + _name + "<br>" +     "公司或機構名稱:" + _company + "<br>" +     "您的職位:" + _job + "<br>" +     "手機:" + _phone + "<br>" +     "Emali:" + _email + "<br>" +     "(如有):" + _weixin; url = encodeURI(url); window.location.href = 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‘] = "[email protected],[url=mailto:[email protected]][email protected][/url]";     //$info[‘mailto‘] = "[email protected],[url=mailto:[email protected]][email protected][/url],[url=mailto:[email protected]][email protected][/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的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.