从表单发送带有附件的电子邮件不起作用

时间:2019-01-13 10:01:58

标签: php html forms file

我一直在一个网站上工作,我想从用户获取输入类型“文件”,并使用php 通过电子邮件将其发送给管理员。 / p>

我的代码如下:

html:

<form class="pure-form"  enctype="multipart/form-data" action="resumeUpload.php" method="POST">
                                            <label for="resume_attachment">Upload your resume</label>
                                            <input type="file" class="pure-input-rounded" name="resume_attachment">
                                            <button type="submit" name="resume_send" class="mt-20 mb-20" value="1">Submit</button>
                                        </form>

php:         

if($_POST['resume_send'] && isset($_FILES['resume_attachment'])) 
{ 

    //$from_email      = 'sender@abc.com'; //from mail, sender email addrress 
    $recipient_email = 'xyz@gmail.com'; //recipient email addrress 

    //Load POST data from HTML form 
    //$sender_name = $_POST["sender_name"] //sender name 
    //$reply_to_email = $_POST["sender_email"] //sender email, it will be used in "reply-to" header 
    //$subject     = 'New Resume' //subject for the email 
    //$message     = $_POST["message"] //body of the email 


    /*Always remember to validate the form fields like this 
    if(strlen($sender_name)<1) 
    { 
        die('Name is too short or empty!'); 
    } 
    */

    //Get uploaded file data using $_FILES array 
    $tmp_name = $_FILES['resume_file']['tmp_name']; // get the temporary file name of the file on the server 
    $name    = $_FILES['resume_file']['name']; // get the name of the file 
    $size    = $_FILES['resume_file']['size']; // get size of the file for size validation 
    $type    = $_FILES['resume_file']['type']; // get type of the file 
    $error   = $_FILES['resume_file']['error']; // get the error (if any) 

    //validate form field for attaching the file 
    if($file_error > 0) 
    { 
        die('Upload error or No files uploaded'); 
    } 

    //read from the uploaded file & base64_encode content 
    $handle = fopen($tmp_name, "r"); // set the file handle only for reading the file 
    $content = fread($handle, $size); // reading the file 
    fclose($handle);                 // close upon completion 

    $encoded_content = chunk_split(base64_encode($content)); 

    $boundary = md5("random"); // define boundary with a md5 hashed value 

    //header 
    $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
    //$headers .= "From:".$from_email."\r\n"; // Sender Email 
    //$headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email addrress to reach back 
    $headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 
    $headers .= "boundary = $boundary\r\n"; //Defining the Boundary 

    //plain text 
    $body = "--$boundary\r\n"; 
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $body .= chunk_split(base64_encode($message)); 

    //attachment 
    $body .= "--$boundary\r\n"; 
    $body .="Content-Type: $file_type; name=".$file_name."\r\n"; 
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
    $body .="Content-Transfer-Encoding: base64\r\n"; 
    $body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n"; 
    $body .= $encoded_content; // Attaching the encoded file with email 

    $sentMailResult = mail($recipient_email, $subject, $body, $headers); 

    if($sentMailResult ) 
    { 
    echo "File Sent Successfully."; 
    unlink($name); // delete the file after attachment sent. 
    } 
    else
    { 
    die("Sorry but the email could not be sent. 
                    Please go back and try again!"); 
    } 
} 
?>

我已使用此代码,但无法完成任务。

任何人都可以在我的代码中找到错误,并提供正确的解决方法。

谢谢。

2 个答案:

答案 0 :(得分:2)

您的问题似乎在这一行:

if($_POST['button'] && isset($_FILES['attachment']))

它有两个问题:

首先,$_POST['button']将始终产生错误的值,因为未设置该值。为此,您需要更改html:<button type="submit" class="mt-20 mb-20" name="button" value="1">Submit</button>。也就是说:添加一个namevalue属性,该值应为not be something that results in falsey

您打算进行这项检查的目的是什么?如果您只想检查请求是否以POST的身份进入,请跳过此步骤,因为$_FILES的存在就足够了。没有$_GET文件上传。

第二,isset($_FILES['attachment'])始终为假,因为在HTML中,此文件上载名称为resume_file,而不是attachment

只有解决了这些问题,您才能继续解决邮件问题(如果存在)

要清楚:您的电子邮件代码从未到达。

答案 1 :(得分:-1)

我认为您应该使用PHPMailer API发送带有附件的邮件。

这里有个提示:

  • 要求用户上传具有输入类型文件的文件

  • 将此文件上传到您的服务器

  • 使用PHPMailer发送附件

    $mail->addAttachment('/var/tmp/file.tar.gz');

https://github.com/PHPMailer/PHPMailer