ssl_options.phps 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * This example shows settings to use when sending over SMTP with TLS and custom connection options.
  4. */
  5. //Import the PHPMailer class into the global namespace
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. //SMTP needs accurate times, and the PHP time zone MUST be set
  8. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  9. date_default_timezone_set('Etc/UTC');
  10. require '../vendor/autoload.php';
  11. //Create a new PHPMailer instance
  12. $mail = new PHPMailer;
  13. //Tell PHPMailer to use SMTP
  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. //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. //Note that these settings are INSECURE
  28. $mail->SMTPOptions = array (
  29. 'ssl' => [
  30. 'verify_peer' => true,
  31. 'verify_depth' => 3,
  32. 'allow_self_signed' => true,
  33. 'peer_name' => 'smtp.example.com',
  34. 'cafile' => '/etc/ssl/ca_cert.pem',
  35. ]
  36. );
  37. //Whether to use SMTP authentication
  38. $mail->SMTPAuth = true;
  39. //Username to use for SMTP authentication - use full email address for gmail
  40. $mail->Username = 'username@example.com';
  41. //Password to use for SMTP authentication
  42. $mail->Password = 'yourpassword';
  43. //Set who the message is to be sent from
  44. $mail->setFrom('from@example.com', 'First Last');
  45. //Set who the message is to be sent to
  46. $mail->addAddress('whoto@example.com', 'John Doe');
  47. //Set the subject line
  48. $mail->Subject = 'PHPMailer SMTP options test';
  49. //Read an HTML message body from an external file, convert referenced images to embedded,
  50. //convert HTML into a basic plain-text alternative body
  51. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  52. //Send the message, check for errors
  53. if (!$mail->send()) {
  54. echo 'Mailer Error: ' . $mail->ErrorInfo;
  55. } else {
  56. echo 'Message sent!';
  57. }