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.

<?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>

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>




</form>  
</div>

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.

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 磁盘异常导致工业相机GigE传图卡顿
      • ¥20 python中使用chatgpt为什么一直连接失败
      • ¥50 使用grpc遇到的问题
      • ¥100 运算速度优化问题,控制在四秒以内
      • ¥15 如何用matlab画出这样的图
      • ¥15 线性回归问题进行特征缩放后,为什么求得的参数不对了
      • ¥15 python爬取网页信息(主要为期刊影响因子、官网等),已有ISSN进行检索,但爬取过程找不到class该怎么办?
      • ¥15 找出5阶幻方的所有解的个数:275305224
      • ¥20 wireshark无网络波动
      • ¥15 C#ArcEngine二次开发引入类似Arcmap内属性表内容的操作