Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:52:33+00:00 2026-05-24T11:52:33+00:00

I am using following code, copied from a website, for a contact page. But

  • 0

I am using following code, copied from a website, for a contact page. But I am not getting any emails and the attached files don’t find their way to my webserver folder /var/www/. Am I missing something here?

I am running this code on localhost.

email.php

ob_start();

$to = 'baltusaj@gmail.com';

/*$name = $_POST['name'];
$email = $_POST['email'];
$confirm_email = $_POST['confirm_email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$hidden = $_POST['hidden'];
$from = $email;
*/
$keys = array('name', 'email', 'confirm_email', 'subject', 'comments', 'hidden');
foreach($keys as $key)
{
    $$key = isset($_POST[$key]) ? $_POST[$key] : null ;
} 

print ('
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
     <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
       <title>Email</title>
<script type="text/javascript">
/*<![CDATA[*/
function progress(){
intWidth = parseInt(document.getElementById("container1").style.width) + 1;
if(intWidth <= 400){
     document.getElementById("container1").style.width = intWidth+"px";
}else{
     document.getElementById("container1").style.width = 0;
}
     setTimeout("progress()",300);
}
/*]]>*/
</script>
</head>

<body>
');

//Make sure email and confirm email are the same
if (!empty ($hidden)) {
    if ($email == $confirm_email) {
    }else{
    $email = '';
    $confirm_email = '';
    }
}


//Do a reg_ex check on the email
if (!empty ($hidden)) {    
   $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
   if (eregi($regexp, $email))
   {
   }else{
       $email = '';
    $confirm_email = '';
   }
}
// End of email checking




if (empty ($hidden)) {

print ('<div id="container2">
<h1 id="content_h1"><a name="text">    E-mail </a></h1> <p>Please use the following form to e-mail us:</p>

');

include ("form.php");    

print ('</div>

<div id="container0" style="display: none;">

<p style="font-size: 15pt; font-family: sans-serif; color:#fd6700; background:#fff;">
    Loading...
    </p>

<div id="container1" style="width:0px; height:5px; background-color:#fd6700; margin-top:0px; text-align: left;"></div>

<p>Please be patient while your data is processed. This may take a few moments especially if you are uploading a file.</p>

</div>
');

}

if (!empty ($hidden)) {

    if ($_FILES['fileatt']['error'] == 1){
        print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1>  
                <p>The maximum file size that can be uploaded using this form is 2 megabytes.
                </p>');

    }elseif ( (!empty ($name)) && (!empty ($email)) && (!empty ($comments))&& (!empty ($subject))) {

    // Get html message content
$form_data = "<p>This email is from <span class=\"bold\">$name</span> \n\n ";
$form_data .= "<p>$comments</p>";

$message =         "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" .
                "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" .
                "<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n" .
                "<head> \n" .
                "  <meta http-equiv=\"content-type\" content= \n" .
                "  \"text/html; charset=iso-8859-1\" /> \n" .
                "<style type=\"text/css\"> \n" .
                "body {    font-size: 9pt; font-family:  verdana, sans-serif;     color: #000; background:#fff; }  \n" .
                ".bold { font-weight: bold; }  \n" .
                "</style>  \n" .
                "</head> \n" .
                "<body>$form_data \n" .
                "</body> \n" .
                "</html> \n\n";

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
             "Content-Type: multipart/mixed;\n" .
             " boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the html message
$message = "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $message . "\n\n";


// Base64 encode the file data
$data = chunk_split(base64_encode($data));

//We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
             "Content-Type: {$fileatt_type};\n" .
             " name=\"{$fileatt_name}\"\n" .
             "Content-Disposition: attachment;\n" .
             " filename=\"{$fileatt_name}\"\n" .
             "Content-Transfer-Encoding: base64\n\n" .
             $data . "\n\n" .
             "--{$mime_boundary}--\n";
}else{
     // Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
             "Content-Type: multipart/mixed;\n" .
             " boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the html message
$message = "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $message . "\n\n";
}                        


//That completes the modifications necessary to accommodate a file attachment. We can now send the message with a quick call to mail:

// Send the message
mail($to, $subject, $message, $headers);

$body = "Dear $name, \n\nThank you for your email. We will contact you as soon as possible regarding the matter. \n \n";

mail ($email, "Re: $subject", $body, 'From:you@your_email.com');

print ('<h1 id="content_h1"><a name="text">            Thank you             </a></h1> <p>We will contact you as soon as possible. You will receive an automatic e-mail immediately confirming the reception of your email.</p>');

}else{
    print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1>  <p>Please fill in all the compulsory fields correctly and then resubmit the form. Thank you.</p>');
    include ("form.php");    
}
}

// This is the end of the insert

print ('

  </div>
</body>
</html>
');

ob_end_flush();

?> 

form.php

<div id="form">
        <form  action="email.php" enctype="multipart/form-data" method="post" onsubmit="
        document.getElementById('container0').style.display='';
        document.getElementById('container2').style.display='none';
        progress();
        return true;">
        <fieldset id="fieldset">
        <label for="name">Contact name<span style="color: red;">*</span>:</label>
        <input <?php if (!empty ($hidden)) { if (empty ($name)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="name" type="text" name="name" value="<?php print "$name"; ?>" tabindex="1" />
        <br />
        <label for="email">E-mail address<span style="color: red;">*</span>:</label>
        <input <?php if (!empty ($hidden)) { if (empty ($email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="email" type="text" name="email" value="<?php print "$email"; ?>" tabindex="1" />
        <br />    
        <label for="confirm_email">Confirm e-mail<span style="color: red;">*</span>:</label>
        <input <?php if (!empty ($hidden)) { if (empty ($confirm_email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="confirm_email" type="text" name="confirm_email" value="<?php print "$confirm_email"; ?>" tabindex="1" />
        <br />    
        <label for="subject">Subject<span style="color: red;">*</span>:</label>
        <input <?php if (!empty ($hidden)) { if (empty ($subject)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="subject" type="text" name="subject" value="<?php print "$subject"; ?>" tabindex="1" />
        <br />    
        <label for="comments">Comments<span style="color: red;">*</span>:</label>
        <textarea <?php if (!empty ($hidden)) { if (empty ($comments)) { print 'style="background: pink;"'; } } ?>class="form_elements" id="comments" name="comments" cols="19" rows="5" tabindex="1"><?php print "$comments"; ?></textarea>
        <br /><br />
        <label for="fileatt">Attach document:</label>
        <input id="fileatt" type="file" name="fileatt"  tabindex="1" />
        <br />
        <input  type="hidden" name="hidden"  value="1" /><br />
        <label for="submit"><span style="color: red;">*</span> Compulsory fields.</label>
        <input id="submit" type="submit" value="Send" tabindex="1" />
        </fieldset>
        </form>
        </div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:52:34+00:00Added an answer on May 24, 2026 at 11:52 am

    For the kajillionth time on this site: Don’t build your own mime messages, it’s unreliable and painful. Use Swiftmailer or PHPMailer instead. PHP’s mail() function is highly unreliable and cannot produce ANY useful diagnostics as to why things are failing. Swift/PHPMailer will both tell you exactly why something blew up.

    beyond that, nowhere do you seem to be doing a move_uploaded_file(), so your uploaded files will be auto-deleted by PHP when the script exits. You MUST deal with the files yourself, which means you have to write code to move the files to whatever place you want them stored in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to query a database from my jsp, but I'd
I am using following code snippet, but its not working :-( //First four characters
I'm using the following code within the JCProperty class to retrieve data from a
I'm using the following code, using the SharpZipLib library, to add files to a
I am using the following code to check if a variable is not nil
The following code sample is an implementation of the Strategy pattern copied from Wikipedia
I using following code and it is successfully uploading files on my local machine.
i am searching file using following code but in output it shows result 5
So I have the following code which I pretty much copied from here .
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.