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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据