如何将上传的文件作为附件发送到邮件中

时间:2013-12-19 05:41:12

标签: php file-upload email-attachments

我使用jquery上传了一个文件,现在我需要将该上传的文件作为附件发送到邮件中。请参阅file upload。如何将此上传作为附件发送到邮件中。

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();
?>

1 个答案:

答案 0 :(得分:2)

您可以使用PHP Mailer类。检查下面的代码。

$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();

您可以从以下链接下载php邮件程序:PHPMailer

如果您有任何疑问,请与我们联系!

由于