• Index
  •  » Articles
  •  » How to send message with the help of PHP

How to send message with the help of PHP

How to send message with the help of PHP

The simplest way to send a message is to use a standard function mail:

bool mail ( string to, string subject, string message 
                            [, string additional_headers [, string additional_parameters]])

Obligatory parameters:

  • Receiver e-mail
  • Message header
  • Message body

Non-obligatory parameters:

  • Message additional headers
  • Additional parameters of the command line.

Return value:

  • true, if message was accepted for delivering
  • false, otherwise

The simplest example:

<?php 
mail
("joecool@example.com""My Subject""Line 1\nLine 2\nLine 3"); 
?> 

If "Fatal error: Call to undefined function: mail()" error appears it means that PHP has been compiled without mail function support or it has been prohibited by server. If you faced with such a problem use sockets for sending mail.

Additional headers of the message can be used for message encoding, sender’s address etc. mentioning. They should look like:

?php 
mail("nobody@example.com", "the subject", $message, 
     "From: webmaster@ example.com \r\n" 
    ."X-Mailer: PHP/" . phpversion()); 
?>

Let’s view more complicated example. Previous scripts with text/plain format. Now we’ll try to send message in HTML format to several addressees with encoding pointing.

<?php 
$to  
"Mary &lt;mary@example.com>, " 
$to .= "Kelly &lt;kelly@example.com>"

$subject "Birthday Reminders for August"

$message 
<html> 
    <head> 
        <title>Birthday Reminders for August</title> 
    </head> 
    <body> 
        <p>Here are the birthdays upcoming in August!</p> 
    </body> 
</html>"


$headers  "Content-type: text/html; charset=windows-1251 \r\n"
$headers .= "From: Birthday Reminder <birthday@example.com>\r\n"
$headers .= "Bcc: birthday-archive@example.com\r\n"

mail($to$subject$message$headers); 
?>

At first we define the receiver. If there are several receivers their addresses are written in the same line and separated by commas. When you define the header and the body of the message pay attention at the encoding they written at, it should be the same as in the charset header.

In our example variable $headers consists of four lines. In the first and second lines we define the type of the message (HTML and its encoding). In the next lines we define sender’s address and address where should be sent the copy of the message.

One of the most frequent errors while sending email in koi8 is message header forming. To solve that problem use the following code:

<?php 
$subject 
"=?koi8-r?B?".base64_encode
(convert_cyr_string($subject"w","k"))."?="
?>

If you made everything correctly but receiver can’t receive it (there are a lot of factors), make sure that it has been really sent. There are two stages. At first write the following code:

?php 
if (mail("nobody@example.com", "the subject", "Example message",   
"From: webmaster@example.com \r\n")) { 
    echo "messege acepted for delivery"; 
} else { 
    echo "some error happen"; 

?>

If at that stage you have an error it means that you haven’t launched sendmail or there are some mistakes in php.ini

In the case if that email has been accepted for delivering try to look at /var/log/mail file. It can be done with the help of tail /var/log/mail command. In the case of successful sending in the log file must appear warning message or lines listed below:

Oct 2 00:21:02 l72 sendmail[131]: h91LL1DG000131: to=root, 
ctladdr=root (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30225, 
relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (h91LL1g1000134 
Message accepted for delivery) 
Oct 2 00:21:18 l72 sendmail[137]: h91LL1g1000134: to=, ctladdr= (0/0), 
delay=00:00:17, xdelay=00:00:16, mailer=local, pri=30774, dsn=2.0.0, stat=Sent

 
  • Top