使用PHPmailer发送附件 - 返回.dat文件

时间:2016-06-23 11:05:04

标签: php phpmailer

希望有人可以指出我错过了什么!

我有一个脚本,我从PHPMailer's Github example here获取了我放在页面中的脚本,并更新了HTML。

虽然图片上传很好并且电子邮件通过,但文件附件是.dat文件,而不是正确的扩展名。

这是我的PHP,以防我改变的东西不应该是:

if (array_key_exists('userfile', $_FILES)) {

$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

    require 'PHPMailerAutoload.php';
    $mail = new PHPMailer;
    $mail->setFrom('my@email.com', 'Competitions');
    $mail->addAddress('jg@email.com', 'JG');
    $mail->Subject = 'Competition Entry';
    $mail->msgHTML("My message body");

    $mail->addAttachment($uploadfile, 'Competiton Entry Attachment');
    if (!$mail->send()) {
        $err[] = "Mailer Error - didn't send email" . $mail->ErrorInfo;
    } else {
        $done[] = "Message sent!";
    }

} else {
    $err[] = 'Failed to move file to ' . $uploadfile;
}

}

非常感谢提前

1 个答案:

答案 0 :(得分:2)

更改

$mail->addAttachment($uploadfile, 'Competiton Entry Attachment');

$name = $_FILES['userfile']['name'];

$ext = end((explode(".", $name)));

$mail->addAttachment($uploadfile, 'Competiton Entry Attachment.'.$ext);