smtp.phps 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * This example shows making an SMTP connection with authentication.
  4. */
  5. //Import the PHPMailer class into the global namespace
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. //SMTP needs accurate times, and the PHP time zone MUST be set
  8. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  9. date_default_timezone_set('Etc/UTC');
  10. require '../vendor/autoload.php';
  11. //Create a new PHPMailer instance
  12. $mail = new PHPMailer;
  13. //Tell PHPMailer to use SMTP
  14. $mail->isSMTP();
  15. //Enable SMTP debugging
  16. // 0 = off (for production use)
  17. // 1 = client messages
  18. // 2 = client and server messages
  19. $mail->SMTPDebug = 2;
  20. //Set the hostname of the mail server
  21. $mail->Host = 'mail.example.com';
  22. //Set the SMTP port number - likely to be 25, 465 or 587
  23. $mail->Port = 25;
  24. //Whether to use SMTP authentication
  25. $mail->SMTPAuth = true;
  26. //Username to use for SMTP authentication
  27. $mail->Username = 'yourname@example.com';
  28. //Password to use for SMTP authentication
  29. $mail->Password = 'yourpassword';
  30. //Set who the message is to be sent from
  31. $mail->setFrom('from@example.com', 'First Last');
  32. //Set an alternative reply-to address
  33. $mail->addReplyTo('replyto@example.com', 'First Last');
  34. //Set who the message is to be sent to
  35. $mail->addAddress('whoto@example.com', 'John Doe');
  36. //Set the subject line
  37. $mail->Subject = 'PHPMailer SMTP test';
  38. //Read an HTML message body from an external file, convert referenced images to embedded,
  39. //convert HTML into a basic plain-text alternative body
  40. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  41. //Replace the plain text body with one created manually
  42. $mail->AltBody = 'This is a plain-text message body';
  43. //Attach an image file
  44. $mail->addAttachment('images/phpmailer_mini.png');
  45. //send the message, check for errors
  46. if (!$mail->send()) {
  47. echo 'Mailer Error: ' . $mail->ErrorInfo;
  48. } else {
  49. echo 'Message sent!';
  50. }