exceptions.phps 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title>PHPMailer - Exceptions test</title>
  6. </head>
  7. <body>
  8. <?php
  9. require '../PHPMailerAutoload.php';
  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. //Set who the message is to be sent from
  15. $mail->setFrom('from@example.com', 'First Last');
  16. //Set an alternative reply-to address
  17. $mail->addReplyTo('replyto@example.com', 'First Last');
  18. //Set who the message is to be sent to
  19. $mail->addAddress('whoto@example.com', 'John Doe');
  20. //Set the subject line
  21. $mail->Subject = 'PHPMailer Exceptions test';
  22. //Read an HTML message body from an external file, convert referenced images to embedded,
  23. //and convert the HTML into a basic plain-text alternative body
  24. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  25. //Replace the plain text body with one created manually
  26. $mail->AltBody = 'This is a plain-text message body';
  27. //Attach an image file
  28. $mail->addAttachment('images/phpmailer_mini.gif');
  29. //send the message
  30. //Note that we don't need check the response from this because it will throw an exception if it has trouble
  31. $mail->send();
  32. echo "Message sent!";
  33. } catch (phpmailerException $e) {
  34. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  35. } catch (Exception $e) {
  36. echo $e->getMessage(); //Boring error messages from anything else!
  37. }
  38. ?>
  39. </body>
  40. </html>