【问题标题】:How to send mail using php by insert html into mail content? [duplicate]如何通过将 html 插入邮件内容来使用 php 发送邮件? [复制]
【发布时间】:2016-02-04 17:27:04
【问题描述】:

如何通过将html插入邮件内容来使用php发送邮件?

我尝试在$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="http://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');
?>

【问题讨论】:

  • 用单引号代替双引号将您的消息括起来
  • 或转义它们\"string\" - 它更长,但语法有效。
  • 下面给出的答案,回答了您发布的内容的问题,应标记为正确。

标签: php html email


【解决方案1】:

您在包装 $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='http://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');
    ?>

【讨论】:

  • 我不知道您为什么要费尽心思来更改引号。只需$message = ''; - 像晚餐一样完成 ;-)
  • @Fred-ii- haha​​hahahaha 确实没注意到!!!! +1
  • 哦,已经完成了 ;-) 干杯
猜你喜欢
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
相关资源
最近更新 更多