Monday, May 24, 2010

FORM: Upload File and Send to Email c/o PHPMailer?

I need help for my web form. I want to create a form on my website that can upload any file and send it to my email.





The script should use PHPmailer cos it's the only one accepted on my hosting. I've tried checking the PHPMailer tutorials I could find but I couldn't get it to work. And I was not always sure if it's the right script I need.





Is there a sample out there that you can direct me to?





Note that what I need is something with PHPmailer, and it should be able to upload different files and multiple files as well.





Thank you for the help, I'd really appreciate it.

FORM: Upload File and Send to Email c/o PHPMailer?
Start small. You're asking for a complete application, whereas if you really want to learn this stuff you should really just be asking for one piece at a time so that you can get your head around it. Otherwise, it may be too daunting down the line for you to easily customize it to do what you need.





1) you need a %26lt;FORM%26gt; that accepts multi-part data (files), with multiple inputs for these files.


2) you need the %26lt;FORM%26gt; to submit to a .PHP file that receives all multi-part data and saves them as temp files on the server


3) the .PHP file needs to send e-mail and attach the temp files to the e-mail





First thing you should do is make the FORM page, and the PHP page, and just try to get the PHP file to receive the files and output to you that it succeeded in doing so.





---- form page ----


%26lt;form name="upload" action="send.php" enctype="multipart/form-data" method="post"%26gt;


%26lt;strong%26gt;Upload a file:%26lt;/strong%26gt;






%26lt;input type="file" name="filedata" size="20"%26gt;


%26lt;input type="button" value=" Upload " class="but" onClick="this.disabled=true;document.for... wait ...';"%26gt;


%26lt;/form%26gt;





---- PHP file ----


$target_path = '/myserver/myfilepath/';


$original_filename = $_FILES['filedata']['name'];





if(move_uploaded_file($_FILES['filedat... $target_path))


{


echo "%26lt;li%26gt;$original_filename successfully uploaded.%26lt;/li%26gt;"


}








---------





If you can get this part to work, you can see how to add more and more files to both sides, and then all you need is the PHP mail command:





$to = 'target_address@myserver.com';


$headers = "From: %26lt;no-reply@myserver.com%26gt;" . "\r\n";


$headers .= 'MIME-Version: 1.0' . "\r\n";


$subject = 'Sent files';


$body = 'Please see attached files.';





if (mail($to, $subject, $body, $headers)) {


echo "%26lt;li%26gt;E-mail sent.%26lt;/li%26gt;";


}


No comments:

Post a Comment