2.19.11. Checking outgoing mail via SMTP
mail.adm.tools
To check the secure connection in line 34, replace
mail.adm.tools
on ssl://mail.adm.tools
and 25
on 465
.
flowchart LR
Хостинг-аккаунт-->|Port 25|mail.adm.tools
mail.adm.tools-->|Letter|Recipient
- Through filemanager or any FTPclient in root directory of the site create a file
smtp_test.php
with code like this:- <?php
- $login = 'from@example.com'; // вместо from@example.com укажите адрес созданного on хостинге почтового ящика
- $password = 'password'; // вместо password укажите password созданного on хостинге почтового ящика
- $to = 'to@example.com'; // вместо to@example.com укажите адрес получателя
- $text = ""Hi, checking the SMTP connection.""; // содержимое отправляемого письма
- // функция получения кода ответа сервера
- function get_data($smtp_conn) {
- $data = "";
- $data .= $str;
- break;
- }
- }
- return $data;
- }
- // формирование служебного заголовка письма
- $header .= "From: =?UTF-8?Q?" . str_replace("+", "_", str_replace("%", "=", urlencode('Test script'))) . "?= <$login>\r\n";
- $header .= "X-Mailer: Test script hosting Ukraine.com.ua \r\n";
- $header .= "Reply-To: =?UTF-8?Q?" . str_replace("+", "_", str_replace("%", "=", urlencode('Test script'))) . "?= <$login>\r\n";
- $header .= "X-Priority: 3 (Normal)\r\n";
- $header .= "To: =?UTF-8?Q?" . str_replace("+", "_", str_replace("%", "=", urlencode('To the recipient of the test letter'))) . "?= <$to>\r\n";
- $header .= "Subject: =?UTF-8?Q?" . str_replace("+", "_", str_replace("%", "=", urlencode('examination'))) . "?=\r\n";
- $header .= "MIME-Version: 1.0\r\n";
- $header .= "Content-Type: text/plain; charset=UTF-8\r\n";
- $header .= "Content-Transfer-Encoding: 8bit\r\n";
- $smtp_conn = fsockopen("mail.adm.tools", 25, $errno, $errstr, 10); // соединение с почтовым сервером mail.adm.tools через порт 25
- $data = get_data($smtp_conn);
- fputs($smtp_conn, base64_encode("$login") . "\r\n"); // отправка логина от почтового ящика (on хостинге он совпадает с именем почтового ящика)
- if ($code == 250) { print ""The letter was sent successfully. Server response $code"; }
- ?>
Attention! Be sure to substitute in the script:
- Run the created script by accessing it through the browser at an address like
example.com/smtp_test.php
. - Check for a test email in the recipient’s mailbox.
Gmail
Important points:
- To connect to your Gmail mailbox, use application password, not your Google account password.
- The example script and library do not collect access data or Google account information.
flowchart LR
Хостинг-аккаунт-->|Port 587|Gmail
Gmail-->|Letter|Recipient
- Connect to hosting via SSH.
- Go to the site directory where the test script will be placed (instead of
example.com/www
enter your details):cd ~/example.com/www/
- Install PHPMailer library:
composer require phpmailer/phpmailer
- Create application password to access your Gmail mailbox.
- Through filemanager or any FTPclient in root directory of the site create a file
gmail_test.php
with code like this:- <?php
- $login = 'from@gmail.com'; // вместо from@gmail.com укажите адрес вашего почтового ящика Gmail
- $password = 'app_password'; // вместо app_password specify the created application password
- $to = 'to@example.com'; // вместо to@example.com укажите адрес получателя
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\SMTP;
- use PHPMailer\PHPMailer\Exception;
- require_once __DIR__ . '/vendor/phpmailer/phpmailer/src/Exception.php';
- require_once __DIR__ . '/vendor/phpmailer/phpmailer/src/PHPMailer.php';
- require_once __DIR__ . '/vendor/phpmailer/phpmailer/src/SMTP.php';
- $mail = new PHPMailer(true);
- try {
- $mail->SMTPDebug = SMTP::DEBUG_SERVER;
- $mail->isSMTP();
- $mail->Host = 'smtp.gmail.com';
- $mail->SMTPAuth = true;
- $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
- $mail->Port = 587;
- $mail->Username = $login;
- $mail->Password = $password;
- $mail->setFrom($login, 'Hosting.XYZ LTD user');
- $mail->addAddress($to, 'John Doe');
- $mail->addReplyTo($login, 'Hosting.XYZ LTD user');
- $mail->Subject = "Gmail SMTP test";
- $mail->Body = 'Hi, test Gmail SMTP connection';
- $mail->send();
- echo "Email message sent.";
- } catch (Exception $e) {
- echo "Error in sending email. Mailer Error: {$mail->ErrorInfo}";
- } finally {
- $mail->smtpClose();
- }
- Run the created script by accessing it through the browser at an address like
example.com/gmail_test.php
. - Check for a test email in the recipient’s mailbox.