pop_before_smtp.phps 2.1 KB

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