【问题标题】:send html format through php mail() [duplicate]通过php mail()发送html格式[重复]
【发布时间】:2019-01-03 02:09:06
【问题描述】:

这是在代码点火器中。为了发送邮件,我使用 php mail() 编写了一个函数。代码是:

function mailsend()
{
    $to="mymail@gmail.com";
    $subject="test";
    $message="<table>
                    <tr>
                        <td>Hello</td>
                        <td>World</td>
                    </tr>
                </table>";
    echo $message;

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

当我从服务器端发送邮件时,我在邮件中获得了 html 代码,而不是真正的表格。但是$messageshows 是浏览器中的真实表格。 mail() 有什么方法可以获取邮件中的真实表格吗?

【问题讨论】:

    标签: php jquery email


    【解决方案1】:

    您可以通过设置标题部分在电子邮件中发送 html 内容。以下是您可以使用的示例代码。

    <?php
    $to = 'maryjane@email.com';
    $subject = 'Marriage Proposal';
    $from = 'peterparker@email.com';
    
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    // Create email headers
    $headers .= 'From: '.$from."\r\n".
        'Reply-To: '.$from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    // Compose a simple HTML email message
    $message = '<html><body>';
    $message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
    $message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
    $message .= '</body></html>';
    
    // Sending email
    if(mail($to, $subject, $message, $headers)){
        echo 'Your mail has been sent successfully.';
    } else{
        echo 'Unable to send email. Please try again.';
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      要使用mail 功能在您的电子邮件中发送html 内容,您需要向其添加标题选项,如下所示:

      $headers =  'From: test@yourdomain.com' . "\r\n" .
                          'Reply-To: test@yourdomain.com' . "\r\n" .
                          "Content-type:text/html;charset=UTF-8" . "\r\n" .
                          'X-Mailer: PHP/' . phpversion();
      
      mail($sendTo, $subject, $message, $headers);
      

      【讨论】:

        【解决方案3】:

        像这样在您的邮件功能中添加一个标题

        $headers =  'From: test@yourdomain.com' . "\r\n" .
                            'Reply-To: hello@example.com' . "\r\n" .
                            "Content-type:text/html;charset=UTF-8" . "\r\n" .
                            'X-Mailer: PHP/' . phpversion();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-25
          相关资源
          最近更新 更多