gmail.phps 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * This example shows settings to use when sending via Google's Gmail servers.
  4. * This uses traditional id & password authentication - look at the gmail_xoauth.phps
  5. * example to see how to use XOAUTH2.
  6. */
  7. //Import PHPMailer classes into the global namespace
  8. use PHPMailer\PHPMailer\PHPMailer;
  9. require '../vendor/autoload.php';
  10. //Create a new PHPMailer instance
  11. $mail = new PHPMailer;
  12. //Tell PHPMailer to use SMTP
  13. $mail->isSMTP();
  14. //Enable SMTP debugging
  15. // 0 = off (for production use)
  16. // 1 = client messages
  17. // 2 = client and server messages
  18. $mail->SMTPDebug = 2;
  19. //Set the hostname of the mail server
  20. $mail->Host = 'smtp.gmail.com';
  21. // use
  22. // $mail->Host = gethostbyname('smtp.gmail.com');
  23. // if your network does not support SMTP over IPv6
  24. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  25. $mail->Port = 587;
  26. //Set the encryption system to use - ssl (deprecated) or tls
  27. $mail->SMTPSecure = 'tls';
  28. //Whether to use SMTP authentication
  29. $mail->SMTPAuth = true;
  30. //Username to use for SMTP authentication - use full email address for gmail
  31. $mail->Username = "username@gmail.com";
  32. //Password to use for SMTP authentication
  33. $mail->Password = "yourpassword";
  34. //Set who the message is to be sent from
  35. $mail->setFrom('from@example.com', 'First Last');
  36. //Set an alternative reply-to address
  37. $mail->addReplyTo('replyto@example.com', 'First Last');
  38. //Set who the message is to be sent to
  39. $mail->addAddress('whoto@example.com', 'John Doe');
  40. //Set the subject line
  41. $mail->Subject = 'PHPMailer GMail SMTP test';
  42. //Read an HTML message body from an external file, convert referenced images to embedded,
  43. //convert HTML into a basic plain-text alternative body
  44. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  45. //Replace the plain text body with one created manually
  46. $mail->AltBody = 'This is a plain-text message body';
  47. //Attach an image file
  48. $mail->addAttachment('images/phpmailer_mini.png');
  49. //send the message, check for errors
  50. if (!$mail->send()) {
  51. echo "Mailer Error: " . $mail->ErrorInfo;
  52. } else {
  53. echo "Message sent!";
  54. }