pop_before_smtp.phps 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * This example shows how to use POP-before-SMTP for authentication.
  4. * POP-before-SMTP is a very old technology that is hardly used any more.
  5. */
  6. //Import PHPMailer classes into the global namespace
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use PHPMailer\PHPMailer\Exception;
  9. use PHPMailer\PHPMailer\POP3;
  10. require '../vendor/autoload.php';
  11. //Authenticate via POP3.
  12. //After this you should be allowed to submit messages over SMTP for a few minutes.
  13. //Only applies if your host supports POP-before-SMTP.
  14. $pop = POP3::popBeforeSmtp('pop3.example.com', 110, 30, 'username', 'password', 1);
  15. //Create a new PHPMailer instance
  16. //Passing true to the constructor enables the use of exceptions for error handling
  17. $mail = new PHPMailer(true);
  18. try {
  19. $mail->isSMTP();
  20. //Enable SMTP debugging
  21. // 0 = off (for production use)
  22. // 1 = client messages
  23. // 2 = client and server messages
  24. $mail->SMTPDebug = 2;
  25. //Set the hostname of the mail server
  26. $mail->Host = 'mail.example.com';
  27. //Set the SMTP port number - likely to be 25, 465 or 587
  28. $mail->Port = 25;
  29. //Whether to use SMTP authentication
  30. $mail->SMTPAuth = false;
  31. //Set who the message is to be sent from
  32. $mail->setFrom('from@example.com', 'First Last');
  33. //Set an alternative reply-to address
  34. $mail->addReplyTo('replyto@example.com', 'First Last');
  35. //Set who the message is to be sent to
  36. $mail->addAddress('whoto@example.com', 'John Doe');
  37. //Set the subject line
  38. $mail->Subject = 'PHPMailer POP-before-SMTP test';
  39. //Read an HTML message body from an external file, convert referenced images to embedded,
  40. //and convert the HTML into a basic plain-text alternative body
  41. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  42. //Replace the plain text body with one created manually
  43. $mail->AltBody = 'This is a plain-text message body';
  44. //Attach an image file
  45. $mail->addAttachment('images/phpmailer_mini.png');
  46. //send the message
  47. //Note that we don't need check the response from this because it will throw an exception if it has trouble
  48. $mail->send();
  49. echo 'Message sent!';
  50. } catch (Exception $e) {
  51. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  52. } catch (\Exception $e) {
  53. echo $e->getMessage(); //Boring error messages from anything else!
  54. }