Skip to main content
Help Center
The GoDaddy Community will undergo maintenance starting on Tuesday, August 3rd at 3pm PST / 6pm EST. Learn more
Showing results for 
Show  only  | Search instead for 
Did you mean: 

send email with attachment by php form


Currently I am creating a form page on the website. User can attach multiple files to a form and receive an email with attached files. However, the form that worked until few days ago is now not available on all hosting domain webpages. So I want to know how can I solve this problem.

Here is my basic code.

if($_POST && isset($_FILES['file']))
    $recipient_email    = ""; //recepient
    $from_email         = ""; //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 = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING); //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 > 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";
        //message text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= chunk_split(base64_encode($sender_message));

        for ($x = 0; $x < $file_count; $x++){      
                if($attachments['error'][$x]>0) //exit script and output error if we encounter any
                    $mymsg = array(
                    1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
                    2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
                    3=>"The uploaded file was only partially uploaded",
                    4=>"No file was uploaded",
                    6=>"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.');  
<form method="post" action="test.php" enctype="multipart/form-data">
<span>Your Name</span>
    <input type="text" value="" name="s_name" />
<span>Your Email</span>
    <input type="email" value=""  name="s_email" />
    <textarea name="s_message"></textarea>
<!-- File input fields, you can add as many as required-->
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
<!-- OR -->
<input type="submit" value="Send" />

Can you tell me what the problem is?

Community Manager

Hi @beginner. Thanks for being part of GoDaddy Community! I wouldn't be able to comment on anything in your code, but one thing you might want to check is the status of your hosting account. It's sometimes possible for email to be suspended for an account. You can reach out to our support team to confirm that email is still working for you. If that's not the case, then hopefully other community members will have additional suggestions for you. 


JesseW - GoDaddy | Community Manager | 24/7 support available at | Remember to choose a solution and give likes.