PHP Mailer发送空附件

时间:2016-04-08 04:09:15

标签: php ajax phpmailer

我正在尝试从联系表单发送附件。我使用phpMailer示例基于我的代码,但这样做没有附件发送。不确定我做错了什么。附加输入的名称是clientResume。

<div id='contactForm' class='animated'>
    <div id='formClose'>X</div>
    <form id="form" action="employerForm.php" method="post" enctype="multipart/form-data">
       <h1>Get Started For Free!</h1>
       <label>Name</label>
       <input class="formField" placeholder="Name" type="text" name="clientName" required>
       <label>Email</label>
       <input class="formField" placeholder="Email" type="email" name="clientEmail" required>
       <label>Resume</label>
       <input class="formField" placeholder="Resume" type="file" name="clientResume" required>
       <input type="hidden" name="MAX_FILE_SIZE" value="30000">
       <input id='formButton' type='button' value='Submit'>
    </form>
</div>

var name = $('input[name="clientName"]').val(),
        email = $('input[name="clientEmail"]').val(),
        company = $('input[name="clientCompany"]').val(),
        comment = $('input[name="clientComment"]').val(),
        resume = $('input[name="clientResume"]').val(),
        form = $(document).find("form"),
        formData = form.serialize();

$.ajax({
    type: 'POST',
    url: form.attr('action'),
    data: formData,
    AccessControlAllowOrigin: '*';
});

<?php

        require_once('phpmailer/PHPMailerAutoload.php');
        $mail = new PHPMailer();

        $name = $_POST['clientName'];
        $email = $_POST['clientEmail'];
        $attachment = tempnam(sys_get_temp_dir(), sha1($_FILES['clientResume']['tmp_name']));

        $subject = "Resume Submission [$name]";
        $message = "Name: $name " . "<br>Email: $email";

        $mail->isSMTP();
        $mail->Debugoutput = 'html';
        $mail->Host = 'correlation-host.correlation-one.com';    
        $mail->Port = 25;
        $mail->SMTPSecure = 'tls';
        $mail->SMTPAuth = false;
        $mail->setFrom($email, $name);
        $mail->addAddress('mpaccione1991@gmail.com', 'Michael Paccione');
        $mail->Subject = 'PHPMailer SMTP test';
        $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['clientResume']['name']));
        if (move_uploaded_file($_FILES['clientResume']['tmp_name'], $uploadfile)) {
            $mail->addAttachment($uploadfile, 'resume');
        }
        $mail->msgHTML($message);
        $mail->Send();
        $mail->SmtpClose();


    ?>

4 个答案:

答案 0 :(得分:1)

没有必要再次重新发明轮子;将您的代码基于the file upload example provided with PHPMailer

您没有move_uploaded_file,因此您的上传不安全。您复制了关于创建安全文件名的部分,但没有复制其后的移动,因此您的addAttachment呼叫点什么都没有。这样做:

$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['clientResume']['name']));
if (move_uploaded_file($_FILES['clientResume']['tmp_name'], $uploadfile)) {
    $mail->addAttachment($uploadfile, 'My uploaded file');
    ...

您的表单在文件输入元素之前缺少隐藏的MAX_FILE_SIZE元素:

<input type="hidden" name="MAX_FILE_SIZE" value="30000">

所有这些也包含在the PHP docs on handling file uploads中。在发布SO之前搜索和阅读源文档总是一个好主意。

答案 1 :(得分:0)

试试这个,

$ MAIL-&GT; addAttachment($附着);

其中$附件类似于$ attachment = move_uploaded_file($ _FILES ['clientResume'] ['tmp_name'],$ target);

答案 2 :(得分:0)

需要访问FormData js API才能从前端正确发送附件。

            var fd = new FormData();
            fd.append("clientName", name);
            fd.append("clientEmail", email);
            fd.append("clientResume", $(document).find('input[name="clientResume"]')[0].files[0]);

            $.ajax({
                type: 'POST',
                url: form.attr('action'),
                processData: false,
                contentType: false,
                data: fd,
                datatype: 'json'
            })

答案 3 :(得分:-1)

只需使用此类型附加文件

即可
$mail->AddAttachment($_FILES['clientResume']['tmp_name'], $_FILES['clientResume']['name']);