Samson John Souza
Samson John Souza

Reputation: 31

php script to send mail with file attachment

I have an HTML form here in which I have an option to upload a file and then send it to me via email. I already have a php script given below that sends text box values and text area values etc via email.

Now what i want is that i want the file uploaded in the form to recieve it to me via email as an attachment with the email in what i will recieve the rest of the form.

contactform.html

<form id="contact form" name="form1" enctype="multipart/form-data" method="post" action="contactformprocess.php">
  <p>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" />
  </p>
  <p>
    <label for="email">Email:</label>
    <input type="text" name="email" id="email" />
  </p>
  <p>
    <label for="message">Message:</label>
    <textarea name="message" id="message" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="fileupload">Upload your file:</label>
    <input type="file" name="fileupload" id="fileupload" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>

and the php script

contactformprocess.php

<?php

/* Subject and Email Variables */

    $emailSubject = 'Contact Form';
    $webMaster = '[email protected]';

/* Gathering Data Variables */

    $name = $_POST['name'];
    $email = $_POST['email'];
    $question = $_POST['message'];

    $body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Message: $message <br>
EOD;

    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

    $theResults = <<<EOD
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Confirmation Page</title>
<style type="text/css">
.header {
    font-family: Trebuchet MS, Arial, Helvetica, sans-serif;
}
</style>
</head>

<body>
<table width="100%" border="0">
  <tr>
    <td bgcolor="#adbe69" class="header"><strong>Confirmation Page</strong></td>
  </tr>
</table>
<table width="100%" border="0" align="left">
  <tr>
    <td><p align="center">Thank You!</p>
    <p align="center">Your Form is Submitted Succesfully,</p>
    <p align="center">You will recieve an email within 24 to 48 hours regarding your message,</p>
    </td>
  </tr>
</table>
</body>
</html>
EOD;
echo "$theResults";
?>

Upvotes: 1

Views: 8835

Answers (1)

Najzero
Najzero

Reputation: 3202

Not that I would reccomend using a php script to send mail with attachments (rather supply a downloadlink and store the document on the server for retrieval for the mail target)

have a look here: http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment

the part where the attachment is retrieved goes like this:

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 

you will have to use your temporary file from the servers (php-)upload directory instead of the attachment.zip

Upvotes: 2

Related Questions