Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I add file attachment to my form and get the attachment by email?

Community Beginner ,
Aug 29, 2012 Aug 29, 2012

I'm using this code and it works fine, but I don't get the attachment file in the email. How can I add this to my code?

HTML

__________________________________

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>

<script src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js" type="text/javascript"></script>

<script type="text/javascript">// <![CDATA[

              $(document).ready(function() {

                $("#form1").validate({

                  rules: {

                    first: "required",// simple rule, converted to {required:true}

                    email: {// compound rule

                    required: true,

                    email: true

                  },

                  last: {

                    last: true

                  },

                  comment: {

                    required: true

                  }

                  },

                  messages: {

                    comment: "Please enter a comment."

                  }

                });

              });

// ]]></script>

<script type="text/javascript">// <![CDATA[

function validate ()

{

          if (document.form1.first.value == "")

          {

          alert("Please enter your First Name");

          document.form1.first.focus();

          document.form1.first.style.border="1px solid red";

          return false;

          }

          else if (document.form1.last.value == "")

          {

          alert("Please enter your Last Name");

          document.form1.last.focus();

          document.form1.last.style.border="1px solid red";

          return false;

          }

          else if (document.form1.emailaddress.value == "")

          {

          alert("Please enter your Email Address");

          document.form1.emailaddress.focus();

          document.form1.emailaddress.style.border="1px solid red";

          return false;

          }

}

function has_focus() {

    if(document.form1.first.value == "")

                    {

                              document.form1.first.focus();

                              document.form1.style.first.border="1px solid green";

                    }

}

function set_focus(x)

{

          document.getElementById(x).style.border="1px solid #80CA75";

}

function clear_focus(x)

{

          document.getElementById(x).style.border="1px solid #DBDFE6";

}

// ]]></script>

</head>

<body>

<p><span style="color: #666666; text-align: center; font-size: 13px;">Please complete this form if you have any technical issue.</span></p>

<form id="form1" action="http://www.southsun.com/php/tech_issue.php" enctype="multipart/form-data" method="post">

<table style="width: 850px; font-size: 15px; padding-left: 20px; text-align: center;" border="0">

<tbody>

<tr>

<td style="text-align: left; padding-bottom: 20px;" colspan="2">

<h2><span style="color: #666666;">Please complete this form if you have any technical issue.</span></h2>

</td>

</tr>

<tr style="padding-top: 40px;">

<td style="text-align: left;"><span style="color: #abaf6f;"><strong>First Name</strong>:*</span><input id="first1" name="first" type="text" />  <br /><br /> <span style="color: #abaf6f;"><strong>Last Name</strong>:*</span><input id="last1" name="last" type="text" /><br /><br /> <span style="color: #abaf6f; padding-right: 33px;"><strong>Email</strong>:</span><span style="color: #abaf6f;">*</span><input id="email1" name="email" type="text" /><br /><br /> <span style="color: #abaf6f;"><strong>Shipping Method:</strong><br /></span> <input name="shippingmethod" type="radio" value="prioritymail" /> Priority Mail                                                                 <input name="shippingmethod" type="radio" value="store" /> In Store Pick up <br /> <input name="shippingmethod" type="radio" value="ground" />  Ground                                                                       <input name="shippingmethod" type="radio" value="3day" /> 3 Day Select<br /><br /> <span style="color: #abaf6f;"><strong>Payment Method:</strong><br /></span> <input name="paymentmethod" type="radio" value="paypal" /> Paypal Method                                                       <input name="paymentmethod" type="radio" value="creditcard" /> Credit Card<br /> <strong><br /> <span style="color: #abaf6f;">If getting an error message, please explain the error:</span></strong><span style="color: #abaf6f;"> <br /></span> <textarea id="errormessage" cols="20" rows="2" name="errormessage"></textarea><br /><br /></td>

<td style="border-left: 1px solid grey; padding-left: 40px; text-align: left;"><span style="color: #abaf6f;"><strong>If using Paypal, Were you redirected successfully?</strong><br /></span> <input name="paypalredirect" type="radio" value="yes" /> Yes                                                                 <input name="paypalredirect" type="radio" value="no" /> No<br /><br /> <span style="color: #abaf6f;"><strong>If using Credit Card, Did you get an error?</strong><br /></span> <input name="carderror" type="radio" value="yes" /> Yes                                                                  <input name="carderror" type="radio" value="no" /> No<br /><br /> <span style="color: #abaf6f;"><strong>What happened after clicking place order? </strong><br /></span> <textarea id="placeorder1" cols="20" rows="2" name="placeorder"></textarea><br /><br /> <span style="color: #abaf6f;"><strong>Comments</strong>: <br /></span> <textarea id="comments1" cols="20" rows="2" name="strcomments"></textarea><br /><br /> <span style="color: #abaf6f;"><strong>Attach PrintScreen</strong>: <br /></span> <input name="strresume" type="file" />

<div style="height: 50px;"> </div>

</td>

</tr>

<tr>

<td style="padding-top: 20px;" colspan="2">( * ) indicates required fields</td>

</tr>

<tr>

<td style="text-align: center; padding-top: 20px;" colspan="2"><input class="button" name="submit" type="submit" value="Submit" />                        <input class="button" name="reset" type="reset" value="Reset" /></td>

</tr>

</tbody>

</table>

</form>

</body></html>

____________________________________________

PHP

____________________

<?php

echo $savestring;

//--------------------------paramaters--------------------------

// Subject of email sent to you.

$subject = 'prueba con uploads';

// Your email address. This is where the form information will be sent.

$emailadd = 'pchinique@yahoo.com';

// Where to redirect after form is processed.

$url = 'http://www.pch-graphicdesign.com';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.

$req = '0';

$target_path = "http://www.pch-graphicdesign.com/php/uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$text = "Results from form:\n\n";

$space = ' ';

$line = '

';

foreach ($_POST as $key => $value)

{

if ($req == '1')

{

if ($value == '')

{echo "$key is empty";die;}

}

$j = strlen($key);

if ($j >= 20)

{echo "Name of form element $key cannot be longer than 20 characters";die;}

$j = 20 - $j;

for ($i = 1; $i <= $j; $i++)

{$space .= ' ';}

$value = str_replace('\n', "$line", $value);

$conc = "{$key}:$space{$value}$line";

$text .= $conc;

$space = ' ';

}

mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

?>

TOPICS
Server side applications
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 30, 2012 Aug 30, 2012
LATEST

Sending a file as an attachment to an email involves setting the correct MIME type and headers. There's a brief tutorial here: http://webcheatsheet.com/php/send_email_text_html_attachment.php.

Also, you need to use the same name as in your form. In the script you have shown here, the name of the file field is strresume, but your processing script uses this: $_FILES['uploadedfile']['name']. It should be this: $_FILES['strresume']['name']

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines