Having trouble with PHPMailer

后端 未结 7 1036
渐次进展
渐次进展 2020-11-28 09:24

I am trying to use PHPMailer to send a gmail email. I followed this post

In order to do this, I set up a function shown below:

function sendEmail($         


        
7条回答
  •  余生分开走
    2020-11-28 10:09

    Try...

    IsSMTP(); // telling the class to use SMTP
    
    try {
      $mail->Host       = "smtp.gmail.com"; // SMTP server
      $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
      $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
      $mail->Username   = "yourname@yourdomain"; // SMTP account username
      $mail->Password   = "yourpassword";        // SMTP account password
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
      $mail->SetFrom('name@yourdomain.com', 'First Last');
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->Subject = 'This is a TEST Message';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML($obdy);
      $mail->AddAttachment('images/phpmailer.gif');      // attachment
      $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
      $mail->Send();
      echo "Message Sent OK

    \n"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! } ?>

    Just replace with your configuration, set the require_once('class.phpmailer.php'); to point to the right place and replace 'contents.html' your HTML template.

    or

    Use this code if you choose not to use an HTML template...

    IsSMTP(); // telling the class to use SMTP
    
    try {
      $mail->Host       = "smtp.gmail.com"; // SMTP server
      $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
      $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
      $mail->Username   = "yourname@yourdomain"; // SMTP account username
      $mail->Password   = "yourpassword";        // SMTP account password
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
      $mail->SetFrom('name@yourdomain.com', 'First Last');
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->Subject = 'This is a TEST message';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML($body);
      $mail->AddAttachment('images/phpmailer.gif');      // attachment
      $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
      $mail->Send();
      echo "Message Sent OK

    \n"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! } ?>

提交回复
热议问题