简体   繁体   English

PHP电子邮件代码不以HTML格式发送电子邮件

[英]PHP Email Code not sending emails in HTML Format

Sorry, can anyone help please?对不起,有人可以帮忙吗?

We had some PHP code which emailed a person enquiring about our services on our website.我们有一些 PHP 代码,它通过电子邮件发送给在我们的网站上询问我们服务的人。 The email used to give a standard email response back to the enquirer formatted in HTML.用于将标准电子邮件响应返回给 HTML 格式的询问者的电子邮件。 But sometime in June 2020 although we couldn't see that any changes were made, the emails started sending without HTML format and basically showing the HTML code.但是在 2020 年 6 月的某个时候,虽然我们看不到任何更改,但电子邮件开始发送,没有 HTML 格式,并且基本上显示了 HTML 代码。

Below is the part of the PHP code which creates the email but I can't see anything wrong with it.下面是创建电子邮件的 PHP 代码部分,但我看不出有什么问题。 So if there is no issue with the code below, what could cause the code to suddenly break and change the response emails which were working in HTML to start sending as HTML code?因此,如果下面的代码没有问题,什么会导致代码突然中断并更改以 HTML 格式工作的响应电子邮件,以开始作为 HTML 代码发送? (or is there something wrong with the code I can't see?) (或者我看不到的代码有问题吗?)

Any help, pointers or ideas would be really appreciated.任何帮助、指示或想法将不胜感激。

    $email_from = 'example@example.co.uk';//<== update the email address
        $email_subject = "Thank You for making enquiry";
        $email_body = '<!DOCTYPE html>
                <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
                <link href="https://fonts.googleapis.com/css?family=Crimson+Text" rel="stylesheet">
                <html>
                <head>
                    <title></title>
                </head>
                    <body>
                        EMAIL CONTENT WILL GO HERE!
                    </body>
                </html>';
    
        $to = $EnquiryEmailAddress;//<== update the email address
        // Always set content-type when sending HTML email
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type:text/html;charset=ISO-8859-1' . "\r\n";
    
        $headers .= "From: $email_from \r\n";
        $headers .= "Reply-To: $email_from \r\n";
    
        mail($to,$email_subject,$email_body,$headers); 

This code relies on the server outbound email is properly setup.此代码依赖于服务器出站电子邮件是否正确设置。 There is a email processor built in PHP called PHPMailer.有一个用 PHP 构建的电子邮件处理器,称为 PHPMailer。 I encourage you to have a look at that package, it will help you a lot.我鼓励你看看那个包,它会对你有很大帮助。 You can read about it here: https://github.com/PHPMailer/PHPMailer你可以在这里阅读: https : //github.com/PHPMailer/PHPMailer

Secondly, I recommend that you UTF-8 encode your emails instead of using ISO-8859-1 (if possible).其次,我建议您使用 UTF-8 编码您的电子邮件,而不是使用 ISO-8859-1(如果可能)。

Also, if your'e running the mail send command while a web page is rendered, the mail send command might take some time.此外,如果您在呈现网页时运行邮件发送命令,邮件发送命令可能需要一些时间。 A good approach is to batch send email when users are not waiting for a webpage to render.一个好的方法是在用户不等待网页呈现时批量发送电子邮件。 This can be done by (for example) store all outbound emails in a database table and have a batch processor work off that table and send the emails sequentially.这可以通过(例如)将所有出站电子邮件存储在数据库表中并让批处理程序处理该表并按顺序发送电子邮件来完成。

Finally, as mentioned in a comment on your question: beware that HTML in emails is very limited, so you have to be careful when constructing the email HTML.最后,正如对您的问题的评论中提到的:请注意电子邮件中的 HTML 非常有限,因此在构建电子邮件 HTML 时必须小心。

Best of luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM