ssl_options.phps 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * This example shows settings to use when sending over SMTP with TLS and custom connection options.
  4. */
  5. //SMTP needs accurate times, and the PHP time zone MUST be set
  6. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  7. date_default_timezone_set('Etc/UTC');
  8. require '../PHPMailerAutoload.php';
  9. //Create a new PHPMailer instance
  10. $mail = new PHPMailer;
  11. //Tell PHPMailer to use SMTP
  12. $mail->isSMTP();
  13. //Enable SMTP debugging
  14. // 0 = off (for production use)
  15. // 1 = client messages
  16. // 2 = client and server messages
  17. $mail->SMTPDebug = 2;
  18. //Ask for HTML-friendly debug output
  19. $mail->Debugoutput = 'html';
  20. //Set the hostname of the mail server
  21. $mail->Host = 'smtp.example.com';
  22. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  23. $mail->Port = 587;
  24. //Set the encryption system to use - ssl (deprecated) or tls
  25. $mail->SMTPSecure = 'tls';
  26. //Custom connection options
  27. $mail->SMTPOptions = array (
  28. 'ssl' => array(
  29. 'verify_peer' => true,
  30. 'verify_depth' => 3,
  31. 'allow_self_signed' => true,
  32. 'peer_name' => 'smtp.example.com',
  33. 'cafile' => '/etc/ssl/ca_cert.pem',
  34. )
  35. );
  36. //Whether to use SMTP authentication
  37. $mail->SMTPAuth = true;
  38. //Username to use for SMTP authentication - use full email address for gmail
  39. $mail->Username = "username@example.com";
  40. //Password to use for SMTP authentication
  41. $mail->Password = "yourpassword";
  42. //Set who the message is to be sent from
  43. $mail->setFrom('from@example.com', 'First Last');
  44. //Set who the message is to be sent to
  45. $mail->addAddress('whoto@example.com', 'John Doe');
  46. //Set the subject line
  47. $mail->Subject = 'PHPMailer SMTP options test';
  48. //Read an HTML message body from an external file, convert referenced images to embedded,
  49. //convert HTML into a basic plain-text alternative body
  50. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  51. //send the message, check for errors
  52. if (!$mail->send()) {
  53. echo "Mailer Error: " . $mail->ErrorInfo;
  54. } else {
  55. echo "Message sent!";
  56. }