douchungu0859 2019-06-14 15:52
浏览 141

2019如何使用PHPMailer V6.0.7和PHP从Web窗体发送多个附件

What is the current best way to send multiple attachments with PHP Mailer v6.0.7 and PHP for 2019? I have read different methods and some say that is wrong due to security issues and times are always changing. I am using a live hosting server as well. I have added Focus Point ---------------- to the code.

I have not really done anything because I am not sure what are 2019 best practices. I have read a lot of duplicates and this one might become flag as well but they're a lot of PHP code Good and Bad and which one is still the best in 2019 who knows. I will use my last working example from my other post but I want to add in attachments.

My focus is the correct PHP for uploading one file and multiple attachments the proper way, $Mail->Attachement with example and if it is really safe to load files to a live server in the temp directory and HTML code is proper for the attachments which I assume they already are.

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

 { <--- 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->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = ''; 
$mail->SMTPSecure = 'ssl'; <-Could be TLS 
$mail->Port = 465; <- Could be 587 or 25
$mail->SMTPAuth = true;
$mail->Username = ''; <-To access your Hosting email
$mail->Password = 'Password'; 

 /* Set the mail sender. */
$mail->setFrom(''); <- From Myself
$mail->addAddress(''); <-To Myself

$mail->Subject = 'Research Requested';

$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');  
} <--Added Closing


<!DOCTYPE html>
<html lang="en">


<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 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 class="form-group">
<label for="Email">Email:</label>
<input type="email" class="form-control"  name="Email" id="Email" placeholder="" 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> 
<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>

All information will be held strictly confidential in accordance with our disclosure policy which can be requested.

<div class="submit">
<button type="submit" name="submit" class="btn btn-primary">Send <i class="fa fa-lock"></i></button>


I am hoping to have an updated example that works and that is up to 2019 standard with security in mind. I am hoping to have one example for one file upload and one example for multiple file upload from a web form. I would also like a detail explanation on each part and why it works that way if possible. I want to play around with attachments but never really understood what going on.

  • 写回答



      相关推荐 更多相似问题


      • ¥15 用MATLAB解决问题,需要代码
      • ¥45 关于C语言设计井字棋游戏问题的实现
      • ¥15 python定义一个学生类
      • ¥20 vuforia的Unity3D开发,模型附上特效后大量报错
      • ¥35 Python实现文本关键词提取
      • ¥20 如何使用c++语言完成实验仪器管理系统
      • ¥15 请问Unity使用Magica Cloth插件怎么调整散兵的帘子和振袖比较好?
      • ¥15 请问为什么原神模型导入Unity做动画默认姿势会站不直?
      • ¥15 ld: cannot open C:\Program: No such file or directory
      • ¥15 请问汏佬要怎么访问这种过滤了post和get的php内容?