纯代码为wordpress添加stmp邮件服务
很多用户在使用wordpress搭建网站时会发现不能发送邮件,为什么呢,因为服务器都支持mail函数的,如何解决这个问题呢,很多人第一时间会选择插件,就我个人而言,不怎么喜欢使用插件,我喜欢用代码来解决,这个教大家如何给网站添加stmp邮件服务。
方法一,直接在wordpress主题目录functions.php文件中添加下面代码
- add_action('phpmailer_init', 'mail_smtp');
- function mail_smtp( $phpmailer ) {
- $phpmailer->FromName = '信息库'; //发件人
- $phpmailer->Host = 'smtp.ym.163.com'; //修改为你使用的SMTP服务器
- $phpmailer->Port = 465; //SMTP端口,开启了SSL加密
- $phpmailer->Username = 'admin@xxko.net'; //邮箱账户
- $phpmailer->Password = '*******'; //输入你对应的邮箱密码,这里使用了*代替
- $phpmailer->From = 'admin@xxko.net'; //你的邮箱
- $phpmailer->SMTPAuth = true;
- $phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25留空,465为ssl)
- $phpmailer->IsSMTP();
- }
注:上面代码中,替换成自己的邮件地址和密码。
方法二,将stmp服务集成到你的主题选项中,首先在functions.php文件中添加下面代码
[reply]
- //使用smtp发送邮件
- if (hl_get_option('mailsmtp_b')){
- //SMTP邮箱设置
- function googlo_mail_smtp($phpmailer) {
- $phpmailer->From = '' . hl_get_option('maildizhi_b') . ''; //发件人地址
- $phpmailer->FromName = '' . hl_get_option('mailnichen_b') . ''; //发件人昵称
- $phpmailer->Host = '' . hl_get_option('mailsmtp_b') . ''; //SMTP服务器地址
- $phpmailer->Port = '' . hl_get_option('mailport_b') . ''; //SMTP邮件发送端口
- if (get_option('smtpssl_b')) {
- $phpmailer->SMTPSecure = 'ssl';
- }else{
- $phpmailer->SMTPSecure = '';
- }//SMTP加密方式(SSL/TLS)没有为空即可
- $phpmailer->Username = '' . hl_get_option('mailuser_b') . ''; //邮箱帐号
- $phpmailer->Password = '' . hl_get_option('mailpass_b') . ''; //邮箱密码
- $phpmailer->IsSMTP();
- $phpmailer->SMTPAuth = true; //启用SMTPAuth服务
- }
- add_action('phpmailer_init', 'googlo_mail_smtp');
- }
然后在主题选项中合适的位置添加
- // STMP邮箱
- $options[] = array(
- 'name' => 'STMP邮箱',
- 'type' => 'heading'
- );
- $options[] = array(
- 'name' => '',
- 'desc' => '请输入您的邮箱地址',
- 'id' => 'maildizhi_b',
- 'std' => '发件人地址',
- 'type' => 'text'
- );
- $options[] = array(
- 'name' => '',
- 'desc' => '请输入您的网站名称',
- 'id' => 'mailnichen_b',
- 'std' => '发件人昵称',
- 'type' => 'text'
- );
- $options[] = array(
- 'name' => '',
- 'desc' => '请输入您的邮箱的SMTP服务器',
- 'id' => 'mailsmtp_b',
- 'std' => 'smtp.ym.163.com',
- 'type' => 'text'
- );
- $options[] = array(
- 'name' => 'SSL安全连接',
- 'desc' => '如果你不知道这个是做什么的,请不要动',
- 'id' => 'smtpssl_b',
- 'std' => '1',
- 'type' => 'checkbox',
- );
- $options[] = array(
- 'name' => '',
- 'desc' => '请输入您的smtp端口,一般QQ邮箱25就可以了',
- 'id' => 'mailport_b',
- 'std' => '25',
- 'type' => 'text'
- );
- $options[] = array(
- 'name' => '',
- 'desc' => '请输入您的邮箱地址',
- 'id' => 'mailuser_b',
- 'std' => '邮箱账号',
- 'type' => 'text'
- );
- $options[] = array(
- 'name' => '',
- 'desc' => '请输入您的邮箱密码',
- 'id' => 'mailpass_b',
- 'std' => '邮箱密码',
- 'type' => 'password'
- );
[/reply]
大功告成。
注,上面代码是用在options-framework,其它后台文件大同小异,区别不大。