{"id":2662,"date":"2018-09-28T16:23:14","date_gmt":"2018-09-28T08:23:14","guid":{"rendered":"https:\/\/www.webteach.tw\/?p=2662"},"modified":"2018-09-28T16:23:14","modified_gmt":"2018-09-28T08:23:14","slug":"php-phpmailer-%e9%99%84%e4%bb%b6-%e5%af%84%e5%a4%9a%e5%80%8bmail-%e5%87%bd%e5%bc%8f%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.webteach.tw\/?p=2662","title":{"rendered":"[ PHP ] \u2013 PHPMailer \u9644\u4ef6 \u5bc4\u591a\u500bMAIL \u51fd\u5f0f\u7bc4\u4f8b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">PHPMailer \u9644\u4ef6 \u5bc4\u591a\u500bMAIL \u51fd\u5f0f\u7bc4\u4f8b<\/p>\n\n\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u5c08\u6848\u5e38\u5e38\u7528\u5230\uff0c\u6240\u4ee5\u5beb\u4e86\u500b\u7c21\u55ae\u7684\u5c0f\u51fd\u5f0f \u628a\u5bc4\u6a94\u6848\u9644\u4ef6\u53ca \u540c\u6642 CC \u7d66\u591a\u4eba\u7684\u529f\u80fd\u90fd\u5beb\u9032\u4f86\u3002<\/p>\n\n\n<pre class=\"wp-block-code\"><code>if (!function_exists('class_sendMail')) {\nfunction class_sendMail($subject, $html, $user,$file='',$recipient=''){\n\/\/\u5176\u5b83\u6536\u4ef6\u4eba\n$recipient_ar = explode(',',$recipient);\/\/\u4ee5\u9017\u865f\u5206\u5272\n$mail = new PHPMailer;\n$mail-&gt;isSMTP();\/\/ Set mailer to use SMTP\n$mail-&gt;Host = 'mail.xxx.com.tw';\/\/\u60a8\u7684 mail server \u4f4d\u7f6e\n$mail-&gt;SMTPAuth = true;\n$mail-&gt;Username = '\u5e33\u865f';\n$mail-&gt;Password = '\u5bc6\u78bc';\n$mail-&gt;SMTPSecure = false;\/\/ Enable TLS encryption, `ssl` also accepted\n$mail-&gt;SMTPAutoTLS = false;\n$mail-&gt;CharSet = \"UTF-8\";\n$mail-&gt;Port = 25; \/\/ TCP port to connect to\n$mail-&gt;setFrom('\u5bc4\u4ef6\u4fe1\u7bb1', '\u5bc4\u4ef6\u8005');\n$mail-&gt;addAddress($user);\/\/ Add a recipient\nforeach($recipient_ar as $value){\n$mail-&gt;addAddress($value);\/\/ Add a recipient\n}\n$mail-&gt;isHTML(true);\/\/ Set email format to HTML\nif($file!=''){\n$mail-&gt;AddAttachment('\u6a94\u6848\u8def\u5f91'.$file); \/\/ \u8a2d\u5b9a\u9644\u4ef6\u6a94\u6a94\u540d \u6ce8\u610f\u6b64\u7bc4\u4f8b $file \u53ea\u662f\u6a94\u540d\uff0c\u82e5\u4f60\u6a94\u6848\u6709\u5206\u591a\u5c64\u8acb\u9023\u6a94\u6848\u8def\u5f91\u4e00\u540c\u5beb\u5728 $file \u4e26\u79fb\u9664 '\u6a94\u6848\u8def\u5f91'. \u5b57\u4e32\n}\n$mail-&gt;Subject = $subject;\n$mail-&gt;Body = $html;\nif(!$mail-&gt;send()) {return false;} else { return true;}\n}\n}\n\/\/\u8f09\u5165\u5bc4\u4fe1class\nrequire_once \".\/plugins\/PHPMailer\/PHPMailerAutoload.php\";\nrequire_once \".\/plugins\/PHPMailer\/class.phpmailer.php\";\nrequire_once \".\/plugins\/PHPMailer\/class.smtp.php\";\n$html ='&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\"&gt;\n&lt;title&gt;\u4fe1\u4ef6\u6a19\u984c &lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;';\n$html .= '&lt;p&gt;\u4fe1\u4ef6\u5167\u5bb9&lt;\/p&gt;';\n$html .= '&lt;\/body&gt;&lt;\/html&gt;';\n$user ='\u6536\u4ef6\u8005mail';\n$subject = '\u5bc4\u4ef6\u6a19\u984c';\n$from = '\u5bc4\u4ef6\u8005mial';\n$headers = \"Content-type: text\/html; charset=utf-8\\r\\n\" .\"From: $from\\r\\n\";\n$recipient='a@test.com.tw,b@test.com.tw,c@test.com.tw';\/\/\u9664\u4e86\u672c\u4f86\u7684\u6536\u4ef6\u8005 \u9084\u8981\u5bc4\u7d66\u9019\u4e9b\u6536\u4ef6\u8005\u6642\u586b\uff0c\u7121\u8acb\u8a2d\u70ba\u7a7a\u5b57\u4e32\nclass_sendMail($subject,$html,$user,'',$recipient);<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHPMailer \u9644\u4ef6 \u5bc4\u591a\u500bMAIL \u51fd\u5f0f\u7bc4\u4f8b\u6700\u8fd1\u5c08\u6848\u5e38\u5e38\u7528\u5230\uff0c\u6240\u4ee5\u5beb\u4e86\u500b\u7c21\u55ae\u7684\u5c0f\u51fd\u5f0f \u628a\u5bc4\u6a94\u6848\u9644\u4ef6\u53ca \u540c\u6642 CC \u7d66\u591a\u4eba\u7684\u529f\u80fd\u90fd\u5beb\u9032\u4f86\u3002<\/p>\n","protected":false},"author":1,"featured_media":2201,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[156,448,456,1252],"class_list":["post-2662","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-cc","tag-php","tag-phpmailer","tag-1252","hvn-theme-has-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/2662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2662"}],"version-history":[{"count":0,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/2662\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/media\/2201"}],"wp:attachment":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}