CityHost.UA
Help and support

How to check sending emails using PHP and SMTP methods

To objectively check the sending of letters from the site, you can use a test php script and a mail service that is not sensitive to letters that can be recognized as spam.

1. Go to the [file manager] of the desired site and create a file named mail.php in the root folder

2. Open the created file and place inside the following php code for sending emails - PHP or SMTP, depending on which sending method you need to check.

3. Get a temporary mailbox to check the delivery of letters using any mail service, we recommend temp-mail.org . After opening the site, a temporary email will be generated immediately, use this email in the next step for the field to whom to send the letter.

4. In the file code, edit the fields from whom and to whom to send the test letter according to the instructions, then save the changes in the file.

5. Access the file through a browser — vash-sayt.com.ua/mail.php

6. The result of the script execution will be immediately displayed on the page: green text will indicate successful sending, red text will indicate failure. In case of successful sending, you will see the letter on the page of the temporary mail service from point 3. In case of failure, check whether the fields in the script are filled in correctly.

Delivery check by PHP MAIL method

FROM@EXAMPLE.COM — enter the email, necessarily in the domain of the site

TO@EXAMPLE.COM — specify the email where the letter should be sent (temporary email from point 3)

 <?php $from = "FROM@EXAMPLE.COM"; $to = "TO@EXAMPLE.COM"; $subject="PHP лист з сайту ".$_SERVER["SERVER_NAME"];$message="версія php на сайті ".phpversion();$headers="From: ".$from."\r\n";$headers.="Content-Type: text/plain; charset=utf-8\r\n";if(mail($to,$subject,$message,$headers)){echo"<h1 style='color:green'>Листа успішно відправлено на ".$to." о ".date("H:i:s")." з сайту ".$_SERVER["SERVER_NAME"];}else{echo"<h1 style='color:red'>Помилка! Листа не відправлено";}

SMTP delivery verification

FROM@EXAMPLE.COM - specify the mailbox created on the hosting [in the mail section]

PASSWORD - enter the password for the created mailbox

TO@EXAMPLE.COM — specify the email where the letter should be sent (temporary email from point 3)

 <?php $user = 'FROM@EXAMPLE.COM'; $pass = 'PASSWORD'; $host = 'ssl://MX1.CITYHOST.COM.UA'; $port = 465; $to = 'TO@EXAMPLE.COM'; $template="Subject: SMTP лист з сайту ".$_SERVER['SERVER_NAME']."\r\n"."To: $to\r\n"."From: $user\r\n"."Content-Type: text/plain; charset=utf-8\r\n"."Content-Transfer-Encoding: 7bit\r\n"."Лист від SMTP користувача $user\r\n.";if(smtp_mail($to,$template,$user,$pass,$host,$port)){echo"<h1 style='color:green'>Листа успішно відправлено на ".$to." о ".date("H:i:s")." з сайту ".$_SERVER["SERVER_NAME"];}else{echo"<h1 style='color:red'>Помилка! Листа не відправлено";}function smtp_mail($to,$template,$user,$pass,$host,$port){if($h=fsockopen($host,$port)){$data=array(0,"EHLO $host",'AUTH LOGIN',base64_encode($user),base64_encode($pass),"MAIL FROM: <$user>","RCPT TO: <$to>",'DATA',$template);foreach($data as $c){$c&&fwrite($h,"$c\r\n");while(substr(fgets($h,256),3,1)!=' '){}}fwrite($h,"QUIT\r\n");return fclose($h);}}

Similar articles