exceptions.phps 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. /**
  3. * This example shows how to make use of PHPMailer's exceptions for error handling.
  4. */
  5. require '../PHPMailerAutoload.php';
  6. //Create a new PHPMailer instance
  7. //Passing true to the constructor enables the use of exceptions for error handling
  8. $mail = new PHPMailer(true);
  9. try {
  10. //Set who the message is to be sent from
  11. $mail->setFrom('from@example.com', 'First Last');
  12. //Set an alternative reply-to address
  13. $mail->addReplyTo('replyto@example.com', 'First Last');
  14. //Set who the message is to be sent to
  15. $mail->addAddress('whoto@example.com', 'John Doe');
  16. //Set the subject line
  17. $mail->Subject = 'PHPMailer Exceptions test';
  18. //Read an HTML message body from an external file, convert referenced images to embedded,
  19. //and convert the HTML into a basic plain-text alternative body
  20. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  21. //Replace the plain text body with one created manually
  22. $mail->AltBody = 'This is a plain-text message body';
  23. //Attach an image file
  24. $mail->addAttachment('images/phpmailer_mini.png');
  25. //send the message
  26. //Note that we don't need check the response from this because it will throw an exception if it has trouble
  27. $mail->send();
  28. echo "Message sent!";
  29. } catch (phpmailerException $e) {
  30. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  31. } catch (Exception $e) {
  32. echo $e->getMessage(); //Boring error messages from anything else!
  33. }