+7 (495) 668-09-95

Knowledge base

Problems with PHP Mailing Scripts

Mail does not reach the recipient (or arrives after a long delay)

If your PHP works as an Apache web server module, then check the domain administrator's registered email address. In ISPmanager, go to "Domains" -> "www-domains", select the desired domain and click the "Edit" button on the toolbar.

In the pop-up window, check the "Administrator Email" box - it must indicate the current locally registered e-mail address. If the field is filled out incorrectly, the email is sent from www@localhost and will be blocked by the recipient's spam filters.

500 Internal Server Error

If you are working with PHP as CGI, verify the administrator email as described above, then open the php.ini file and check that the following line is present and correctly written:

sendmail_path = "/usr/sbin/sendmail -t -i -f webmaster@o-smaug.ru"

where webmaster@o-smaug.ru -  is the current administrator email locally registered for the server.

If the php.ini file is missing, create it in the /home/user_name/data/php-bin/ directory, where user_name is the name of the user listed as the website owner.

PHP Mailing Scripts

PHP mailing feature - mail()

The most basic mailing script is as follows:

<?php

 mail("joecool@example.com", "My Subject", "Line 1\nLine 2\nLine 3");

?>

More sophisticated version:

<?php

if($_POST['submit']) {       //check whether "Send" button is pressed

        $title = substr(htmlspecialchars(trim($_POST['title'])), 0, 1000);

        $mess =  substr(htmlspecialchars(trim($_POST['mess'])), 0, 10000);

        $to = 'test@test.ru';      //recipient

        $from='test@test.ru';   //sender

        mail($to, $title, $mess, 'From:'.$from);

        echo 'Letter has been sent.';

}

?>

Even more sophisticated version:

<?php

if((isset($_POST['name'])&&$_POST['name']!="")&&(isset($_POST['phone'])&&$_POST['phone']!="")){

        $to = 'mail@yandex.ru';

        $subject = 'Dial back'; //Message header

        $message = '

                <html>

                    <head>

                        <title>'.$subject.'</title>

                    </head>

                    <body>

                        <p>Name: '.$_POST['name'].'</p>

                        <p>Phone: '.$_POST['phone'].'</p>                       

                    </body>

                </html>';

        $headers  = "Content-type: text/html; charset=utf-8 \r\n";

        $headers .= "From: Sender <from@example.com>\r\n";

        mail($to, $subject, $message, $headers);

}

?>
Return to category