Include html in email

谁都会走 提交于 2019-12-18 09:46:14

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答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)



来源:https://stackoverflow.com/questions/4916477/include-html-in-email

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!