Sending email in PHP
The mail() function in PHP is prototyped as follows:bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

You’ll notice from the return type of the function that a Boolean is returned upon completion. If PHPsuccessfully passed the email to the SMTP server then true is returned. If an error occurred then false is returned. Please note that even if true is returned the mail may not be sent! (For example if the SMTP is incorrectly configured. In this case you should consult your SMTP server logs to see what went wrong).
To send the email from the previous section – with error checking – we use the following code:
<?php
$to = “johndoe@fakedomain.com”;
$from = “janedoe@anotherfakedomain.com”;
$subject = “This is a test email”;
$message = “Dear John, This is a fake email, I hope you enjoy it. From Jane.”;
$headers = “From: $from ”;
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo “The email to $to from $from was successfully sent”;
} else {
echo “An error occurred when sending the email to $to from $from”;
?>
It is also possible to send an email to multiple recipients with a single call to the mail() function. Here is an example. All you need to do is modify the recipient variable as follows:
<?php
$to = "johndoe@fakedomain.com, somebodyelse@fakedomain.com";
?>
Sending HTML emails in PHP
There is no real trick to sending HTML formatted email. After all, it’s only text – just as normal emailmessages are. The difference is in how email clients interpret that text.
The main step involved is in telling the email client that the email is HTML and not plain text. To do this we simply add the Content-type header. HTML uses the text/html mime type. Plain text emails use text/plain.
Here is an example:
<?php
$to = "johndoe@fakedomain.com";
$from = "janedoe@anotherfakedomain.com";
$subject = "This is a test email";
$message =<<<EOF
<strong>Hello World!</strong>
EOF;
$headers = "From: $from\r\n";
$headers .= "(anti-spam-content-type:) text/html\r\n";
$success = mail($to, $subject, $message, $headers);
if ($success)
echo "The email to $to from $from was successfully sent";
else
echo "An error occurred when sending the email to $to from $from";
?>
Simplifying life with PEAR
In the previous two chapters I showed how to send plaintext and HTML emails. The main limitation with doing HTML as I showed in the previous chapter is that in an email client that doesn’t support HTML email, the user sees a bunch of HTML tags.
The solution to this is to send a multi-part email. That is, within the one message, send a plaintext version and a HTML version. Unfortunately this is complicated with the mail() function as-is.
In order to get around this, we use the Mail_Mime class from the Pear repository. This also allows us to send attachments with our email.
In the following example, we use the Mail_Mime class to send an email with both plain text and HTML, as well as an attachment. Comments are included in the code to explain what is happening.
<?php
require_once('Mail.php'); // These two files are part of Pear,
require_once('Mail/Mime.php'); // and are required for the Mail_Mime class
$to = "johndoe@fakedomain.com";
// the email address of the person receiving the email
$from = "janedoe@anotherfakedomain.com";
// the email address of the sender
$subject = "This is a test email";
// the subject of the email
$attachment = "/path/to/someFile.pdf";
// the path to the file we are attaching to the email
// Next we must build an array of email headers for the email.
// This is structured slightly differently to the mail() function.
// The array key is the header name. Remember that subject
// is a header too!
$headers = array('From' => $from,
'Subject' => $subject);
// Here we create the plaintext version of the email to be sent
$textMessage = "Dear John,\n\nThis is a fake email, I hope you enjoy it.\n\nFrom Jane.";
// Here we create the HTML version of the email to be send, using
// the heredoc syntax
$htmlMessage = <<<EOF
<strong>Hello World!</strong>
EOF;
$mime = new Mail_Mime();
// create a new instance of the Mail_Mime class
$mime->setTxtBody($textMessage);
// set our plaintext message
$mime->setHtmlBody($htmlMessage);
// set our HTML message
$mime->addAttachment($attachment);
// attach the file to the email
$body = $mime->get();
// This builds the email message and returns it to $body.
// This should only be called after all the text, html and
// attachments are set.
$hdrs = $mime->headers($headers);
// This builds the corresponding headers for the plaintext,
// HTML and any other required headers. It also includes
// the headers we created earlier by passing them as an argument.
$mail = &Mail::factory('mail');
// Creates an instance of the mail backend that we can use
// to send the email.
$mail->send($to, $hdrs, $body);
// Send our email, according to the address in $to, the email
// headers in $hdrs, and the message body in $body.
?>
0 comments :