PHP - PHPMailer - 如何添加具有正确扩展名的文件作为附件
PHP - PHPMailer - how to add file as attachment with correct extension
我正在建立一个网站并有一个表单,用户可以在其中输入一些字段,即姓名、电子邮件等,但也可以选择上传文件。我正在使用 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
}
如有任何帮助,我们将不胜感激。我将某些字段留空以防止显示我的信息。即 $mail->From
首先我要说的是,您需要在 file_ext 附加时调试并检查它,并确保它不为空。
要获取文件扩展名,请使用:
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
让我知道你的结果,我可以提供进一步的帮助。
@Hanoncs 我想我设法得到了它。该行:
$mail->addAttachment($file_destination, 'Uploaded file');
正在重命名附加到 'Uploaded file' 的文件,字面意思。所以,显然,在电子邮件中,文件没有文件扩展名,因为名称。所以我所要做的就是将 'Uploaded file' 更改为带有扩展名的内容,例如'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
}
}
无论如何,感谢您的所有帮助,尤其是关于 IDE 的帮助,现在开始进行设置。
我正在建立一个网站并有一个表单,用户可以在其中输入一些字段,即姓名、电子邮件等,但也可以选择上传文件。我正在使用 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
}
如有任何帮助,我们将不胜感激。我将某些字段留空以防止显示我的信息。即 $mail->From
首先我要说的是,您需要在 file_ext 附加时调试并检查它,并确保它不为空。
要获取文件扩展名,请使用:
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
让我知道你的结果,我可以提供进一步的帮助。
@Hanoncs 我想我设法得到了它。该行:
$mail->addAttachment($file_destination, 'Uploaded file');
正在重命名附加到 'Uploaded file' 的文件,字面意思。所以,显然,在电子邮件中,文件没有文件扩展名,因为名称。所以我所要做的就是将 'Uploaded file' 更改为带有扩展名的内容,例如'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
}
}
无论如何,感谢您的所有帮助,尤其是关于 IDE 的帮助,现在开始进行设置。