{
    分享网正式开通,我们为大家提供免费资源,欢迎大家踊跃投稿!

WordPress发送邮件函数wp_mail使用详解

前几天一个多年的站长朋友问我wordpress发送邮件的功能问题,其实WordPress内置了一套用于提醒用户的邮件系统,并提供了wp_mail函数用于发送邮件。只是由于服务器提供商一般都没有安装邮件服务,所以直接使用会报错,但是我们可以通过smtp插件来恢复服务,当然你也可以自己配置主机的mail服务,但是那样做会让你的邮件出现在垃圾箱里!

wp_mail( string|array $to, string $subject, string $message, string|array $headers = '',string|array $attachments = array() )

官方描述:像PHP发送邮件一样,发送邮件。

函数介绍:
真正的返回值并不意味着用户成功地收到了电子邮件。这仅仅意味着所使用的方法能够处理请求而没有任何错误。使用两个‘wp_mail_FROM’和‘wp_mail_FROM_name’挂钩允许在设置‘name email@Address.com’这样的FROM地址时创建FROM地址。如果只设置‘wp_mail_From’,那么只使用电子邮件地址而不使用名称。默认的内容类型是“text/plain”,它不允许使用HTML。但是,您可以使用‘wp_mail_content_type’过滤器来设置电子邮件的内容类型。默认字符集基于博客中使用的字符集。可以使用‘wp_mail_charset’过滤器来设置字符集。

参数:

  • $to
    (string|array(Required)收信人地址,多个收信人使用数组形式。
  • $subject
    (string(Required) 邮件主题
  • $message
    (string(Required) 邮件内容
  • $headers
    (string|array(Optional) 额外的头部.
    Default value: ”
  • $attachments
    (string|array(Optional) 附件.
    Default value: array()

返回值:

(bool) 当邮件发送过程不报错时返回true

使用实例

发送HTML邮件

$to = 'sendto@example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );

指定额外的头部可以发送HTML内容的邮件。

使用数组形式发送邮件

<?php
// assumes $to, $subject, $message have already been defined earlier...
$headers[] = 'From: Me Myself <me@example.net>';
$headers[] = 'Cc: John Q Codex <jqc@wordpress.org>';
$headers[] = 'Cc: iluvwp@wordpress.org'; // note you can just use a simple email address
wp_mail( $to, $subject, $message, $headers );
?>

使用phpmailer替代mail发送邮件时配置smtp

add_action( 'phpmailer_init', 'mailer_config', 10, 1);
function mailer_config(PHPMailer $mailer){
$mailer->IsSMTP();
$mailer->Host = "mail.telemar.it"; // your SMTP server
$mailer->Port = 25;
$mailer->SMTPDebug = 2; // write 0 if you don't want to see client/server communication in page
$mailer->CharSet = "utf-8";
}

使用额外的头部发送HTML内容邮件

$to = 'emailsendto@example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name &lt;support@example.com');
wp_mail( $to, $subject, $body, $headers );

资源均来自第三方,谨慎下载,前往第三方网站下载


米微资源分享网 , 版权所有丨本站资源仅限于学习研究,严禁从事商业或者非法活动!丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress发送邮件函数wp_mail使用详解
喜欢 ()分享 (0)