Creating A New Line In $body Of PHP Mail Function

后端 未结 6 815
感情败类
感情败类 2021-01-02 07:50

I\'ve got the PHP mail(): function working for a form I\'m using on my site. I am now trying to format the $body attribute to look nicer and more organized when I receive th

6条回答
  •  被撕碎了的回忆
    2021-01-02 08:28

    I couldn't add a comment to Mohammad Saqib's answer, but I wanted to say that you shouldn't use
    as a replacement for \r\n because this may cause the email to be caught by 'line too long' spam filters. New lines are important to keep the email more human and allow successful delivery!

    The method I recommend is:

    $output[] = "line 1";
    $output[] = "line 2";
    $EmailBody = join("\r\n",$output);
    

    Also you must make sure you 'sanitize' headers to avoid header injection from user input, here is a function that is double sure to stop injection:

    function sanitize(&$data){
       return str_ireplace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($data));
    }
    
    
    $headers = 'From: '.sanitize($email);
    

    If you wanted HTML tags to show in the email, change the type using the header:

    $headers = 'Content-type: text/html;\r\nFrom: '.sanitize($email);
    

    If you did display HTML, you should clean the user input to avoid anything unexpected... i.e. HTML injection. Use this function anywhere you display user input in HTML:

    function htmlclean($str){
        return htmlentities($str, ENT_QUOTES);
    }
    

    e.g. $body = "Brand Name: " . htmlclean($brandname);

提交回复
热议问题