Home / code / php

Send email with PHP

You can easily send mail with php.

For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.
Normally this is all been done by you server administrator.

The function you need to send mail is mail().

mail("email@domain.com",$title,$mail_body)
The above example works, but is very basic. You can include headers for more options:
$headers  ="MIME-Version: 1.0\r\n";
$headers .="Content-type: text/html; charset=iso-8859-1\r\n";
$headers .="From: me@myhost.com";

mail("email@domain.com",$title,$mail_body,$headers)
The first 2 header lines, say in what 'language' the mail is written.
The From: lines say who sends the mail.

The first 2 lines are needed if you want to include HTML-tags in your mail.

Here are some more line you can add in the header :

$headers .= "Reply-To: othermailaddress@mail.com\r \n";
$headers .= "X-Priority: 3\r \n"; 
$headers .= "X-MSMail-Priority: High\r \n"; 
$headers .= "X-Mailer: Just My Server\r \n";
$headers .= "Cc: other_email\r \n";
$headers .= "Bcc: another_email \r\n";
Note : If you use BCC always put it as the last header line, it might give problems.

 

TOP

Latest script:

 

Books: