gmail.phps 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title>PHPMailer - GMail SMTP test</title>
  6. </head>
  7. <body>
  8. <?php
  9. //SMTP needs accurate times, and the PHP time zone MUST be set
  10. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  11. date_default_timezone_set('Etc/UTC');
  12. require '../PHPMailerAutoload.php';
  13. //Create a new PHPMailer instance
  14. $mail = new PHPMailer();
  15. //Tell PHPMailer to use SMTP
  16. $mail->isSMTP();
  17. //Enable SMTP debugging
  18. // 0 = off (for production use)
  19. // 1 = client messages
  20. // 2 = client and server messages
  21. $mail->SMTPDebug = 2;
  22. //Ask for HTML-friendly debug output
  23. $mail->Debugoutput = 'html';
  24. //Set the hostname of the mail server
  25. $mail->Host = 'smtp.gmail.com';
  26. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  27. $mail->Port = 587;
  28. //Set the encryption system to use - ssl (deprecated) or tls
  29. $mail->SMTPSecure = 'tls';
  30. //Whether to use SMTP authentication
  31. $mail->SMTPAuth = true;
  32. //Username to use for SMTP authentication - use full email address for gmail
  33. $mail->Username = "username@gmail.com";
  34. //Password to use for SMTP authentication
  35. $mail->Password = "yourpassword";
  36. //Set who the message is to be sent from
  37. $mail->setFrom('from@example.com', 'First Last');
  38. //Set an alternative reply-to address
  39. $mail->addReplyTo('replyto@example.com', 'First Last');
  40. //Set who the message is to be sent to
  41. $mail->addAddress('whoto@example.com', 'John Doe');
  42. //Set the subject line
  43. $mail->Subject = 'PHPMailer GMail SMTP test';
  44. //Read an HTML message body from an external file, convert referenced images to embedded,
  45. //convert HTML into a basic plain-text alternative body
  46. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  47. //Replace the plain text body with one created manually
  48. $mail->AltBody = 'This is a plain-text message body';
  49. //Attach an image file
  50. $mail->addAttachment('images/phpmailer_mini.gif');
  51. //send the message, check for errors
  52. if (!$mail->send()) {
  53. echo "Mailer Error: " . $mail->ErrorInfo;
  54. } else {
  55. echo "Message sent!";
  56. }
  57. ?>
  58. </body>
  59. </html>