{"id":619,"date":"2023-02-26T11:00:49","date_gmt":"2023-02-26T03:00:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=619"},"modified":"2023-04-29T20:18:05","modified_gmt":"2023-04-29T12:18:05","slug":"phpmailer-solve-opencart-email-sending-failure","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/phpmailer-solve-opencart-email-sending-failure\/","title":{"rendered":"PHPMailer\u89e3\u51b3OpenCart\u53d1\u9001\u90ae\u4ef6\u4e0d\u6210\u529f"},"content":{"rendered":"<p>\u5f88\u591a\u4eba\u53cd\u6620Opencart\u540e\u53f0\u914d\u7f6e\u90ae\u4ef6\u534f\u8bae\u4e3aMAIL\u548cSMTP\u53d1\u9001\u90ae\u4ef6\u90fd\u4e0d\u6210\u529f\uff0c\u5b98\u65b9\u548c\u4e2d\u6587\u8bba\u575b\u4e0a\u4e5f\u6709\u4eba\u53cd\u6620\u8fd9\u4e2a\u95ee\u9898\uff0c\u672c\u6587\u7d22\u6027\u628aOpencart\u4e2d\u53d1\u9001\u90ae\u4ef6\u7684\u7c7b\u5e93\u66ff\u6362\u6210PHPMailer\uff0c\u7ecf\u6d4b\u8bd5\u5b8c\u7f8e\u89e3\u51b3\u3002<\/p>\n<ol>\n<li>\u4e0b\u8f7d<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/code.google.com\/a\/apache-extras.org\/p\/phpmailer\/downloads\/list\" title=\"PHPMailer\">PHPMailer<\/a>\uff0c\u89e3\u538b\u4ee5\u540e\uff0c\u5c06\u91cc\u9762\u7684 <code>class.phpmailer.php<\/code> \u548c <code>class.smtp.php<\/code> \u590d\u5236\u5230Opencart\u7684system\/library\/PHPMailer \u76ee\u5f55\u4e0b\u9762<\/li>\n<\/ol>\n<p><!-- more --><\/p>\n<ol start=\"2\">\n<li>\u4fee\u6539system\/startup.php\uff0c\u6ce8\u91ca\u6389\u539f\u6765\u5f15\u5165mail\u7c7b\u4ee3\u7801\uff0c\u6dfb\u52a0\u5f15\u5165class.phpmailer.php<\/li>\n<\/ol>\n<pre><code class=\"language-php\">\/\/\u7528phpmailer\u4ee3\u66ff\u7cfb\u7edf\u81ea\u5e26\u7684mail\u548csmtp\u7684\u53d1\u9001\u90ae\u4ef6\u65b9\u5f0f\nrequire_once(DIR_SYSTEM . &#039;library\/PHPMailer\/class.phpmailer.php&#039;);\n\/\/require_once(DIR_SYSTEM . &#039;library\/mail.php&#039;);<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728Opencart\u4e2d\u9700\u8981\u53d1\u9001\u90ae\u4ef6\u7684\u4ee3\u7801\u6bb5\u4e2d\uff0c\u5c06\u539f\u6765new Mail()\u7684\u53d1\u9001\u90ae\u4ef6\u76f8\u5e94\u6ce8\u91ca\u6389\uff0c\u91cd\u5199PHPMailer\u7684\u53d1\u9001\u90ae\u4ef6\u5b9e\u73b0\u65b9\u6cd5\u3002\u6bd4\u5982\u6211\u8fd9\u91cc\u4ee5 admin\/controller\/sale\/contact.php\u4e3a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-php\">foreach ($emails as $email) {\n    \/\/$mail = new Mail();\n    \/\/$mail-&gt;protocol = $this&gt;config&gt;get(&#039;config_mail_protocol&#039;);\n    \/\/$mail-&gt;parameter = $this&gt;config&gt;get(&#039;config_mail_parameter&#039;);\n    \/\/$mail-&gt;hostname = $this&gt;config&gt;get(&#039;config_smtp_host&#039;);\n    \/\/$mail-&gt;username = $this&gt;config&gt;get(&#039;config_smtp_username&#039;);\n    \/\/$mail-&gt;password = $this&gt;config&gt;get(&#039;config_smtp_password&#039;);\n    \/\/$mail-&gt;port = $this&gt;config&gt;get(&#039;config_smtp_port&#039;);\n    \/\/$mail-&gt;timeout = $this&gt;config&gt;get(&#039;config_smtp_timeout&#039;);\n    \/\/$mail-&gt;setTo($email);\n    \/\/$mail-&gt;setFrom($this&gt;config&gt;get(&#039;config_email&#039;));\n    \/\/$mail-&gt;setSender($store_name);\n    \/\/$mail-&gt;setSubject($this&gt;request&gt;post[&#039;subject&#039;]);\n    \/\/\n    \/\/foreach ($attachments as $attachment) {\n    \/\/    $mail-&gt;addAttachment($attachment[&#039;path&#039;], $attachment[&#039;filename&#039;]);\n    \/\/}\n    \/\/\n    \/\/$mail-&gt;setHtml($message);\n    \/\/$mail-&gt;send();\n\n    \/**\n     * \u7528phpmailer \u53d1\u9001\u90ae\u4ef6\n     * @author yuansir\n     *\/\n    ob_start();\n    error_reporting(0);\n    $mail = new PHPMailer();\n    $mail-&gt;CharSet = &quot;UTF-8&quot;;\n    $mail-&gt;IsSMTP();\n    $mail-&gt;Host = $this&gt;config&gt;get(&#039;config_smtp_host&#039;);  \/\/ SMTP server\n    $mail-&gt;SMTPAuth = true;  \/\/ enable SMTP authentication\n    $mail-&gt;Host = $this&gt;config&gt;get(&#039;config_smtp_host&#039;);\n    $mail-&gt;Port = $this&gt;config&gt;get(&#039;config_smtp_port&#039;);\n    $mail-&gt;Username = $this&gt;config&gt;get(&#039;config_smtp_username&#039;);\n    $mail-&gt;Password = $this&gt;config&gt;get(&#039;config_smtp_password&#039;);\n    $mail-&gt;SetFrom($this&gt;config&gt;get(&#039;config_email&#039;), $store_name);  \/\/\u53d1\u4ef6\u4eba\u90ae\u7bb1\u548c\u53d1\u4ef6\u4eba\n    $mail-&gt;Subject = $this&gt;request&gt;post[&#039;subject&#039;];  \/\/\u90ae\u4ef6\u4e3b\u9898\n    $mail-&gt;MsgHTML($message);  \/\/\u90ae\u4ef6\u5185\u5bb9\n    $mail-&gt;AddAddress($email, &quot;&quot;);  \/\/\u6536\u4ef6\u4eba\u90ae\u7bb1\u548c\u6536\u4ef6\u4eba\n    foreach ($attachments as $attachment) {\n        $mail-&gt;AddAttachment($attachment[&#039;path&#039;]);  \/\/\u6dfb\u52a0\u9644\u4ef6\n    }\n    $mail-&gt;Send();  \/\/\u53d1\u9001\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728\u540e\u53f0\u914d\u7f6e\u90ae\u4ef6\u53d1\u9001SMTP\u5373\u53ef\uff0c\u63a5\u4e0b\u6765\u8981\u505a\u7684\u5c31\u662f\u66ff\u6362\u5168\u7ad9\u6d89\u53ca\u5230\u53d1\u9001\u90ae\u4ef6\u7684\u4ee3\u7801\u6bb5\u4e3aPHPMailer\u7684\u4ee3\u7801\uff0c\u5173\u4e8ePHPMailer\u7684\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u53bb\u5b98\u65b9\u67e5\u9605\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u4eba\u53cd\u6620Opencart\u540e\u53f0\u914d\u7f6e\u90ae\u4ef6\u534f\u8bae\u4e3aMAIL\u548cSMTP\u53d1\u9001\u90ae\u4ef6\u90fd\u4e0d\u6210\u529f\uff0c\u5b98\u65b9\u548c\u4e2d\u6587\u8bba\u575b\u4e0a\u4e5f\u6709\u4eba\u53cd\u6620\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[173],"tags":[175],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-opencart","tag-phpmailer"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=619"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/619\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}