dongxun1244 2013-11-24 14: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 14: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条)

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?