简体   繁体   中英

PHP mailer doesn't work in HTML format

I'm using a pretty standard e-mail creating function which I've used before, but it doesn't work for some reason I can't figure out. It sends a blank e-mail no matter what content I put inside it.

function sendToUser($email,$admin_email,$subject,$content){
    $to=$email;

    $random_hash = md5(date('r', time())); 
    $headers = "From: Site Name <$admin_email>";
    $headers .= "\r\nReply-To: $admin_email";
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
    ob_start(); //Turn on output buffering
    ?>
    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/plain; charset="utf-8"
    Content-Transfer-Encoding: 7bit

    <?php echo $content; ?>

    --PHP-alt-<?php echo $random_hash; ?>--
    <?
    //copy current buffer contents into $message variable and delete current output buffer
    $message = ob_get_clean();

    mail($to, $subject, $message, $headers);
}

Now, if I call it like this:

sendToUser("myprivatemail@yahoo.com","admin@site.com","Testing","E-mail content");

It sends the e-mail, but it arrives empty. Does anyone see what's wrong here? Or could it be some server setting I'm unfamiliar with?

--PHP-alt-<?php echo $random_hash; cannot have whitespace before it.

Try this:

function sendToUser($email,$admin_email,$subject,$content){
    $to=$email;

    $random_hash = md5(date('r', time())); 
    $headers = "From: Site Name <$admin_email>";
    $headers .= "\r\nReply-To: $admin_email";
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
    ob_start(); //Turn on output buffering
    ?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

<?php echo $content; ?>

--PHP-alt-<?php echo $random_hash; ?>--
    <?
    //copy current buffer contents into $message variable and delete current output buffer
    $message = ob_get_clean();

    mail($to, $subject, $message, $headers);
}

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