25

I am using PEAR mail system to send authenticated mails.I need to send HTML mails that has alinks.It was working fine before i started using PEAR mail.Now i am not able to send HTML mails.

mail body looks like this:

$body = <<<EOD

Hiya $username

You might be interested in the current 'haves' and 'wants' on example.com

Latest Haves
<a href="http://www.exmaple.com/product/have/64/Titan+Fast+Track+SunGlass">Titan Fast Track SunGlass</a>

EOD;

a tag appears as it is in the mail.Any idea how to solve this??Pls help..

Sakura
  • 279
  • 2
  • 4
  • 9
  • Take a look at the Mail_mime package. It allows you to include both a html- and plaintext-version of the e-mail: http://pear.php.net/manual/en/package.mail.mail-mime.example.php – nikc.org Sep 01 '09 at 10:50

3 Answers3

33

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.

John
  • 1
  • 13
  • 98
  • 177
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 1
    what about the SMTP server configuration ?, do you have a link for that ? – Francisco Corrales Morales May 14 '14 at 14:15
  • 1
    Hi, I read https://pear.php.net/manual/en/package.mail.mail.send.php, there is `To` in the `$headers`. I wonder what's the difference between recipient email `$recipient` in `send()` method and in `$headers` variable? Is it necessary to put `To` in the `$headers`? – stenlytw Mar 16 '16 at 15:55
  • @karim79 - Could you please help me with http://stackoverflow.com/questions/39785613/html-email-through-ses-smtp-interface-pear ? – S R Sep 30 '16 at 07:16
  • How do you handle errors with this? What happens if the email doesn't send, or the SMTP server goes down? This code doesn't account for either scenario. (I bring this up because I'm looking for an example that has error handling.) – Tiffany Jan 19 '17 at 17:33
  • @John I was hoping you could help me out with this issue https://stackoverflow.com/questions/58309553/how-to-include-php-html-file-in-pear-mail Thanks – Jay Smoke Oct 09 '19 at 17:44
19

What do your headers look like? Here are mine:

$headers = array(
    'To' => $recipients,
    'From' => $adminEmail,
    'Subject' => $subject,
    'MIME-Version' => 1,
    'Content-type' => 'text/html;charset=iso-8859-1'
);
story
  • 729
  • 2
  • 9
  • 22
3

Please note that the example posted by karim79 has a header parameter that may cause you much grief: "Return-Path" - when I included this parameter like the example it prevented me from adding a from name, only a sender email address worked.

Specifically (when I added a debug param to see what was happening) there were extra angle brackets added around the from name so it tried to send this to the smtp server:

From: <from name <name@domain.com>> or
From: <"from name" <name@domain.com>> when I tried using quotes.
This caused the smtp connection to quit with an error of invalid address.

Also when using the mime_mail class you need to specify the "To:" parameter in the headers or it will appear to be sent to undisclosed addresses when you receive it. So replace the Return-Path param with a To param and it will work.

Robert
  • 137
  • 3
  • 17