如何发送包含HTML内容的电子邮件

时间:2014-04-28 15:41:10

标签: php html email

我正在尝试发送包含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>

谢谢你们

2 个答案:

答案 0 :(得分: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电子邮件。

答案 1 :(得分:2)

只需将相应的标题传递给邮件功能:

// 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);