1

I have uploaded a file using jquery and now i need to send that uploaded file in mail as an attachment.Refer fiddle file upload.How to send this upload in mail as an attachment.

HTML

  <form id="myform" action="process-upload.php" method="post" enctype="multipart/form-data">

<input id="tele" type="file" name="filename"/>
<br/>
<input  class="formbtn" type="submit" value="Upload" />

jquery

$(document).ready(function () {

$('#myform').validate({
    rules: {
        filename: {
            required: true,
            extension: "docx|rtf|doc|pdf"
        }
    },
    messages: {
        filename: {
            required: "Please upload resume",
            extension: "Please upload valid file formats"
        }
    }
});

});

php

 <?php
require("class.phpmailer.php");
$email = new PHPMailer();
$email->From      = 'from@mail.com';
$email->FromName  = 'Name';
$email->Subject   = 'Subject';
$email->Body      = 'Message Body';
$email->AddAddress( 'abc@gmail.com' );
$target_path = "uploads/";

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

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
$email->AddAttachment(basename($target_path . $_FILES['uploadedfile']['name']));


return $email->Send();
?>
Rama Priya
  • 2,387
  • 7
  • 28
  • 39
  • I have not written it yet.My question itself in that only how to send this uploaded file as attachment – Rama Priya Dec 19 '13 at 05:42
  • it is not very difficult task.try to google it.visit some links for emails [http://webcheatsheet.com/php/send_email_text_html_attachment.php],[http://php.net/manual/en/function.mail.php] – Mahmood Rehman Dec 19 '13 at 05:46
  • @RamaPriya : Read this link http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – Nanhe Kumar Dec 19 '13 at 05:47
  • refer to this link: http://netsperience.org/content/blog/attach-a-file-email-with-php-code-actually-works – user2936213 Dec 19 '13 at 05:47

1 Answers1

2

You can use PHP Mailer class for that. Check below code for that.

$email = new PHPMailer();
$email->From      = 'from@mail.com';
$email->FromName  = 'Name';
$email->Subject   = 'Subject';
$email->Body      = 'Message Body';
$email->AddAddress( 'to@mail.com' );

$filetoattach = 'File path';

$email->AddAttachment( $filetoattach , 'filename.doc' );

return $email->Send();

You can download php mailer class from this link : PHPMailer

Let me know if you have any query!

Thanks

Chandresh M
  • 3,808
  • 1
  • 24
  • 48
  • i have no idea on how to include php mailer.can you please explain it in a fiddle – Rama Priya Dec 19 '13 at 05:55
  • There is nothing like any hard or smart work at all, just download PHPMailer and include class file like this : **require_once('folderpath/class.phpmailer.php');** – Chandresh M Dec 19 '13 at 06:03
  • Hi kindly check the updated code.mail function is not working for me – Rama Priya Dec 19 '13 at 07:31
  • Why using PHP mail() function? Try with PHPMailer. – Chandresh M Dec 19 '13 at 07:37
  • As it is a dynamic function how i need to mention the path for temporary storage and then filename in your code $filetoattach = 'File path'; $email->AddAttachment( $filetoattach , 'filename.doc' ); – Rama Priya Dec 19 '13 at 07:42
  • Hi now i have updated the code with php mailer function.still its not working.kindly suggest some solution to send attachment in mail.@Chandresh – Rama Priya Dec 19 '13 at 08:45
  • are mails going without attachment? – urfusion Dec 19 '13 at 09:12
  • I think this line: $email->AddAttachment(basename($target_path . $_FILES['uploadedfile']['name'])); should be **$email->AddAttachment($_FILES['uploadedfile']['name'],basename($target_path . $_FILES['uploadedfile']['name']));** – Chandresh M Dec 19 '13 at 09:15
  • No entire mail functionality is not working simply it remains in same page after chosen the file and click on upload button @urfusion – Rama Priya Dec 19 '13 at 10:04
  • $email->AddAttachment($_FILES['uploadedfile']['name'],basename($target_path . $_FILES['uploadedfile']['name'])); even after changed this line mail functionality is not working @Chandresh – Rama Priya Dec 19 '13 at 10:04
  • please check that the object is creating or not try var_dump($email). if it is working then upload a file on root with a normal mail function $to = 'example@gmail.com'; $subject = 'the subject'; $message = 'hello'; $headers = 'From:example@example.com' . "\r\n" . 'Reply-Path:noreply@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); //mail($to, $subject, $message, $headers, '-freturn@yourdomain.com') by this you can check that is your server is able to send mail or not – urfusion Dec 19 '13 at 10:20