2019 如何使用 PHPMailer V6.0.7 和 PHP 从 Web 表单发送多个附件
2019 How to use PHPMailer V6.0.7 and PHP to send multiple Attachements from a Web Form
2019 年使用 PHP Mailer v6.0.7 和 PHP 发送多个附件的最佳方式是什么?我读过不同的方法,有些人说这是错误的,因为安全问题和时代总是在变化。我也在使用实时托管服务器。我在代码中添加了 Focus Point ----------------。
我还没有真正做任何事情,因为我不确定 2019 年的最佳实践是什么。我已经阅读了很多重复的内容,这个也可能成为旗帜,但它们有很多 PHP 代码好与坏,谁知道哪一个仍然是 2019 年最好的。我将使用我的另一个 post 中的最后一个工作示例,但我想添加附件。
我的重点是正确 PHP 以正确的方式上传一个文件和多个附件,$Mail->Attachement with example 以及将文件加载到临时目录中的实时服务器是否真的安全HTML 代码适用于我认为它们已经存在的附件。
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP; <- This fixed most of my issues but not always required per Sychro
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer(true);
if(isset($_POST[‘submit’]))
{ <--- Added Opening
$first_name = ((isset($_POST['FirstName']))&&(!is_null($_POST['FirstName'])))? $_POST['FirstName']:'';
$last_name = ((isset($_POST['LastName']))&&(!is_null($_POST['LastName'])))? $_POST['LastName']:'';
$email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
$age = ((isset($_POST['Age']))&&(!is_null($_POST['Age'])))? $_POST['Age']:'';
$student = ((isset($_POST['Student']))&&(!is_null($_POST['Student'])))? $_POST['Student']:'';
$agree18 = ((isset($_POST['Agree18']))&&(!is_null($_POST['Agree18'])))? $_POST['Agree18']:'';
Focus Point ----------------
Any Security PHP Code for Uploaded Files Updated to 2019 Standards
----------------------------
Note: Hosting Service should provide this infor to you.
----------
$mail->isSMTP();
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = 'mail.email.org';
$mail->SMTPSecure = 'ssl'; <-Could be TLS
$mail->Port = 465; <- Could be 587 or 25
$mail->SMTPAuth = true;
$mail->Username = 'Mail@email.org'; <-To access your Hosting email
$mail->Password = 'Password';
/* Set the mail sender. */
$mail->setFrom('Mail@email.org'); <- From Myself
$mail->addAddress('Mail@email.org'); <-To Myself
$mail->Subject = 'Research Requested';
$mail->isHTML(TRUE);
$mail->Body = '<html> First Name: '.$first_name.' Last Name: '.$last_name.' Email: '.$email.' Age: '.$age.' Student: '.$student.' Agree18: '.$agree18.'
';
Focus Point ----------------
$mail->addAttachment('File Path', 'Type');
----------------------------
$mail->send()
} <--Added Closing
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="container">
<form method="POST" enctype=multipart/form-data action="#"> <--added #
<h4>Want to Learn More? Please fill out the form below.</h4>
<div class="form-group">
<label for="FirstName">First Name:</label>
<input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="First Name" required="">
</div>
<div class="form-group">
<label for="LastName">Last Name:</label>
<input type="text" class="form-control" name="LastName" id="LastName" placeholder="Last Name"required="">
<div class="form-group">
<label for="Age">How old are you?</label>
<input type="text" class="form-control" name="Age" id="Age" placeholder="Age"required="">
</div>
<div class="form-group">
<label for="Email">Email:</label>
<input type="email" class="form-control" name="Email" id="Email" placeholder="Mail@help.org" required="">
<h4Please Select if your a Student?</h4>
<div><small class="form-text text-muted">
Select all that apply.
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="Student" id="Student" value="Yes" >
<label class="custom-control-label" for="Student">Student</label>
Focus Point ----------------
<h4>Please upload a Photo:</h4>
<br>
<div class="form-group">
<label for="Photo">Photo</label>
<input type="file" class="form-control-file" id="Photo">
<div class="form-group">
<label for="Photo2">Photo 2</label>
<input type="file" class="form-control-file" id="Photo2">
------------------------------------------
<h4>Please agree to the following:</h4>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="Agree18" id="Agree18" value="Yes" required="" >
<label class="custom-control-label" for="Agree18">I can prove I am 18 years or older. I am legal age to participate according to my country. Proof may be required by law.</label>
根据我们可以要求的披露政策,所有信息将严格保密。
<div class="submit">
<button type="submit" name="submit" class="btn btn-primary">Send <i class="fa fa-lock"></i></button>
</form>
</div>
我希望有一个有效的更新示例,它符合 2019 年的安全标准。我希望有一个文件上传示例和一个从 Web 表单上传多个文件的示例。我还想对每个部分进行详细解释,如果可能的话,为什么它会这样工作。我想玩弄执着心,但一直不明白是怎么回事。
这是一个非常模糊的问题,可能会导致您因过于宽泛而获得接近的选票,而且您似乎忽略了很容易找到的资源。
PHPMailer 提供了两个文件,它们提供了 how to send one file, and how to send multiple files 的示例,它们绝对是您编写代码的基础。
就“2019 年标准”而言,它们与过去 10 年的标准并没有太大区别。它主要归结为简单的事情:
- 清理你的输入(删除你知道是坏的东西)
- 验证您的输入(检查您剩下的内容是否符合您的预期)
- 根据上下文适当地转义您的输出(例如,HTML转义不适用于将数据写入 CSV 文件)
因此,例如在您的代码中,您可以这样做:
$email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
这会检查电子邮件字段是否存在,仅此而已。据您所知,它可能是 War 和 Peace 的 base64 编码 PDF,它仍然会通过此检查。在最近的 PHP 版本中,您实际上可以使用 the null coalesce operator:
在更少的代码中执行相同的检查
$email = $_POST['Email'] ?? '';
但是,这仍然没有进行任何过滤或验证。 PHPMailer 在接受地址的任何方法上提供自动电子邮件地址验证,因此您可以使用:
if (!isset($_POST['email']) || !$mail->addReplyTo($_POST['Email'])) {
die('Invalid email address');
}
顺便说一下,这是在联系表单中使用提交者地址的正确方法;你没有把它放在发件人地址中(因为那会是伪造的),但你仍然可以在收到邮件时回复提交者。
另一个例子是你的Agree18
字段;这是一个复选框,因此它应该根本不存在(如果未选中该复选框),或者存在并具有 yes
的值(尽管您可以忽略该值 - 它的存在就足够了)。所以 强制执行该模式 - 您现有的代码将完全接受该字段中的任何内容(请记住 HTML 不是针对此的任何保护)。所以你可以说:
$agree18 = isset($_POST['Agree18']) ? 'yes' : 'no';
现在没有办法通过那个领域引入任何不好的东西;如果存在则为 yes
,否则为 no
。干净、简单、安全。
转义输出的主要规则是不显示用户提供的任何内容。比如这是wide-open到XSS攻击:
echo $_POST['Email'];
这多更安全:
echo htmlspecialchars($_POST['Email'], ENT_QUOTES);
如果您已经验证它包含一个有效的电子邮件地址,会更安全。
总之,你应该已经有了想法了。
2019 年使用 PHP Mailer v6.0.7 和 PHP 发送多个附件的最佳方式是什么?我读过不同的方法,有些人说这是错误的,因为安全问题和时代总是在变化。我也在使用实时托管服务器。我在代码中添加了 Focus Point ----------------。
我还没有真正做任何事情,因为我不确定 2019 年的最佳实践是什么。我已经阅读了很多重复的内容,这个也可能成为旗帜,但它们有很多 PHP 代码好与坏,谁知道哪一个仍然是 2019 年最好的。我将使用我的另一个 post 中的最后一个工作示例,但我想添加附件。
我的重点是正确 PHP 以正确的方式上传一个文件和多个附件,$Mail->Attachement with example 以及将文件加载到临时目录中的实时服务器是否真的安全HTML 代码适用于我认为它们已经存在的附件。
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP; <- This fixed most of my issues but not always required per Sychro
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer(true);
if(isset($_POST[‘submit’]))
{ <--- Added Opening
$first_name = ((isset($_POST['FirstName']))&&(!is_null($_POST['FirstName'])))? $_POST['FirstName']:'';
$last_name = ((isset($_POST['LastName']))&&(!is_null($_POST['LastName'])))? $_POST['LastName']:'';
$email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
$age = ((isset($_POST['Age']))&&(!is_null($_POST['Age'])))? $_POST['Age']:'';
$student = ((isset($_POST['Student']))&&(!is_null($_POST['Student'])))? $_POST['Student']:'';
$agree18 = ((isset($_POST['Agree18']))&&(!is_null($_POST['Agree18'])))? $_POST['Agree18']:'';
Focus Point ----------------
Any Security PHP Code for Uploaded Files Updated to 2019 Standards
----------------------------
Note: Hosting Service should provide this infor to you.
----------
$mail->isSMTP();
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = 'mail.email.org';
$mail->SMTPSecure = 'ssl'; <-Could be TLS
$mail->Port = 465; <- Could be 587 or 25
$mail->SMTPAuth = true;
$mail->Username = 'Mail@email.org'; <-To access your Hosting email
$mail->Password = 'Password';
/* Set the mail sender. */
$mail->setFrom('Mail@email.org'); <- From Myself
$mail->addAddress('Mail@email.org'); <-To Myself
$mail->Subject = 'Research Requested';
$mail->isHTML(TRUE);
$mail->Body = '<html> First Name: '.$first_name.' Last Name: '.$last_name.' Email: '.$email.' Age: '.$age.' Student: '.$student.' Agree18: '.$agree18.'
';
Focus Point ----------------
$mail->addAttachment('File Path', 'Type');
----------------------------
$mail->send()
} <--Added Closing
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="container">
<form method="POST" enctype=multipart/form-data action="#"> <--added #
<h4>Want to Learn More? Please fill out the form below.</h4>
<div class="form-group">
<label for="FirstName">First Name:</label>
<input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="First Name" required="">
</div>
<div class="form-group">
<label for="LastName">Last Name:</label>
<input type="text" class="form-control" name="LastName" id="LastName" placeholder="Last Name"required="">
<div class="form-group">
<label for="Age">How old are you?</label>
<input type="text" class="form-control" name="Age" id="Age" placeholder="Age"required="">
</div>
<div class="form-group">
<label for="Email">Email:</label>
<input type="email" class="form-control" name="Email" id="Email" placeholder="Mail@help.org" required="">
<h4Please Select if your a Student?</h4>
<div><small class="form-text text-muted">
Select all that apply.
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="Student" id="Student" value="Yes" >
<label class="custom-control-label" for="Student">Student</label>
Focus Point ----------------
<h4>Please upload a Photo:</h4>
<br>
<div class="form-group">
<label for="Photo">Photo</label>
<input type="file" class="form-control-file" id="Photo">
<div class="form-group">
<label for="Photo2">Photo 2</label>
<input type="file" class="form-control-file" id="Photo2">
------------------------------------------
<h4>Please agree to the following:</h4>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="Agree18" id="Agree18" value="Yes" required="" >
<label class="custom-control-label" for="Agree18">I can prove I am 18 years or older. I am legal age to participate according to my country. Proof may be required by law.</label>
根据我们可以要求的披露政策,所有信息将严格保密。
<div class="submit">
<button type="submit" name="submit" class="btn btn-primary">Send <i class="fa fa-lock"></i></button>
</form>
</div>
我希望有一个有效的更新示例,它符合 2019 年的安全标准。我希望有一个文件上传示例和一个从 Web 表单上传多个文件的示例。我还想对每个部分进行详细解释,如果可能的话,为什么它会这样工作。我想玩弄执着心,但一直不明白是怎么回事。
这是一个非常模糊的问题,可能会导致您因过于宽泛而获得接近的选票,而且您似乎忽略了很容易找到的资源。
PHPMailer 提供了两个文件,它们提供了 how to send one file, and how to send multiple files 的示例,它们绝对是您编写代码的基础。
就“2019 年标准”而言,它们与过去 10 年的标准并没有太大区别。它主要归结为简单的事情:
- 清理你的输入(删除你知道是坏的东西)
- 验证您的输入(检查您剩下的内容是否符合您的预期)
- 根据上下文适当地转义您的输出(例如,HTML转义不适用于将数据写入 CSV 文件)
因此,例如在您的代码中,您可以这样做:
$email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
这会检查电子邮件字段是否存在,仅此而已。据您所知,它可能是 War 和 Peace 的 base64 编码 PDF,它仍然会通过此检查。在最近的 PHP 版本中,您实际上可以使用 the null coalesce operator:
在更少的代码中执行相同的检查$email = $_POST['Email'] ?? '';
但是,这仍然没有进行任何过滤或验证。 PHPMailer 在接受地址的任何方法上提供自动电子邮件地址验证,因此您可以使用:
if (!isset($_POST['email']) || !$mail->addReplyTo($_POST['Email'])) {
die('Invalid email address');
}
顺便说一下,这是在联系表单中使用提交者地址的正确方法;你没有把它放在发件人地址中(因为那会是伪造的),但你仍然可以在收到邮件时回复提交者。
另一个例子是你的Agree18
字段;这是一个复选框,因此它应该根本不存在(如果未选中该复选框),或者存在并具有 yes
的值(尽管您可以忽略该值 - 它的存在就足够了)。所以 强制执行该模式 - 您现有的代码将完全接受该字段中的任何内容(请记住 HTML 不是针对此的任何保护)。所以你可以说:
$agree18 = isset($_POST['Agree18']) ? 'yes' : 'no';
现在没有办法通过那个领域引入任何不好的东西;如果存在则为 yes
,否则为 no
。干净、简单、安全。
转义输出的主要规则是不显示用户提供的任何内容。比如这是wide-open到XSS攻击:
echo $_POST['Email'];
这多更安全:
echo htmlspecialchars($_POST['Email'], ENT_QUOTES);
如果您已经验证它包含一个有效的电子邮件地址,会更安全。
总之,你应该已经有了想法了。