dongxun1244 2013-11-24 06:31
浏览 92
已采纳

如何使phpmailer代码更简单,它用于发送2个邮件

I have php code using phpmailer to send two different messages to two users. I have duplicate the code twice to send both mails, but that's makes the process takes long tome to complete the task. is there any solution to make my code more simple

//// -------------------- send email. to student adviser ---------------------------------------------------------- require("phpmailer/class.phpmailer.php"); $mail = new PHPMailer();

$mail->Username = "rms@gmail.com"; 
$mail->Password = "12121212"; 
$mail->AddAddress($advisoremail);
$mail->FromName = "RMS-NCT";

$mail->Subject = "New Request from: ".$_SESSION['UID'];
$mail->Body    = "Dear Mr. Adviser you have got new request from 26s12115 ... click here to access it. http://localhost/rms/"; 
//-----------------------------------------------------------------------

$mail->Host = "ssl://smtp.gmail.com"; 
$mail->Port = 465;
$mail->IsSMTP(); 
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
    echo "Mailer Error: " . $mail->ErrorInfo;
else
    echo "Message has been sent";

// ------------send email to student ----------------------

$mail = new PHPMailer();
$mail->Username = "rms@gmail.com"; // your GMail user name
$mail->Password = "12121212"; 
$mail->AddAddress($_SESSION['UEMAIL']);
$mail->FromName = "RMS-NCT";

$mail->Subject = "Receipt for your new Request";
$mail->Body    = "Dear Student .. Your request has been sent.. you will get response as soon as possible."; 
//-----------------------------------------------------------------------

$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
    echo "Mailer Error: " . $mail->ErrorInfo;
else
    echo "Message has been sent";   

展开全部

  • 写回答

2条回答 默认 最新

  • douji5523 2013-11-24 06:34
    关注

    As long as you have two different subjects and bodies, I would say no. There is no way to simplify this task. But you could put the task in one function, that gets the different parameters. So you just have one function to call.

    require("phpmailer/class.phpmailer.php"); 
    
    function send_mail($email, $subject, $body) {
        $mail = new PHPMailer();
    
        $mail->Username = "rms@gmail.com"; 
        $mail->Password = "12121212"; 
        $mail->AddAddress($email);
        $mail->FromName = "RMS-NCT";
    
        $mail->Subject = $subject;
        $mail->Body    = $body; 
        //-----------------------------------------------------------------------
    
        $mail->Host = "ssl://smtp.gmail.com"; 
        $mail->Port = 465;
        $mail->IsSMTP(); 
        $mail->SMTPAuth = true;
        $mail->From = $mail->Username;
        if(!$mail->Send())
            echo "Mailer Error: " . $mail->ErrorInfo;
        else
            echo "Message has been sent";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部