【问题标题】:How can i send an e-mail with HTML content [duplicate]如何发送包含 HTML 内容的电子邮件 [重复]
【发布时间】:2014-04-28 15:41:09
【问题描述】:

我正在尝试发送带有 html 内容的电子邮件。

我想在上面插入一些图片,但我不知道该怎么做,这里是我的代码:

<?php
if(isset($_POST['submit'])){
$to      = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: myemail@gmail.com' . "\r\n" .
    'Reply-To: '.$to.' ' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

}

?>

<form method="POST">
<input type="text" placeholder="from" name="from" />

<input type="text" placeholder="to" name="to" />
<input type="text" placeholder="Subject" name="subject" />
<textarea type="text" placeholder="Message" name="message"></textarea>
<input name="submit" type="submit" />
</form>

谢谢大家

【问题讨论】:

标签: php html email


【解决方案1】:

只需要将适当的标头传递给邮件函数:

// Send HTML Message
$headers  = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
$headers .= 'From: example name <example@example.com>' . PHP_EOL;
mail('to@example.com', 'Subject', $output, $headers);

【讨论】:

    【解决方案2】:
        $to = "$email";
        // Change this to your site admin email
        $from = "admin@MYSITE.COM";
        $subject = "Welcome to MYSITE.COM";
        //Begin HTML Email Message where you need to change the activation URL inside
        $message = '<html>
        <body bgcolor="#FFFFFF">
        Dear ' . $firstname . ',
        <br /><br />
        Thank you for joining MYSITE.COM. 
        <br /><br />
        You can now start shopping on our website and enjoy all of our services. 
        Please click on the link bellow to go to our website &gt;&gt;
        <a href="http://www.MYSITE.COM">http://www.MYSITE.COM</a>
        <br /><br />
        Your Login Details is as follows: 
        <br /><br />
        E-mail Address: ' . $email . ' <br />
        Password: ' . $password . ' 
        <br /><br /> 
        Regards
        MYSITE.COM
        </body>
        </html>';
        // end of message
        $headers = "From: $from\r\n";
        $headers .= "Content-type: text/html\r\n";
        $to = "$to";
        // Finally send the activation email to the member
        mail($to, $subject, $message, $headers);
    

    您可以根据需要编辑代码。但它应该可以帮助您了解如何直接从 PHP 发送 HTML 电子邮件。

    【讨论】: