phpmailer 發送圖片


 

php對郵箱地址進行檢測

 1 function PageIsMail(array $email_arr){
 2     foreach($email_arr as $email) {
 3         //郵箱正則匹配(不能以-_.開頭)
 4         if (!preg_match("/^[0-9A-Za-z][0-9A-Za-z-_.]*@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i", $email)){
 5             echo "(method1)invalid email: $email<br />";
 6             continue;
 7         }
 8         //名稱在4到16位之間
 9         if (strlen(explode("@",$email)[0])<4 or strlen(explode("@",$email)[0])>16 ){
10             echo "(method2)invalid email: $email<br />";
11             continue;
12         }
13         //域名是否存在(依賴網絡,不建議使用)
14         if(checkdnsrr(explode("@",$email)[1],"MX") === false) {
15             echo "(methodof3)invalid email: $email<br />";
16             continue;
17         }
18         echo "email: $email<br />";
19     }
20 }

 

 

phpmailer 發送帶圖片的郵件

原理:

  1、將html中的圖片文件(本地直接添加,網絡圖片則需要下載)加載為郵件附件$mail->addEmbeddedImage(pic_path, 附件id)

  2、將html中的圖片代碼替換成<img src="cid:添加附件時的id" />

直接上代碼

 1 // 發送郵件
 2 function SendMail(){
 3     require_once("./class.phpmailer.php");
 4     $mail = new PHPMailer();
 5     //$mail->SMTPDebug = 3;
 6     $this->mail=$mail;
 7     $this->mail->isSMTP();
 8     $this->mail->CharSet = 'UTF-8';
 9     $this->mail->Host = 'smtp.exmail.qq.com';
10     $this->mail->SMTPAuth = true;
11     $mail->Username = 'xxx@xx.com';
12     $mail->Password = 'pwd';
13     $this->mail->SMTPSecure = 'ssl';
14     $this->mail->Port = 465;
15     $this->mail->From = 'xxx@xx.com';
16     $this->mail->addAddress('yyy@yy.com','toName');
17     $this->mail->isHTML(true);
18     $this->mail->Subject = '中文主題測試';
19     $hmtl = '<i>帥哥</i>:<b>你好</b>!!!<br><img src="./static/pic/xx.png" /><br><img src="./static/pic/xx.png" />';
20     //帶圖處理,不帶圖不處理,圖片格式如上(圖片未加格式)
21     $this->mail->Body = ProcessImg($hmtl);
22     $this->mail->AltBody = strip_tags($hmtl);
23     if(!$this->mail->send()) {
24         echo 'Message could not be sent.';
25         echo 'Mailer Error: ' . $this->mail->ErrorInfo;
26     } else {
27         echo 'Message has been sent';
28     }
29 }
30 
31 // 處理MailBody中的圖片,將本地圖片加入附件方可發送成功
32 function ProcessImg($html){
33     $resutl = preg_match_all('@<img src=.+? />@', $html, $matches);
34     if(!$resutl) return $html;
35     $trans = array();
36     foreach ($matches[0] as $key => $img) {
37         $id = 'img' . $key;
38         preg_match('/src="(.*?)"/', $html, $path);
39         if ($path[1]){
40             $this->mail->addEmbeddedImage($path[1], $id);
41             $trans[$img] = '<img src="cid:' . $id . '" />';
42         }
43     }
44     $html = strtr($html, $trans);
45     return $html;
46 }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM