开发者

Include html in email

I need to include some HTML things in PHP, for example to add <a href="#">link</a> in a message like this:

<?php
$to = $themail;
$subject = "Expiration d'une annonce";
$body = "Hey,\n\n";                     
// I need to include a link here in the body like <a href 开发者_运维知识库="http://www.www.com"> Link </a>
mail($to, $subject, $body)
?>

Any ideas?


I suggest using PHPMailer, easy to use, takes care of all nesseccery headers, easy attachment sending, multiple recipients etc.

http://phpmailer.worxware.com/index.php?pg=phpmailer


This is very basic: mail()

Set the correct headers (from php.net)

// 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);

your $message may now contain HTML. For complex html/emails, it's advisable to look at some packages such as the PEAR Mailer class for instance.


I didn't understand. You need to echo to the html like this ?

echo '<a href ="http://www.www.com"> Link </a>';

Or do you need to do this :

$body .= '<a href ="http://www.www.com"> Link </a>';

What's exactly that you are trying to do ?

If you are trying to send HTML data via mail(), you need to set few headers

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
    mail($to, $subject, $body, $headers);

For more information - check http://php.net/manual/en/function.mail.php example 4


php.net/mail has a lot of examples

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// 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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

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

I also found this article useful:

PHP: Sending Email (Text/HTML/Attachments)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