test_smtp_gmail_advanced.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <html>
  2. <head>
  3. <title>PHPMailer - SMTP (Gmail) advanced test</title>
  4. </head>
  5. <body>
  6. <?php
  7. require_once('../class.phpmailer.php');
  8. //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
  9. $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
  10. $mail->IsSMTP(); // telling the class to use SMTP
  11. try {
  12. $mail->Host = "mail.yourdomain.com"; // SMTP server
  13. $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
  14. $mail->SMTPAuth = true; // enable SMTP authentication
  15. $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
  16. $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
  17. $mail->Port = 465; // set the SMTP port for the GMAIL server
  18. $mail->Username = "yourusername@gmail.com"; // GMAIL username
  19. $mail->Password = "yourpassword"; // GMAIL password
  20. $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  21. $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  22. $mail->SetFrom('name@yourdomain.com', 'First Last');
  23. $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  24. $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  25. $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  26. $mail->MsgHTML(file_get_contents('contents.html'));
  27. $mail->AddAttachment('images/phpmailer.gif'); // attachment
  28. $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  29. $mail->Send();
  30. echo "Message Sent OK</p>\n";
  31. } catch (phpmailerException $e) {
  32. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  33. } catch (Exception $e) {
  34. echo $e->getMessage(); //Boring error messages from anything else!
  35. }
  36. ?>
  37. </body>
  38. </html>