Popular Posts

Friday, 2 July 2010

Electronic mail

Electronic mail

For orignial post kindly visit www.apepoint.com




Electronic mail, most commonly abbreviated email or e-mail, is a method of exchanging digital messages. E-mail systems are based on a store-and-forward model in which e-mail server computer systems accept, forward, deliver and store messages on behalf of users



An electronic mail message consists of two components, the message header, and the message body, which is the email's content.



message header usually state the senders name, email address, and the date that it was sent.




Internet e-mail messages consist of two major sections:
Header — Structured into fields such as summary, sender, receiver, and other information about the e-mail.
Body — The message itself as unstructured text; sometimes containing a signature block at the end. This is exactly the same as the body of a regular letter.






In these days e-mail is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.





So, you want to send automated email messages from your PHP application. This can be in direct response to a user's action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated.



Note that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your php.ini file to point to your email server.



For sending mail we need to use mail function

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


The below is an example of simple mail

Example : A simple mail function

mail('recipient@some.net', 'Subject', 'Your message here.');
?>

Example : Here is one more example of the same



$to = 'susheel1104@gmail.com';
$subject = 'the subject';
$message = 'hi';
$headers = 'From: webmaster@apepoint.com' . "\r\n" .
'Reply-To: webmaster@apepointcom' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

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

Example An example of sending mail with attachement.



In the below example the variable $filename is the file name which will get attached with the message



$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";


if (mail($mailto, $subject, $body, $header))
{
echo = "mail send ... OK";
}
else
{
echo = "mail send ... ERROR!";
}

}
?>



To add mail priority we need to add few more line in mail header

$headers .= "X-Priority: 1 (Higuest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";

During mail sending we need to provide complete header for more of this i recommend to study MIME Header

No comments:

Post a Comment