使用WordPress发送电子邮件的相关PHP函数用法解析
<p>wp_mail() 函数用来发送邮件,类似于 PHP 的 mail() 函数。</p>
<p>
默认的发件人名称是 WordPress,发件人邮箱是类似 wordpress@example.com。<br><strong>用法</strong></p>
<div>
<div>
<div id="highlighter_560524">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>wp_mail( </code><code>$to</code><code>, </code><code>$subject</code><code>, </code><code>$message</code><code>, </code><code>$headers</code><code>, </code><code>$attachments</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>参数</strong></p>
<p>
<strong>$to</strong></p>
<p>
(字符串 | 数组)(必须)收件人。多个收件人可以使用数组或者以逗号分隔邮箱的字符串。</p>
<p>
默认值:None</p>
<p>
<strong>$subject</strong></p>
<p>
(字符串)(必须)邮件标题。</p>
<p>
默认值:None</p>
<p>
<strong>$message</strong></p>
<p>
(字符串)(必须)邮件内容。</p>
<p>
默认值:None</p>
<p>
<strong>$headers</strong></p>
<p>
(字符串 | 数组)(可选)邮件 Headers 信息。可以是数组,也可以是以回车分隔的字符串(格式:name: content)。</p>
<p>
默认值:空字符串</p>
<p>
<strong>$attachments</strong></p>
<p>
(字符串 | 数组)(可选)邮件附件。多个附件可以使用数组或者以回车分隔的字符串。</p>
<p>
默认值:空字符串</p>
<p>
<strong>返回值</strong></p>
<p>
(布尔)返回是否成功发送邮件。</p>
<p>
<strong>例子</strong></p>
<p>
最基本的邮件发送:</p>
<div>
<div>
<div id="highlighter_36483">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>wp_mail( </code><code>'me@example.net'</code><code>, </code><code>'The subject'</code><code>, </code><code>'The message'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
附件和 headers 信息:</p>
<div>
<div>
<div id="highlighter_297904">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
</td>
<td>
<div>
<div>
<code>$attachments</code> <code>= </code><code>array</code><code>( WP_CONTENT_DIR . </code><code>'/uploads/file_to_attach.zip'</code> <code>);</code>
</div>
<div>
<code>$headers</code> <code>= </code><code>'From: My Name <myname@example.com>'</code> <code>. </code><code>"rn"</code><code>;</code>
</div>
<div>
<code>wp_mail( </code><code>'test@example.org'</code><code>, </code><code>'subject'</code><code>, </code><code>'message'</code><code>, </code><code>$headers</code><code>, </code><code>$attachments</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
Html 邮件和一次发送多个邮箱:</p>
<div>
<div>
<div id="highlighter_813593">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
</td>
<td>
<div>
<div>
<code>$multiple_to_recipients</code> <code>= </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'recipient1@example.com'</code><code>,</code>
</div>
<div>
<code> </code><code>'recipient2@foo.example.com'</code>
</div>
<div>
<code>)</code>
</div>
<div>
<code>wp_mail( </code><code>$multiple_to_recipients</code><code>, </code><code>'The subject'</code><code>, </code><code>'<p>The <em>HTML</em> message</p>'</code><code>, </code><code>'Content-Type: text/html'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
其它</p>
<p>
此函数位于:wp-includes/pluggable.php</p>
<p>
<br><strong>解决邮件无法发送的问题</strong><br>
邮件无法发送是 WordPress 比较常见的一种问题,一般是由于服务器不支持 PHP 的 mail() 函数造成的,通常有两种解决方法。</p>
<p>
PS:WordPress 发送邮件一定要使用 wp_mail() 函数,而不是直接调用 PHP SMTP 类或者其它函数。</p>
<p>
<strong>一、配置服务器环境</strong></p>
<p>
如果你使用的是 VPS,可以通过配置服务器的方式让服务器支持 mail() 函数,根据每个人的情况不同可以通过搜索引擎找到配置方法,这里就不详细说了。</p>
<p>
<strong>二、使用 SMTP</strong></p>
<p>
第二种就是用 SMTP 发送邮件了,也是我比较推荐的一种方法。SMTP 就是利用其它的免费邮件服务商的服务器发送邮件,所以用这种方式还不容易被识别成垃圾邮件。</p>
<p>
好了,让 WordPress 支持 SMTP 推荐的是使用 WP SMTP 插件,安装好后在后台配置一下你的 SMTP 信息,我使用的是 QQ 邮箱。</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="使用WordPress发送电子邮件的相关PHP函数用法解析" alt="使用WordPress发送电子邮件的相关PHP函数用法解析" src="https://zhuji.jb51.net/uploads/img/202305/9175164f27062ac90adcce9d64c746fb.jpg"></p>
<p>
配置好后可以在下边测试一下是否配置成功。</p>
<p>
插件主页:https://wordpress.org/plugins/wp-smtp/</p>
頁:
[1]