smtp.phps 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title>PHPMailer - 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 = "mail.example.com";
  26. //Set the SMTP port number - likely to be 25, 465 or 587
  27. $mail->Port = 25;
  28. //Whether to use SMTP authentication
  29. $mail->SMTPAuth = true;
  30. //Username to use for SMTP authentication
  31. $mail->Username = "yourname@example.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 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.gif');
  49. //send the message, check for errors
  50. if (!$mail->send()) {
  51. echo "Mailer Error: " . $mail->ErrorInfo;
  52. } else {
  53. echo "Message sent!";
  54. }
  55. ?>
  56. </body>
  57. </html>