Sending attachments with PHPmailer - returns .dat file

老子叫甜甜 提交于 2019-12-10 23:56:04

问题


Hopefully someone out there can point out what I've missed!

I have a script which I took from PHPMailer's Github example here which I placed in my page, and updated the HTML.

Although the image uploads fine and the email comes through, the file attachment is a .dat file instead of the correct extension.

Here's my PHP in case something I've changed isn't as it should be:

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;
}

}

Many thanks in advance


回答1:


Change

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

To

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

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

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


来源:https://stackoverflow.com/questions/37989808/sending-attachments-with-phpmailer-returns-dat-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!