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

时间:2016-05-20 14:08:31

标签: php email phpmailer

我正在尝试使用PHPMailer一次发送两封电子邮件,一封给我们,另一封给用户,但由于某些原因,这不起作用。好像我只能发一封电子邮件。见下文。我试图创建两个完全独立的电子邮件类,但仍然无法正常工作。

// 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!";
      }

非常感谢任何帮助。

0 个答案:

没有答案