Справочники, инструменты, документация

PHP: Отправка писем по SMTP через Gmail, примеры и настройка

SMTP — это широко используемый сетевой протокол, предназначенный для передачи электронной почты в сетях TCP/IP.
PHP: Отправка писем по SMTP через Gmail, примеры и настройка

Для отправки писем по SMTP из PHP можно использовать довольно популярный php-класс, размещенный на GitHub PHPMailer: https://github.com/PHPMailer/PHPMailer

Рекомендуемый способ его инсталляции - через Composer (должен быть загружен или установлен на сервере). Для тестирования скрипта нужно зайти по SSH в пустой, доступный из web каталог и выполнить:

$ composer require phpmailer/phpmailer

После выполнения команды в каталоге будет создана директория vendor, в которую будут загружены файлы класса PHPMailer и автозагрузчик Composer. Теперь в той же директории, из которой запускался Composer создается тестовый скрипт:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
  // Server settings
  $mail->setLanguage('ru', 'vendor/phpmailer/phpmailer/language/'); // Перевод на русский язык

  // Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail->SMTPDebug = 1; // Enable verbose debug output

  $mail->isSMTP(); // Set mailer to use SMTP

  $mail->SMTPAuth = true; // Enable SMTP authentication

  //$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
  //$mail->Port = 465; // TCP port to connect to
  $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  $mail->Port = 587; // TCP port to connect to

  $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
  $mail->Username = 'USERNAME@gmail.com'; // SMTP username
  $mail->Password = 'PASSWORD'; // SMTP password

  //Recipients
  $mail->setFrom('USERNAME@gmail.com', 'USERNAME');
  $mail->addAddress('SENDER@example.com'); // Name is optional

  //Content
  $mail->isHTML(true); // Set email format to HTML
  $mail->Subject = 'Test mail to user';
  $mail->Body  = 'This is the very simple HTML message body <b>in bold!</b>';
  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

  $mail->send();
  echo 'Message has been sent';
} catch (Exception $e) {
  echo 'Message could not be sent.';
  echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Еще один пример тестового скрипта:

/**
 * This example shows settings to use when sending via Google's Gmail servers.
 * This uses traditional id & password authentication - look at the gmail_xoauth.phps
 * example to see how to use XOAUTH2.
 * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
 */

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require '../vendor/autoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Use `$mail->Host = gethostbyname('smtp.gmail.com');`
//if your network does not support SMTP over IPv6,
//though this may cause issues with TLS

//Set the SMTP port number:
// - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or
// - 587 for SMTP+STARTTLS
$mail->Port = 465;

//Set the encryption mechanism to use:
// - SMTPS (implicit TLS on port 465) or
// - STARTTLS (explicit TLS on port 587)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'username@gmail.com';

//Password to use for SMTP authentication
$mail->Password = 'yourpassword';

//Set who the message is to be sent from
//Note that with gmail you can only use your account address (same as `Username`)
//or predefined aliases that you have configured within your account.
//Do not use user-submitted addresses in here
$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address
//This is a good place to put user-submitted addresses
$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
  echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
  echo 'Message sent!';
  //Section 2: IMAP
  //Uncomment these to save your message in the 'Sent Mail' folder.
  #if (save_mail($mail)) {
  #  echo "Message saved!";
  #}
}

//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl', '*' ) to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
  //You can change 'Sent Mail' to any other folder or tag
  $path = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail';

  //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
  $imapStream = imap_open($path, $mail->Username, $mail->Password);

  $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
  imap_close($imapStream);

  return $result;
}

Если после запуска скрипта отображается такая ошибка: SMTP Error: Could not connect to SMTP host или SMTP ERROR: Failed to connect to server: Network is unreachable, то следует проверить открыт ли SMTP порт в firewall и работоспособность DNS-сервера. Дополнительные сведения по ошибкам находятся на этой странице: https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting.

Настройка аккаунта Google для отправки через SMTP

Чтобы Google разрешил отправку писем через SMTP нужно произвести настройку учетной записи, иначе в логе PHPMailer может появится сообщение об ошибке: SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and then try again (свойство $mail->SMTPDebug должно быть установлено в 1 или 2).

Следует учитывать, что Google разрешает отправлять через свои почтовые аккаунты Gmail не более 99 писем в сутки или до 2000 (500 - пробный аккаунт), с использованием платной версии почтового приложения G Suite. При превышении ограничения на отправку писем по SMTP пользователь теряет возможность отправлять новые письма на 24 часа, но доступ к своему аккаунту остается. Поскольку правила могут измениться в будущем, то посмотреть ограничения можно на https://support.google.com/a/answer/166852?hl=ru.

Google будет автоматически изменять поле «От» любого сообщения, отправленного через SMTP на адрес электронной почты, записанный в настройках аккаунта. Чтобы изменить адрес «от» в настройках аккаунта нужно перейти на вкладку «Аккаунты и импорт» и нажать на «Добавить другой адрес электронной почты», после чего назначить его используемым по умолчанию.