選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

100 行
3.2KB

  1. <?php
  2. /**
  3. * This example shows settings to use when sending via Google's Gmail servers.
  4. * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
  5. */
  6. //SMTP needs accurate times, and the PHP time zone MUST be set
  7. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  8. date_default_timezone_set('Etc/UTC');
  9. require '../PHPMailerAutoload.php';
  10. //Create a new PHPMailer instance
  11. $mail = new PHPMailer;
  12. //Tell PHPMailer to use SMTP
  13. $mail->isSMTP();
  14. //Enable SMTP debugging
  15. // 0 = off (for production use)
  16. // 1 = client messages
  17. // 2 = client and server messages
  18. $mail->SMTPDebug = 2;
  19. //Ask for HTML-friendly debug output
  20. $mail->Debugoutput = 'html';
  21. //Set the hostname of the mail server
  22. $mail->Host = 'smtp.gmail.com';
  23. // use
  24. // $mail->Host = gethostbyname('smtp.gmail.com');
  25. // if your network does not support SMTP over IPv6
  26. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  27. $mail->Port = 587;
  28. //Set the encryption system to use - ssl (deprecated) or tls
  29. $mail->SMTPSecure = 'tls';
  30. //Whether to use SMTP authentication
  31. $mail->SMTPAuth = true;
  32. //Username to use for SMTP authentication - use full email address for gmail
  33. $mail->Username = "username@gmail.com";
  34. //Password to use for SMTP authentication
  35. $mail->Password = "yourpassword";
  36. //Set who the message is to be sent from
  37. $mail->setFrom('from@example.com', 'First Last');
  38. //Set an alternative reply-to address
  39. $mail->addReplyTo('replyto@example.com', 'First Last');
  40. //Set who the message is to be sent to
  41. $mail->addAddress('whoto@example.com', 'John Doe');
  42. //Set the subject line
  43. $mail->Subject = 'PHPMailer GMail SMTP test';
  44. //Read an HTML message body from an external file, convert referenced images to embedded,
  45. //convert HTML into a basic plain-text alternative body
  46. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  47. //Replace the plain text body with one created manually
  48. $mail->AltBody = 'This is a plain-text message body';
  49. //Attach an image file
  50. $mail->addAttachment('images/phpmailer_mini.png');
  51. //send the message, check for errors
  52. if (!$mail->send()) {
  53. echo "Mailer Error: " . $mail->ErrorInfo;
  54. } else {
  55. echo "Message sent!";
  56. //Section 2: IMAP
  57. //Uncomment these to save your message in the 'Sent Mail' folder.
  58. #if (save_mail($mail)) {
  59. # echo "Message saved!";
  60. #}
  61. }
  62. //Section 2: IMAP
  63. //IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
  64. //Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
  65. //You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
  66. //be useful if you are trying to get this working on a non-Gmail IMAP server.
  67. function save_mail($mail) {
  68. //You can change 'Sent Mail' to any other folder or tag
  69. $path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
  70. //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
  71. $imapStream = imap_open($path, $mail->Username, $mail->Password);
  72. $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
  73. imap_close($imapStream);
  74. return $result;
  75. }

Powered by TurnKey Linux.