在wordpress中配置自己的邮件,可以更方便的使用邮件功能
自定义设置wordpress邮件配置
在functions.php设置使用自定义邮件
function mail_smtp($phpmailer){
$user = get_option('emailUser');//用户名 可以写入设置也可以固定写死
$pwd = get_option('emailPwd');//密码 可以写入设置也可以固定写死
$host = get_option('emailHost');//主机 可以写入设置也可以固定写死
$port = get_option('emailPort');//端口 可以写入设置也可以固定写死
$ssl = get_option('emailSsl');//是否是加密状态 可以写入设置也可以固定写死
if($user && $pwd && $host && $port){
$phpmailer->isSMTP();
$phpmailer->SMTPAuth = true; // 开启SMTPAuth服务
$phpmailer->Port = $port; // SMTP邮件发送端口,常用端口有:25,安全链接端口:465
$phpmailer->SMTPSecure = ($ssl==1)?'ssl':''; // 是否通过SSL链接,如不是此处删掉ssl,如果是不用改
$phpmailer->Host = $host; // SMTP服务器地址
$phpmailer->Username = $user; // 您的邮件地址
$phpmailer->Password = $pwd; // 你的邮箱登陆密码
}
}
add_action('phpmailer_init', 'mail_smtp');
邮件代码的调用
function sendMail(){
$tit = '测试邮件';
$user = '[email protected]';
$to = '[email protected]';
$body = '这是一封测试邮件';
$headers = array('Content-Type: text/html; charset=UTF-8','From: '.$tit.' <'.$user.'>');
return wp_mail($to,$tit,$body,$headers);
}
注意:这个要先开启转发邮箱的smtp功能
腾讯邮箱开启方法
邮箱-设置-账户-POP/SMTP功能