DFdou's Blog Life is short,Be yourself.

7kankan11bookekzwС˵Ҫ · uawx鶼Ķ ðƴӢ ޴½
1507/091

WordPress-wp_mail()函数

有时候在自定义主题的时候,会用到发邮件的功能,这个时候可能就需要用到这个函数了。
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 ( empty( $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的模板页(这个做法比较方便),或者手动导入全部需要的类文件(不推荐)。
参考资料:《Phpmailer的用法》

Some Random Posts

Comments (1) Trackbacks (0)
  1. 发件人名字设置方法如下,在$header里加入类似于”-reply@nwhy.org>”的代码即可 :chaocai

    [Reply]


Leave a comment

:zhuang :xizao :wuwu :wenhao :wc :touxiang :shuaya :shuajian :shengtian :paopao :no :meinv :maonv :loveu :leilei :kua :jrjr :dahan :chi :chaocai :byebye


CommentLuv Enabled

No trackbacks yet.