生命不息,奋斗不止/创造价值-传递价值-获得价值
所谓迷茫,就是才华配不上梦想 每一个让你难堪的现在,都有一个不够努力的曾经

WordPress 使用 smtp 发送评论提醒邮件

如果你空间不支持正常邮件发送功能的(小猫的空间就是如此),需要使用smtp发送评论邮件的话,那么你就可以继续看下文的教程了。

首先,PHPMailer,把里面的PHPMailer文件夹丢在你的主题目录里面。

然后再你 functions.php 文件里面加以下代码即可,记得修改对应的邮件参数即可,强烈建议使用163邮箱,因为我用163发smtp那么久没出过什么错,需要的地方我都有注释的:

/**
 * WordPress 使用 smtp 发送评论提醒邮件
 * http://www.wpdaxue.com/wordpress-comment-mail-notify-smtp.html
 */
function comment_mail_notify_editFromClmao($comment_id) {
	$comment = get_comment($comment_id);
	$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
	$spam_confirmed = $comment->comment_approved;
	if (($parent_id != '') && ($spam_confirmed != 'spam')) {
		$to = trim(get_comment($parent_id)->comment_author_email);
		$subject = '您在 ['%20. get_option("blogname") . '] 的留言有了回应';
		$message = '
			<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
				<p>'%20. trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
				<p>您曾在《'%20. get_the_title($comment->comment_post_ID) . '》的留言:<br />'
					. trim(get_comment($parent_id)->comment_content) . '</p>
				<p>'%20. trim($comment->comment_author) . ' 给您的回应:<br />'
						. trim($comment->comment_content) . '<br /></p>
				<p>您可以点击 <a href="'%20. htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回应内容</a></p>
				<p>欢迎再度光临 <a href="'%20. get_option('home') . '">'%20. get_option('blogname') . '</a></p>
				<p>(此邮件由系统发出, 请勿回复.)</p>
			</div>';
		header("content-type:text/html;charset=utf-8");
		ini_set("magic_quotes_runtime",0);
		require get_template_directory().'/PHPMailer/class.phpmailer.php';
		try {
			$mail = new PHPMailer(true);
			$mail->IsSMTP();
			$mail->CharSet='UTF-8';
			$mail->SMTPAuth = true;
			$mail->Port = 25;
			$mail->Host = "smtp.163.com";//邮箱smtp地址,此处以163为例
			$mail->Username = "你的邮箱账号";//你的邮箱账号
			$mail->Password = "你的邮箱密码";//你的邮箱密码
			$mail->From = "你的邮箱账号";//你的邮箱账号
			$mail->FromName = get_option('blogname');
			$to = $to;
			$mail->AddAddress($to);
			$mail->Subject = $subject;
			$mail->Body = $message;
			$mail->WordWrap = 80;
			//$mail->AddAttachment("f:/test.png"); //可以添加附件
			$mail->IsHTML(true);
			$mail->Send();
			} catch (phpmailerException $e) {
			// echo "邮件发送失败:".$e->errorMessage(); //测试的时候可以去掉此行的注释
		}
	}
}
add_action('comment_post', 'comment_mail_notify_editFromClmao');

效果图:

20140426221820

赞(0)
未经允许不得转载:jack361博客 » WordPress 使用 smtp 发送评论提醒邮件

评论 抢沙发

评论前必须登录!

 

如果你爱我或恨我,请↓

联系我