Take Input Type File (HTML) to JavaScript

2020-04-10 06:02发布

问题:

i'm using PHPMailer, JavaScript and PHP, through my code i send the email to specific user, next step is attach a file with the email.

But i have two questions about that:

  1. first one is related with the following code:

HTML part:

<input type="file" name="myFile" id="myFile" required/>

I'm using this one to attach the file, part of the email

JavaScript part, my function to send email (so far i can send an email with test data)

function SendMail(){
    var cod="1234"; var subject="hello my friend";
    $.get("SendMail.php?cod="+cod+"&subject="+subject+""); // (1)
}

In (1) how can i send the input file attached to my php file?

Like,

    $.get("SendMail.php?cod="+cod+"&subject="+subject+"&mail="FileRelatedWithInputID);
  1. When i got successfully the file in PHP, how have i to format it to attach it to the email using PHPMailer?

That's it, my two questions about this issue, i appreciate your answers and suggestions.

Thank you for your time and attention.

Edited:

My PHP file:

require '../PHPMailer_5.2.4/PHPMailerAutoload.php';
$server = "localhost";
$user = "root";
$pass = "pass";
$bd = "BD";
$strHTML1=$_GET["cod"];
$strHTML2=$_GET["subject"];


 // HOW DO I _GET THE FILE FROM JS AND FORMAT FILE PROPERLY TO THE MAIL?


$strHTML3= $strHTML1.$strHTML2;

$mail="mail@gmail.com";

session_start(); 

$conexion = mysqli_connect($server, $user, $pass,$bd) 
or die("ERROR");

 $mail = new PHPMailer();
 $mail->isSMTP();
 $mail->CharSet = "UTF-8";
 $mail->SMTPDebug = 2;
 $mail->Mailer = "smtp";
 $mail->WordWrap = 50;  
 $mail->PluginDir = "../tickets/PHPMailer_5.2.4/";
 $mail->Debugoutput = 'html';
 $mail->Host = 'smtp.gmail.com';
 $mail->Port = 465;
 $mail->SMTPSecure = 'ssl';
 $mail->SMTPAuth = true; 
 $mail->Username = "mail@site.com.en";  
 $mail->Password = "12345";
 $mail->SMTPSecure = 'ssl';  
 $headers = "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
 $headers .= "From:Home\r\n"; 
 $mail->AddAddress($mail, "test");
 $mail->isHTML(true); 
 $mail->Subject = $strHTML2;
 $mail->Body    =$strHTML3;



 if(!$mail->send()) {
     echo 'Message could not be sent.';
     echo 'Mailer Error: ' . $mail->ErrorInfo.'  ';
     exit;
   }
 $close = mysqli_close($conexion) 
 or die("ERROR");

回答1:

You are try to send file using get method. Get method not support multipart. For upload file you should use post method and form should have multipart/form-data. Your form should be like as

<form method="post" action="" enctype="multipart/form-data" id="sendMailForm">
<input type="hidden" name="code" id="code" value="1234"/>
<input type="hidden" name="subject" id="subject" value="Hello My Friends"/>
<input type="submit" value="Send Mail" />

Call SendMail function in form submit.

$("#sendMailForm").submit(function(evt){     
   evt.preventDefault();
   var formData = new FormData($(this)[0]); 
   SendMail(formData);
   return false;
 });

function SendMail(formData){
    $.ajax({
     url: 'SendMail.php',
     type: 'POST',
     data: formData,
     async: false,
     cache: false,
     contentType: false,
     enctype: 'multipart/form-data',
     processData: false,
     success: function (response) {
       alert(response);
     }
   });
}

Update your send mail code at server side for attach file and get data in post method instead of get method.

require '../PHPMailer_5.2.4/PHPMailerAutoload.php';
$server = "localhost";
$user = "root";
$pass = "pass";
$bd = "BD";
$strHTML1=$_POST["cod"];
$strHTML2=$_POST["subject"];

$strHTML3= $strHTML1.$strHTML2;

$mail="mail@gmail.com";

session_start(); 

$conexion = mysqli_connect($server, $user, $pass,$bd) 
or die("ERROR");

 $mail = new PHPMailer();
 $mail->isSMTP();
 $mail->CharSet = "UTF-8";
 $mail->SMTPDebug = 2;
 $mail->Mailer = "smtp";
 $mail->WordWrap = 50;  
 $mail->PluginDir = "../tickets/PHPMailer_5.2.4/";
 $mail->Debugoutput = 'html';
 $mail->Host = 'smtp.gmail.com';
 $mail->Port = 465;
 $mail->SMTPSecure = 'ssl';
 $mail->SMTPAuth = true; 
 $mail->Username = "mail@site.com.en";  
 $mail->Password = "12345";
 $mail->SMTPSecure = 'ssl';  
 $headers = "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
 $headers .= "From:Home\r\n"; 
 $mail->AddAddress($mail, "test");
 $mail->isHTML(true); 
 $mail->Subject = $strHTML2;
 $mail->Body    =$strHTML3;

 //Attach file in sendmail - 

if (isset($_FILES['myFile']) &&
    $_FILES['myFile']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['myFile']['tmp_name'],
    $_FILES['uploaded_file']['name']);
}

 if(!$mail->send()) {
     echo 'Message could not be sent.';
     echo 'Mailer Error: ' . $mail->ErrorInfo.'  ';
     exit;
   }
 $close = mysqli_close($conexion) 
 or die("ERROR");