dt2015 2016-05-20 14:08
浏览 104

同时通过PHPMailer发送两封电子邮件

I am trying to send two email at once using PHPMailer, one to us, and the other to the user, but for some reasons, this doesn't work. It seems I can only send one email. See below. I tried to create two complete separate email class but that still doesn't work.

// PHPmailer
      //Create a new PHPMailer instance
      $mail_us = new PHPMailerOAuth;
  //Tell PHPMailer to use SMTP
  $mail_us->isSMTP();

  //Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail_us->SMTPDebug = 0;

  //Ask for HTML-friendly debug output
  $mail_us->Debugoutput = 'html';

  //Set the hostname of the mail server
  $mail_us->Host = 'smtp.gmail.com';

  //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  $mail_us->Port = 587;

  //Set the encryption system to use - ssl (deprecated) or tls
  $mail_us->SMTPSecure = 'tls';

  //Whether to use SMTP authentication
  $mail_us->SMTPAuth = true;

  //Set AuthType
  $mail_us->AuthType = 'XOAUTH2';

  //User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
  $mail_us->oauthUserEmail = "jon@email.com";

  //Obtained From Google Developer Console
  $mail_us->oauthClientId = "";

  //Obtained From Google Developer Console
  $mail_us->oauthClientSecret = "";

  //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
  //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
  // eg: http://localhost/phpmail/get_oauth_token.php
  $mail_us->oauthRefreshToken = "";

  //Set who the message is to be sent from
  //For gmail, this generally needs to be the same as the user you logged in as
  $mail_us->setFrom('jon@email.com', 'Jo');

  //Set who the message is to be sent to
  $mail_us->addAddress("");
  $mail_us->addCC('');
  $mail_us->addBCC('');

  $mail_us->addReplyTo('', 'John');

  //Set the subject line
  $mail_us->Subject = "New expert";

  //Read an HTML message body from an external file, convert referenced images to embedded,
  //convert HTML into a basic plain-text alternative body

  $mail_us->Body  .= "<h3>message </h3> <br>";
  $mail_us->Body  .= "message<br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->Body  .= "<b>message </b><br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->Body  .= "message <br>";
  $mail_us->AltBody = 'Please view this email HTML format.';



  //send the message, check for errors
  if (!$mail_us->send()) {
      echo "Mailer Error: " . $mail_us->ErrorInfo;
  } else {
      echo "Message sent!";
  }




      //////////////////////////////////////////////////////////////////////////
      /////Email the user //////

       // PHPmailer
      //Create a new PHPMailer instance
      $mail = new PHPMailerOAuth;

      //Tell PHPMailer to use SMTP
      $mail->isSMTP();

      //Enable SMTP debugging
      // 0 = off (for production use)
      // 1 = client messages
      // 2 = client and server messages
      $mail->SMTPDebug = 0;

      //Ask for HTML-friendly debug output
      $mail->Debugoutput = 'html';

      //Set the hostname of the mail server
      $mail->Host = 'smtp.gmail.com';

      //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
      $mail->Port = 587;

      //Set the encryption system to use - ssl (deprecated) or tls
      $mail->SMTPSecure = 'tls';

      //Whether to use SMTP authentication
      $mail->SMTPAuth = true;

      //Set AuthType
      $mail->AuthType = 'XOAUTH2';

      //User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
      $mail->oauthUserEmail = "";

      //Obtained From Google Developer Console
      $mail->oauthClientId = "";

      //Obtained From Google Developer Console
      $mail->oauthClientSecret = "";

      //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
      //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
      // eg: http://localhost/phpmail/get_oauth_token.php
      $mail->oauthRefreshToken = "";

      //Set who the message is to be sent from
      //For gmail, this generally needs to be the same as the user you logged in as
      $mail->setFrom('', '');

      //Set who the message is to be sent to
      $mail->addAddress("");
      $mail->addBCC('');
      $mail->addBCC('');
      $mail->addBCC('');

      $mail->addReplyTo('', '');

      //Set the subject line
      $mail->Subject = "New expert";

      //Read an HTML message body from an external file, convert referenced images to embedded,
      //convert HTML into a basic plain-text alternative body


      $mail->Body .= "message <br><br>";
      $mail->Body .= "message <br><br>";
      $mail->Body .= "message<br><br>";
      $mail->Body .= "message<br><br>";
      $mail->Body .= "message<br><br>";
      $mail->Body .= "message.<br><br>";
      $mail->Body .= "Best, <br>";
      $mail->Body .= "John<br>";
      $mail->AltBody = "message
                        ";



      //send the message, check for errors
      if (!$mail->send()) {
          echo "Mailer Error: " . $mail->ErrorInfo;
      } else {
          echo "Message sent!";
      }

Any help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集