PHP-PHPMailer-如何将文件添加为具有正确扩展名的附件

PHP-PHPMailer-如何将文件添加为具有正确扩展名的附件,php,email,upload,phpmailer,Php,Email,Upload,Phpmailer,我正在建立一个网站,有一个表单,用户可以在其中输入一些字段,即姓名、电子邮件等,但也可以选择上载文件。我正在使用PHPMailer构建一封电子邮件,其中包含用户的数据作为电子邮件字段,然后将其发送给我,以便我可以查看它。在上传文件之前,一切正常。我对PHP比较陌生,所以它可能是一些小东西,但它让我发疯。在下面的代码中,我为上传的文件生成一个新名称,然后附加文件扩展名。然后我将其移动到临时文件夹,并尝试通过$mail->addAttachment将其附加到电子邮件。电子邮件会发送所有内容,但附加的

我正在建立一个网站,有一个表单,用户可以在其中输入一些字段,即姓名、电子邮件等,但也可以选择上载文件。我正在使用PHPMailer构建一封电子邮件,其中包含用户的数据作为电子邮件字段,然后将其发送给我,以便我可以查看它。在上传文件之前,一切正常。我对PHP比较陌生,所以它可能是一些小东西,但它让我发疯。在下面的代码中,我为上传的文件生成一个新名称,然后附加文件扩展名。然后我将其移动到临时文件夹,并尝试通过
$mail->addAttachment
将其附加到电子邮件。电子邮件会发送所有内容,但附加的文件没有扩展名——当我手动下载并添加适当的扩展名时,它可以工作,但由于某些原因,文件不会作为正确的类型附加

    // if one of the fields is set they all will be, i.e. the form has been submitted
    if (isset($_POST['firstName']))
    {
    // array to hold possible runtime errors
    $errors = array();

    // if statement to see if user failed to enter one of the required fields
    if (empty($_POST['firstName']) || empty($_POST['lastName']) || empty($_POST['email']) || empty($_POST['subject'])
    || empty($_POST['message']) || empty($_POST['phone']))
    {
    array_push($errors, 'Please enter all required fields');
    }


    // var to keep track of whether or not we have a file
    $have_file = ($_FILES['inputFile']['error'] != UPLOAD_ERR_NO_FILE);

    //checks for file upload as well
    if ($have_file)
    {
    // here a file has been uploaded, so we make sure its an acceptable type
    $ext_whitelist = array('dwg', 'asm', 'acp', '3dxml', 'cgr', 'dft', 'dxf', 'iam', 'idw', 'ipt', 'ipn', 'par', 'prt',
    'skp', 'rvt', 'rfa',' sldasm', 'slddrw', 'sldprt', 'step', 'stl');

    // var to store file array
    $file = $_FILES['inputFile'];

    // file properties stored for easier readability
    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    // get file extension
    $file_ext = explode('.', $file_name);
    $file_ext = strtolower(end($file_ext));


    if (!in_array($file_ext, $ext_whitelist))
    {
        if ($ext == 'php')
        {
            array_push($errors, 'Nice try');;
        }
        else
        {
            array_push($errors, 'Please enter a valid file type');
        }
    }


    // checks file size
    if ($file_size > 64000000)
    {
        array_push($errors, 'File too large, please call for further information');
    }
    }


    // if we have an error, we just output that, or those, otherwise, we proceed
    // with mailer
    if (!empty($errors))
    {
    foreach ($errors as $err) { ?>

        <p class="text-center" style="margin-top:20px;font-size:16px;">

            <?php echo $err; ?>

        </p>

    <?php
    }

    }
    // if here, there have been no errors
    else
    {
    require 'PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    //mail server setup
    $mail->isSMTP();                                    // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';                     // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                             // Enable SMTP authentication
    $mail->Username = '';         // SMTP username
    $mail->Password = '';                     // SMTP password
    $mail->SMTPSecure = 'tls';                          // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                  // TCP port to connect to

    //add To and From fields
    $mail->From = '';
    $mail->FromName = 'Mailer';
    $mail->addAddress('');

    $mail->isHTML(true);                                // Set email format to HTML


    //add message contents
    $mail->Subject = $_POST['subject'];

    $mail->Body = $_POST['message'] . '<br><br>' . $_POST['firstName'] . ' ' . $_POST['lastName'] . '<br>' . $_POST['phone'];

    // adds organization if its there
    if (!empty($_POST['organization']))
    {
        $mail->Body .= '<br>' . $_POST['organization'];
    }

    // uploads/attaches file if there was one
    if($have_file)
    {
        // give file unique name and set its destination as a temporary folder
        $file_name_new = uniqid('', true) . '.' . $file_ext;
        $file_destination = sys_get_temp_dir() . '\\' . $file_name_new;

        if (move_uploaded_file($file_tmp, $file_destination))
        {
            echo $file_destination;
            $mail->addAttachment($file_destination, 'Uploaded file');
        }
        else
        {
            ?>
            <p class="text-center" style="margin-top:20px;font-size:16px;">Error sending message, please call and let us know</p>
            <?php
        }
    }

    //send the message
    if (!$mail->send())
    {
        ?>
        <p class="text-center" style="margin-top:20px;font-size:16px;">Error sending message, please call and let us know</p>
        <?php
    }
    else
    {
        ?>
        <p class="text-center" style="margin-top:20px;font-size:16px;">Message sent! Thank you for visiting us today</p>
        <?php
    }
//如果设置了其中一个字段,则所有字段都将被设置,即表单已提交
如果(isset($_POST['firstName']))
{
//数组以保存可能的运行时错误
$errors=array();
//if语句,以查看用户是否未能输入所需字段之一
如果(空($_POST['firstName'])空($_POST['lastName']))|空($_POST['email']))|空($_POST['subject']))
||空($_POST['message'])| |空($_POST['phone']))
{
数组_push($errors,'请输入所有必填字段');
}
//var来跟踪我们是否有一个文件
$have_file=($_FILES['inputFile']['error']!=上传错误文件);
//同时检查文件上传
如果($have_文件)
{
//这里有一个文件已经上传,所以我们确保它是一个可接受的类型
$ext_白名单=数组('dwg','asm','acp','3dxml','cgr','dft','dxf','iam','idw','ipt','ipn','par','prt',
‘skp’、‘rvt’、‘rfa’、‘sldasm’、‘slddrw’、‘sldprt’、‘step’、‘stl’;
//var来存储文件数组
$file=$\u文件['inputFile'];
//存储文件属性以便于阅读
$file_name=$file['name'];
$file_tmp=$file['tmp_name'];
$file_size=$file['size'];
$file_error=$file['error'];
//获取文件扩展名
$file\u ext=explode('.',$file\u name);
$file_ext=strtolower(end($file_ext));
if(!in_数组($file_ext,$ext_whitelist))
{
如果($ext=='php')
{
数组_push($errors,'Nice try');;
}
其他的
{
数组_push($errors,'请输入有效的文件类型');
}
}
//检查文件大小
如果($file_size>64000000)
{
array_push($errors,'文件太大,请致电了解更多信息');
}
}
//如果我们有一个错误,我们只输出那个或那个,否则,我们继续
//和梅勒
如果(!empty($errors))
{
foreach($errors作为$err){?>


首先,我要说的是,您需要在追加文件时调试和检查文件_ext,并确保它不是空的

要获取文件扩展名,请使用:

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);

请告诉我您的结果,我可以提供进一步帮助。

首先,我要说的是,您需要在附加文件时调试和检查文件,并确保文件不是空的

要获取文件扩展名,请使用:

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);

请告诉我您的结果,我可以提供进一步帮助。

首先,我要说的是,您需要在附加文件时调试和检查文件,并确保文件不是空的

要获取文件扩展名,请使用:

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);

