dongyi8383 2015-12-11 19:33
浏览 44
已采纳

php邮件设置功能开关无法正常工作

I'm trying to make a function/switch for diffrent settings for the PHPmailer. (something like this:

function create_mailer($current_site,$from,$from_name){

    require_once 'classes/PHPMailer/PHPMailerAutoload.php';
    $mailer = new PHPMailer;

    switch ($current_site){
        case 'site1':
            $mailer->isSMTP();
            $mailer->Host       = 'mail.site1.com';
            $mailer->SMTPAuth   = true;
            $mailer->SMTPSecure = 'tls';
            $mailer->Port       = 587;
            $mailer->Username   = 'username1';
            $mailer->Password   = 'password1';
            $mailer->From = $from;
            $mailer->FromName = $from_name;
            $mailer->isHTML(true);
            return $mailer;
        break;
    case 'site2':
            $mailer = new PHPMailer;
            $mailer->isSMTP();
            $mailer->Host       = 'mail.site2.com';
            $mailer->SMTPAuth   = true;
            $mailer->SMTPSecure = 'tls';
            $mailer->Port       = 587;
            $mailer->Username   = 'username2';
            $mailer->Password   = 'password2';
            $mailer->From = $from;
            $mailer->FromName = $from_name;
            $mailer->isHTML(true);
            return $mailer;
        break;
    }
}

In de index.php I've made something like :

require 'functions/create_mailer.php';
$mailer = create_mailer('site1','mail1@sitename1.com','mailname');
$mail = new Mailer($mailer);
$mail->send('mailtemplate/new_bid.php',['something'=>'else'],function($m) {
                $m->to('email@to.somebody');
                $m->subject('mailsubject');
});

When i don't use the switch the mailer is working. (Iff the settings are just in the index.php i mean) I cant figure out why this isn't working beacause i alway thought that this kind must work:)

please advice.

thanks in advanced.

  • 写回答

1条回答 默认 最新

  • doujiyun8846 2015-12-11 19:41
    关注

    You are calling create_mailer('sitename1','mail1@sitename1.com','mailname'); but are testing with 'site1'.

    Either add a default to your switch, or call create_mailer('site1','mail1@sitename1.com','mailname');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本