How to Send HTML Mails Using Pear Mail

how to send html mails using PEAR mail

If you follow this example there's no reason it shouldn't work:

<?php
include('Mail.php');
include('Mail/mime.php');

// Constructing the email
$sender = "Leigh <leigh@no_spam.net>";// Your name and email address
$recipient = "Leigh <leigh@no_spam.net>"; // The Recipients name and email address
$subject = "Test Email";// Subject for the email
$text = 'This is a text message.';// Text version of the email
$html = '<html><body><p>HTML message</p></body></html>';// HTML version of the email
$crlf = "\r\n";
$headers = array('From' => $sender, 'Return-Path' => $sender, 'Subject' => $subject);

// Creating the Mime message
$mime = new Mail_mime($crlf);

// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);

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

// Sending the email
$mail =& Mail::factory('mail');
$mail->send($recipient, $headers, $body);
?>

NOTE: in order for the above example to work one needs the Pear Mail Mime Package in addition the Pear Mail one. You can get the package here https://pear.php.net/package/Mail_Mime/download.

Using Pear Mail to send plain text and html emails (How to set Content-Type and use boundary properly)

You have to set the ContentType on your $mime object

$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->setTXTBody($text);
$mime->setContentType($content_type);
$body = $mime->get();

Now I suggest you to use another mailer than one coming from pear. Pear is a bit an old packet manager and some application stop supporting it like PHPUnit and Symfony.

It's better to use another one which could be installed via composer. You can have a look after Swiftmailer

PHP Pear Mail sending plain text instead of HTML format

You are setting your html content as the plain text content:

 ...
$message->setTXTBody($text);
...

You need setHTMLBody() (or both...):

 ...
$message->setHTMLBody($text);
...

PHP Html Email using Pear HTML Table and Pear Email

The problem was with mime type, which require to code again. Basically the message I was sending was not encoded with UTF-8 which I did. This time I user Mail_mimePart instead of Mail_mime and it works treat.

That example was very very helpful

---ahttp://tiger-fish.com/comment/reply/133---

and some of the parameter explanation for Mail_mimePart can be found on below link

ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.mail-mimepart.php---

How to attach the attachment using Mail_mimePart.

ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.addsubpart.php---

Hope it helps.

Pear Mail, how to send plain/text + text/html in UTF-8

I discovered that the headers are supposed to be written differently. In particular, some of them are parameters for the mime object, and not email headers. Then the mime_params array should be passed to the get() function.

This is the correct way to set the headers:

$headers = array(
'From' => 'info@mydomain.com',
'Return-Path' => 'info@mydomain.com',
'Subject' => 'mysubject',
'Content-Type' => 'text/html; charset=UTF-8'
);

$mime_params = array(
'text_encoding' => '7bit',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8',
'head_charset' => 'UTF-8'
);

$mime = new Mail_mime();

$html = '<html><body><b>my body</b></body></html>';
$text = 'my body';

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

$body = $mime->get($mime_params);
$headers = $mime->headers($headers);
$mail_object =& Mail::factory('smtp', $GLOBALS['pear_mail_config']);
$mail_object->send('test@mydomain.com', $headers, $body);


Related Topics



Leave a reply



Submit