user1406071
user1406071

Reputation: 627

Send an Email with PHP & SMTP - Both Text & HTML

I have a code with I'm using to send PHP emails to my recipients. I have found a way to send them a HTML email by including the 'Content-Type' => "text/html" in the header's section. My question is that how will those receive my email, who can't display HTML emails? They will see the HTML code? If so, than how can my mailer application decide, when to send the Plain and when to send the HTML formation?

Here is my code for instance:

<?php
 require_once "Mail.php";

 $from = "Example <[email protected]>";
 $to = "[email protected]";
 $subject = "Hi!";
 $body = '<html>This is a HTML message...</html>'

 $host = "smtp";
 $username = "user";
 $password = "pass";

 $headers = array ('From' => $from,
   'To' => $to,
   'Content-Type' => "text/html",
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

 $mail = $smtp->send($to, $headers, $body);

 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?>

Upvotes: 2

Views: 5095

Answers (1)

Piotr
Piotr

Reputation: 79

Here is a class that I always use on my projects: http://phpmailer.worxware.com/ It's quite good and allows sending HTML and TEXT emails and supports attachments. If you are limited to built in functions only, you should include boundaries that define where HTML and TEXT versions start. More info here: http://webcheatsheet.com/PHP/send_email_text_html_attachment.php

Upvotes: 1

Related Questions