smtp_no_auth.phps 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * This example shows making an SMTP connection without using 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. //We don't need to set this as it's the default value
  25. //$mail->SMTPAuth = false;
  26. //Set who the message is to be sent from
  27. $mail->setFrom('from@example.com', 'First Last');
  28. //Set an alternative reply-to address
  29. $mail->addReplyTo('replyto@example.com', 'First Last');
  30. //Set who the message is to be sent to
  31. $mail->addAddress('whoto@example.com', 'John Doe');
  32. //Set the subject line
  33. $mail->Subject = 'PHPMailer SMTP without auth test';
  34. //Read an HTML message body from an external file, convert referenced images to embedded,
  35. //convert HTML into a basic plain-text alternative body
  36. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  37. //Replace the plain text body with one created manually
  38. $mail->AltBody = 'This is a plain-text message body';
  39. //Attach an image file
  40. $mail->addAttachment('images/phpmailer_mini.png');
  41. //send the message, check for errors
  42. if (!$mail->send()) {
  43. echo "Mailer Error: " . $mail->ErrorInfo;
  44. } else {
  45. echo "Message sent!";
  46. }