Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to create an autoresponder email and the content is a PHP variable. I want it to output HTML code and right now it's not.

//Example:
$respondmessage = " Hello $fullname,
 We are confirming your Appointment today!
Please <a href="http://yourlink.com/">click here to confirm</a>!
";

This outputs:

Hello Your Name,
 We are confirming your Appointment today!
Please <a href="http://yourlink.com/">click here to confirm</a>!

In the email. Is there a way to make the email accept HTML code?

share|improve this question
How are you sending the email? You can send emails in HTML format, but by default they'll be plain text. – andrewsi yesterday
2  
Show the entire relevant PHP code. BTW you forgot to put backslash before the double-quotes – Dor yesterday

3 Answers

You can following line as an header in your code :

$headers = "Content-type: text/html\r\n";

This will help you to create email into HTML

share|improve this answer

From http://php.net/manual/en/function.mail.php example# 4

<?php

// multiple recipients
$to = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>
share|improve this answer

Assuming you're using the built in mail function in PHP:

<?php
// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

This example is from: http://us2.php.net/manual/en/function.mail.php

The important part is the $headers variable.

By the way, as andrewsi mentioned - you need to escape your quotes:

$respondmessage = "Hello $fullname,
    We are confirming your Appointment today!
    Please <a href=\"http://yourlink.com/\">click here to confirm</a>!";
share|improve this answer
This looks like what I need! Thank you! I will test this out in an hour or so and let you know how it works for me! I looked up something earlier and it said something about the charset but couldn't find a definite answer. Thanks again! Will let you know! – Phil Mulkins yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.