Manage cookies that are used for advertising, such as ad personalization, remarketing, and ad effectiveness analysis.
4.9.3. Check outgoing mail via SMTP
mail.adm.tools
To check for a secure connection, replace
mail.adm.tools with ssl://mail.adm.tools and 25 with 465 on line 34.
flowchart LR
h[Hosting account]-->|Port 25|mail.adm.tools
mail.adm.tools-->|Message|Recipient
- Using file manager or any FTP client in the site root directory, create a
smtp_test.phpfile with this code:<?php $login = 'from@example.com'; // instead of from@example.com specify the address of the mailbox created on hosting $password = 'password'; // instead of password, specify the password of the mailbox created on hosting $to = 'to@example.com'; // instead of to@example.com, specify the recipient's address $text = "Hi, checking the SMTP connection."; // message content // function for getting the server response code function get_data($smtp_conn) { $data = ""; while ($str = fgets($smtp_conn, 515)) { $data .= $str; if (substr($str, 3, 1) == " ") { break; } } return $data; } // forming the header of the message $header = "Date: " . date("D, j M Y G:i:s") . " +0300\r\n"; $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 .= "Message-ID: <12345654321." . date("YmjHis") . "@ukraine.com.ua>\r\n"; $header .= "To: =?UTF-8?Q?" . str_replace("+", "_", str_replace("%", "=", urlencode('To the recipient of the test message'))) . "?= <$to>\r\n"; $header .= "Subject: =?UTF-8?Q?" . str_replace("+", "_", str_replace("%", "=", urlencode('check'))) . "?=\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); // connection to mail.adm.tools mail server via port 25 if (!$smtp_conn) { print "Server connection failed"; fclose($smtp_conn); exit; } $data = get_data($smtp_conn); fputs($smtp_conn, "EHLO ukraine.com.ua\r\n"); // starting of greeting $code = substr(get_data($smtp_conn), 0, 3); // check if the server returned an error if ($code != 250) { print "EHLO greeting error"; fclose($smtp_conn); exit; } fputs($smtp_conn, "AUTH LOGIN\r\n"); // start of authorization procedure $code = substr(get_data($smtp_conn), 0, 3); if ($code != 334) { print "Server did not allow to start authorization"; fclose($smtp_conn); exit; } fputs($smtp_conn, base64_encode("$login") . "\r\n"); // sending the login of the mailbox (on hosting it coincides with the name of the mailbox) $code = substr(get_data($smtp_conn), 0, 3); if ($code != 334) { print "Error accessing such user"; fclose($smtp_conn); exit; } fputs($smtp_conn, base64_encode("$password") . "\r\n"); // sending password $code = substr(get_data($smtp_conn), 0, 3); if ($code != 235) { print "Incorrect password"; fclose($smtp_conn); exit; } fputs($smtp_conn, "MAIL FROM:$login\r\n"); // sending a MAIL FROM value $code = substr(get_data($smtp_conn), 0, 3); if ($code != 250) { print "The server refused the MAIL FROM command"; fclose($smtp_conn); exit; } fputs($smtp_conn, "RCPT TO:$to\r\n"); // sending the recipient's address $code = substr(get_data($smtp_conn), 0, 3); if ($code != 250 AND $code != 251) { print "The server did not accept the RCPT TO command"; fclose($smtp_conn); exit; } fputs($smtp_conn, "DATA\r\n"); // sending the DATA command $code = substr(get_data($smtp_conn), 0, 3); if ($code != 354) { print "Server did not accept DATA"; fclose($smtp_conn); exit; } fputs($smtp_conn, $header . "\r\n" . $text . "\r\n.\r\n"); // sending message body $code = substr(get_data($smtp_conn), 0, 3); if ($code != 250) { print "Message sending error"; fclose($smtp_conn); exit; } if ($code == 250) { print "Message sent successfully. Server response $code"; } fputs($smtp_conn, "QUIT\r\n"); // end the sending with QUIT fclose($smtp_conn); // connection closing ?>⚠️ Be sure to substitute in the script:
- Run the created script by accessing it via a browser at an address like
example.com/smtp_test.php. - Check to see if the test message is in the recipient's mailbox.
Gmail
Important points:
- To connect to your Gmail mailbox, use app password, not your Google account password.
- The script and library from the example do not collect access data or Google account information.
flowchart LR
h[Hosting account]-->|Port 587|Gmail
Gmail-->|Message|Recipient
- Connect to the hosting via SSH.
- Go to the directory of the site where the test script will be located (instead of
example.com/wwwspecify your data):cd ~/example.com/www/ - Install PHPMailer library:
composer require phpmailer/phpmailer - Create] app password to access your Gmail mailbox.
- Using file manager or any FTP client in the site root directory, create a
gmail_test.phpfile with this code:<?php $login = 'from@gmail.com'; // instead of from@gmail.com, specify your Gmail address. $password = 'app_password'; // instead of app_password, specify the created app password $to = 'to@example.com'; // instead of to@example.com, specify the recipient's address 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 Ukraine user'); $mail->addAddress($to, 'John Doe'); $mail->addReplyTo($login, 'Hosting Ukraine 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 via a browser at an address like
example.com/gmail_test.php. - Check to see if the test message is in the recipient's mailbox.