Using Mail_Mime

0
Your rating: None

PHP's built-in mail() function is simple to use, but it does have its shortcomings. For starters, you cannot use it with an SMTP server that requires authentication. Second, it’s not very easy to use it for sending HMTL email or email with attachments. A solution is to use PEAR’s Mail and Mail_Mime classes.

In layman’s terms, Mail_Mime turns the information you need to send—be it HTML or attachments—into the proper syntax for an email message. The Mail class is then used to send that message.

Sending HTML email

To start, you’ll need to include two class definitions (after having installed both PEAR packages, of course):

require_once (‘Mail.php’);
require_once (‘Mail/mime.php’);

You’ll also want to establish your message body. For HTML emails, you should create a plain text and an HTML equivalent. For the HTML content, it should be a complete HTML document:

$text = 'Plain text version';
$html = 'Your HTML part';

Now create a new Mail_Mime object:
$mime = new Mail_Mime();

Using the object’s methods, set the email’s body for both the plain text and the HTML versions:

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

Before sending the email, the extra headers have to be generated: who the email is from and what the subject is. There are separate methods for each:

$mime->setFrom('to@kiran.org.in');
$mime->setSubject('Email Subject');

You can also use addCc() and addBcc() to carbon copy and blind carbon copy other
addresses. Now that all this information has been stored in the object, use the get() and headers() methods to fetch the generated code for use with the Mail class:

$body = $mime->get();
$headers = $mime->headers();

Note that you must call these two functions in this order as the headers() method partly uses the result of the get() method.

The result is two pieces of data that can be used in an email (print their values to see the end result). Finally, the Mail class can be used to send the actual email.
To do so, you don’t actually create an object of type Mail but rather invoke the class’s factory() method directly:

$mail =& Mail::factory($backend);

For the $backend value, this can be: mail (PHP’s built-in mail() function); sendmail (if installed on the server); or smtp (to use an SMTP server). Assuming you can use the mail() function or sendmail on the server already, either of those options is fine.

The final step is to send the email, providing the to address, the headers, and the body:

$mail->send('to@kiran.org.in', $headers,$body);

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

SEO Architect,SEO Specialist reflect my values.

By enduring black hat methods the SEO industry is setting itself up for washout and sleepwalking into oblivion.

Jobs in India