请告诉我您的结果,我可以提供进一步帮助。

首先,我要说的是,您需要在附加文件时调试和检查文件,并确保文件不是空的

要获取文件扩展名,请使用:

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);

让我知道你的结果,我可以进一步帮助你。

@Hanoncs我想我已经设法得到了。电话:

$mail->addAttachment($file_destination, 'Uploaded file');
将附件中的文件重命名为“uploadedfile”。因此,很明显,在电子邮件中,由于文件名的原因,文件上没有文件扩展名。因此,我所要做的就是将“uploadedfile”更改为具有扩展名的文件,例如“uploadedfile.ext”,它似乎可以工作。这是一个愚蠢但非常令人沮丧的错误,因为文件上载处理似乎工作得相当好。因此,块现在看起来如下所示:

    // uploads/attaches file if there was one
    if($have_file)
    {
    // give file unique name and set its destination as a temporary folder
    $file_name_new = uniqid('', true) . '.' . $file_ext;
    $file_destination = sys_get_temp_dir() . '\\' . $file_name_new;

    if (move_uploaded_file($file_tmp, $file_destination))
    {
        echo $file_destination;
        $mail->addAttachment($file_destination, 'uploadedFile.dwg');
    }
    else
    {
        ?>
        <p class="text-center" style="margin-top:20px;font-size:16px;">Error sending message, please call and let us know</p>
        <?php
    }
}
//上载/附加文件(如果有)
如果($have_文件)
{
//为文件指定唯一名称,并将其目标设置为临时文件夹
$file\u name\u new=uniqid(“”,true)。。$file\u ext;
$file\u destination=sys\u get\u temp\u dir()。$file\u name\u new;
如果(移动上传的文件($file\u tmp,$file\u destination))
{
echo$file\u目的地;
$mail->addAttachment($file_destination,'uploadedFile.dwg');
}
其他的
{
?>
发送消息时出错,请致电告知


@汉诺克斯:我想我是设法弄到的

$mail->addAttachment($file_destination, 'Uploaded file');
将附件中的文件重命名为“uploadedfile”。因此,很明显,在电子邮件中,由于文件名的原因,文件上没有文件扩展名。因此,我所要做的就是将“uploadedfile”更改为具有扩展名的文件,例如“uploadedfile.ext”,它似乎可以工作。这是一个愚蠢但非常令人沮丧的错误,因为文件上载处理似乎工作得相当好。因此,块现在看起来如下所示:

    // uploads/attaches file if there was one
    if($have_file)
    {
    // give file unique name and set its destination as a temporary folder
    $file_name_new = uniqid('', true) . '.' . $file_ext;
    $file_destination = sys_get_temp_dir() . '\\' . $file_name_new;

    if (move_uploaded_file($file_tmp, $file_destination))
    {
        echo $file_destination;
        $mail->addAttachment($file_destination, 'uploadedFile.dwg');
    }
    else
    {
        ?>
        <p class="text-center" style="margin-top:20px;font-size:16px;">Error sending message, please call and let us know</p>
        <?php
    }
}
//上载/附加文件(如果有)
如果($have_文件)
{
//为文件指定唯一名称,并将其目标设置为临时文件夹
$file\u name\u new=uniqid(“”,true)。。$file\u ext;
$file\u destination=sys\u get\u temp\u dir()。$file\u name\u new;
如果(移动上传的文件($file\u tmp,$file\u destination))
{
echo$file\u目的地;
$mail->addAttachment($file_destination,'uploadedFile.dwg');
}
其他的
{
?>
发送消息时出错,请致电告知

@河内