Include html in email

后端 未结 4 1338
遇见更好的自我
遇见更好的自我 2020-12-22 12:02

I need to include some HTML things in PHP, for example to add link in a message like this:



        
相关标签:
4条回答
  • 2020-12-22 12:35

    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

    0 讨论(0)
  • 2020-12-22 12:40

    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.

    0 讨论(0)
  • 2020-12-22 12:41

    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

    0 讨论(0)
  • 2020-12-22 12:51

    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 讨论(0)
提交回复
热议问题