Jump to content

[jQuery]Pleas help sending email attachments


lingo5

Recommended Posts

I am using jquery to attach multiple files.

I have put together this script that sends an email with attachments, but it doesn't send the attachments....I can't see what I have done wrong.

Please help. Thanks

 

<!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> 
<title>E-mail with Attachment</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../css/demos.css"/>
<script src="../js/jquery-1.7.js" type="text/javascript" 
<script src="../js/jquery.MultiFile.pack.js" type="text/javascript" language="javascript"> 
</script> 
</head>

<body> 
<?php 
if ($_SERVER['REQUEST_METHOD']=="POST"){ 

$to="info@mycompany.com"; 
$subject="E-mail with attachment"; 

$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; 

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; 

$headers = "From: $from\r\n" . 
"MIME-Version: 1.0\r\n" . 
"Content-Type: multipart/mixed;\r\n" . 
" boundary=\"{$mime_boundary}\""; 

$message="This is an example"; 

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


foreach($_FILES as $userfile){ 

$tmp_name = $userfile['tmp_name']; 
$type = $userfile['type']; 
$name = $userfile['name']; 
$size = $userfile['size']; 


if (file_exists($tmp_name)){ 

if(is_uploaded_file($tmp_name)){ 

$file = fopen($tmp_name,'rb'); 

// read the file content into a variable 
$data = fread($file,filesize($tmp_name)); 

fclose($file); 

$data = chunk_split(base64_encode($data)); 
} 

$message .= "--{$mime_boundary}\n" . 
"Content-Type: {$type};\n" . 
" name=\"{$name}\"\n" . 
"Content-Disposition: attachment;\n" . 
" filename=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n"; 
} 
} 


// here's our closing mime boundary that indicates the last of the message 
$message.="--{$mime_boundary}--\n"; 
// now we just send the message 
if (@mail($to, $subject, $message, $headers)) 
print_r($_FILES); 
else 
echo "Failed to send"; 
} else { 
?> 
<p>Send an e-mail with an attachment:</p> 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
enctype="multipart/form-data" name="form1"> 
<p>Your name: <input type="text" name="fromname"></p> 
<p>Your e-mail: <input type="text" name="fromemail"></p> 
<p>Mod List: <textarea name="question" maxlength="1000" cols="25" rows="6" name="modlist"></textarea> 
<p><input type="file" name="file[]" class="multi" /></p> 
<p><input type="submit" name="Submit" value="Submit"></p> 
</form> 
<?php } ?> 
</body> 

</html>

Link to comment
Share on other sites

I have no idea how to do this with jQuery. However, I have just been learning how to do it with PHP Mailer and can tell you it is as simple as anything with this script (takes about half an hour to get used to everything about it). You might want to consider looking into it :)

 

$mail->AddAttachment("Document to attach.extension", "[optional]Alternative name for attachment"); // attachment

Link to comment
Share on other sites

Yeah sure, when you download PHP Mailer it comes with quite a few example scripts. It uses OOP. Here's a sample script I edited to use only the options I need:

 

<?php
$HTML_message = '<div style="color:#FF000;margin-left:5em;">HTML Testing message with red text and a margin</div>';

require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

  $mail->AddReplyTo('name@yourdomain.com', 'First Last');                              //who to reply too
  $mail->SetFrom('name@yourdomain.com', 'First Last');                                 // who sent it
  $mail->Subject = 'PHPMailer Test Email';                                             // title of email
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional non html message- MsgHTML will create an alternate automatically
  $mail->MsgHTML($HTML_message);                                                       // your html message
  $mail->AddAttachment('images/phpmailer.gif');                                        // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif');                                   // another attachment
  $mail->Send();                                                                       // send the email!
?>

 

 

 

Have you made sure the file is definitely being uploaded?

Link to comment
Share on other sites

Thanks Joe92,

I have actually made a lot of progress wih the Jquery option.

I am now able to attach mutiple files and email them correctly using the Multiple File Upload JQuery plugin...which is verrry cool.

This i how I do it:

 

1 - download the Multiple File Upload JQuery plugin here

http://www.fyneworks.com/jquery/multiple-file-upload/#

2 - create your html form

<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/demos.css"/>
<script src="../js/jquery-1.7.js" type="text/javascript"> </script>          // link to jquery-1.7.js and jquery.MultiFile.pack.js
<script src="../js/jquery.MultiFile.pack.js" type="text/javascript" language="javascript">    
</script> 
</head>
<body>
<form action="mail2.php" method="post" name="form1" enctype="multipart/form-data">
<table width="343" border="1">
<tr>
<td>To</td>
<td><input name="txtTo" type="text" id="txtTo"></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="txtSubject" type="text" id="txtSubject"></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
</tr>
<tr>
<td>Form Name</td>
<td><input name="txtFormName" type="text"></td>
</tr>
<tr>
<tr>
<td>Form Email</td>
<td><input name="txtFormEmail" type="text"></td>
</tr>
<tr>
<td>Attachment</td>
<td>
<input name="fileAttach[]" type="file" class="multi"><br><br> //don't forget to add class="multi" for the juery multiple selection to work
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
</body>
</html>

3 - Create your sendmail script:

<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$strTo = $_POST["txtTo"];
$strSubject = $_POST["txtSubject"];
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniq id Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Attachment ***//
for($i=0;$i<count($_FILES["fileAttach"]["name"]);$i++)
{
if($_FILES["fileAttach"]["name"][$i] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"][$i];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"][$i])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //

if($flgSend)
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
?>
</body>
</html>

..and that's it...try it because it's quite nice...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.