简体   繁体   中英

PHP mail(); Send e-mail as plain text AND a HTML

I've the following PHP code for sending e-mails with PHP mail() as HTML :

<?php
    $to = "test@example.com";

    $subject = "This is the subject";

    $headers = "From: Me<test@exapmle.com>\r\n";
    $headers .= "Return-Path: test@exapmle.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    $headers .= "X-Priority: 1\r\n";

    $message = "This is my <b>message</b>!";

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

But I want to support plain text and HTML. HTML for things like bold font and plain text to show This is my message! in non-html supporting email clients instead of This is my <b>message</b>! .

How can I do this?

I read about boundary. Is that what I'm looking for? But if so, how to use it? I don't understand it.

I would strongly suggest you to use existing library like PHPMailer ( https://github.com/PHPMailer/PHPMailer ), which could do everything you need and even more.

$mail = new PHPMailer;
$mail->isHTML(true);
$mail->Body = '<b>Html body</b> here.';
$mail->AltBody = 'Normal text here.';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM