如何通过使用php发送邮件,方法是将html插入邮件内容中


How to send mail using php by insert html into mail content?

如何使用php发送邮件,方法是将html插入邮件内容?

我试图在$message内插入html代码,当我测试时显示错误

像这样Parse error: syntax error, unexpected 'margin' (T_STRING)

我该怎么办?

<?PHP
include("connect.php");
$email = "test_mail@hotmail.com";    
$to = $email;
$subject = "test subject";
$message = "
<body style="margin: 0; padding: 0;">
    <table border="1" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td>
                <img src="https://i.stack.imgur.com/Jy9QUm.jpg"/>
            </td>
        </tr>
        <tr>
            <td>
                test text
            </td>
        </tr>
    </table>
</body>
";
$headers  = 'MIME-Version: 1.0' . "'r'n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$headers .= 'From: EXAMPLE <noreply@example.com>' . "'r'n";
$headers .= 'Return-Path: return@example.com' . "'r'n";
mail($to, $subject, $message, $headers, '-freturn@example.com');
?>

您在包装$body字符串时遇到问题,请尝试此操作

 <?PHP
    include("connect.php");
    $email = "test_mail@hotmail.com";    
    $to = $email;
    $subject = "test subject";
    $message = "
    <body style='margin: 0; padding: 0;'>
        <table border='1' cellpadding='0' cellspacing='0' width='100%'>
            <tr>
                <td>
                    <img src='https://i.stack.imgur.com/Jy9QUm.jpg'/>
                </td>
            </tr>
            <tr>
                <td>
                    test text
                </td>
            </tr>
        </table>
    </body>
    ";
    $headers  = 'MIME-Version: 1.0' . "'r'n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
    $headers .= 'From: EXAMPLE <noreply@example.com>' . "'r'n";
    $headers .= 'Return-Path: return@example.com' . "'r'n";
    mail($to, $subject, $message, $headers, '-freturn@example.com');
    ?>