WordPress企业主题定制/开发/优化

WordPress发送邮件wp_mail()函数

首页 » WORDPRESS主题技术 » WordPress发送邮件wp_mail()函数

WordPress发送邮件的wp_mail()函数

有时候会用到wordpress发邮件的功能,这个时候就需要用到这个函数了。
wp_mail()函数是WP用来发邮件的一个函数,类似于PHP的mail(),函数定义如下:

function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ){
// Compact the input, apply the filters, and extract them back out
extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );

if ( !is_array($attachments) )
$attachments = explode( "\n", $attachments );

global $phpmailer;

// (Re)create it, if it's gone missing
if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer();
}

// Headers
if ( emptyempty( $headers ) ) {
$headers = array();
} else {
//...下边省略..............
//具体文件在WP根目录下wp-mail.php

$to是要发送的email地址,$subject是主题,$message是邮件内容,$headers比较复杂,可以这样设置

$headers = 'From: '. get_option('blogname') .' < ' . get_option('admin_email') . '>';
//尖括号前边有个空格,不要忘记了!

get_option('blogname')就是博客名称,get_option('admin_email')是admin的邮箱地址。还可以设置content-type,charset等参数。
当然了,需要使用wp_mail()函数,就需要成为WP的模板页(这个做法比较方便),或者手动导入全部需要的类文件(不推荐)。

相关项目

  • WordPress外贸企业主题

  • 最近更新

  • 热门标签