Issues Questions How to send Html type email with multiple attachment

asked on

Hello Sanwebe,

Thank You!

Script of Multiple attachment with plain text is working fine but when use html content type then email is not sending.

I have used below code

$body .= "Content-type:text/html;charset=UTF-8"

But it is not working.

Please check and assist me.

Best Regards,

Mohd Tariq Khan

    Add Answer
    All HTML codes will be stripped in the comment, except inline <code> HTML tag.
    1 Answer
    1 answered on

    What do you mean "Not Working?", is it sending the plain mail alright but not the HTML? May be you've forgot to remove PHP Filter from filter_var($_POST["s_message"], FILTER_SANITIZE_STRING);, this filter remove all HTML tags fom the content. Here's complete code without the filter function.

    if($_POST &amp;&amp; isset($_FILES['file']))
        $recipient_email    = "[email protected]"; //recepient
        $from_email         = "[email protected]"; //from email using site domain.
        $subject            = "Attachment email from your website!"; //email subject line
        $sender_name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING); //capture sender name
        $sender_email = filter_var($_POST["s_email"], FILTER_SANITIZE_STRING); //capture sender email
        $sender_message = $_POST["s_message"]; //capture message
        $attachments = $_FILES['file'];
        //php validation
            die('Name is too short or empty');
        if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
          die('Invalid email');
            die('Too short message! Please enter something');
        $file_count = count($attachments['name']); //count total files attached
        $boundary = md5(""); 
        if($file_count &gt; 0){ //if attachment exists
            $headers = "MIME-Version: 1.0\r\n"; 
            $headers .= "From:".$from_email."\r\n"; 
            $headers .= "Reply-To: ".$sender_email."" . "\r\n";
            $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 
            //html message text
            $body = "--$boundary\r\n";
            $body .= "Content-Type: text/html; charset=UTF-8\r\n";
            $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
            $body .= chunk_split(base64_encode($sender_message)); 
            for ($x = 0; $x &lt; $file_count; $x++){       
                    if($attachments['error'][$x]&gt;0) //exit script and output error if we encounter any
                        $mymsg = array( 
                        1=&gt;"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
                        2=&gt;"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
                        3=&gt;"The uploaded file was only partially uploaded", 
                        4=&gt;"No file was uploaded", 
                        6=&gt;"Missing a temporary folder" ); 
                    //get file info
                    $file_name = $attachments['name'][$x];
                    $file_size = $attachments['size'][$x];
                    $file_type = $attachments['type'][$x];
                    //read file 
                    $handle = fopen($attachments['tmp_name'][$x], "r");
                    $content = fread($handle, $file_size);
                    $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
                    $body .= "--$boundary\r\n";
                    $body .="Content-Type: $file_type; name=".$file_name."\r\n";
                    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
                    $body .="Content-Transfer-Encoding: base64\r\n";
                    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
                    $body .= $encoded_content; 
        }else{ //send plain email otherwise
           $headers = "From:".$from_email."\r\n".
            "Reply-To: ".$sender_email. "\n" .
            "X-Mailer: PHP/" . phpversion();
            $body = $sender_message;
         $sentMail = @mail($recipient_email, $subject, $body, $headers);
        if($sentMail) //output success or failure messages
            die('Thank you for your email');
            die('Could not send mail! Please check your PHP mail configuration.');  

    Here's HTML form for email :

    &lt;form method="post" action="" enctype="multipart/form-data"&gt;
    &lt;span&gt;Your Name&lt;/span&gt; &lt;br /&gt;
    &lt;input type="text" value="Jon Snow" name="s_name" /&gt;&lt;br /&gt;
    &lt;span&gt;Your Email&lt;/span&gt;&lt;br /&gt;
    &lt;input type="email" value=""  name="s_email" /&gt;&lt;br /&gt;
    &lt;span&gt;Message&lt;/span&gt;&lt;br /&gt;
    &lt;textarea name="s_message"&gt;&lt;/textarea&gt;&lt;br /&gt;
    &lt;span&gt;Attachments&lt;/span&gt;&lt;br /&gt;
    &lt;!-- File input fields, you can add as many as required--&gt;
        &lt;input type="file" name="file[]" /&gt;&lt;br /&gt;
        &lt;input type="file" name="file[]" /&gt;&lt;br /&gt;
        &lt;input type="file" name="file[]" /&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;!-- OR --&gt;
    &lt;! -- &lt;input type="file" name="file[]" multiple="multiple" /&gt; --&gt;
    &lt;input type="submit" value="Send" /&gt;
      All HTML codes will be stripped in the comment, except inline <code> HTML tag.

      Can You Answer this? Help us answer the question!

        • Answer
        Supports basic Markdown syntax. Excluded syntaxes (Headers, Lists, Blockquotes, Table, Images)