简体   繁体   English

将输入类型文件(HTML)转换为JavaScript并使用PHPmailer发送电子邮件

[英]Take Input Type File (HTML) to JavaScript and Send an Email Using PHPmailer

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. 我正在使用PHPMailer,JavaScript和PHP,通过我的代码我将电子邮件发送给特定用户,下一步是附上电子邮件的文件。

But i have two questions about that: 但我有两个问题:

  1. first one is related with the following code: 第一个与以下代码相关:

HTML part: HTML部分:

<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) JavaScript部分,我发送电子邮件的功能(到目前为止我可以发送带有测试数据的电子邮件)

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? 在(1)我如何发送附加到我的PHP文件的输入文件?

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? 当我在PHP中成功获取文件时,如何使用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: 我的PHP文件:

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

You are try to send file using get method. 您尝试使用get方法发送文件。 Get method not support multipart. 获取方法不支持multipart。 For upload file you should use post method and form should have multipart/form-data. 对于上传文件,您应该使用post方法,表单应该有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. 在表单提交中调用SendMail函数。

$("#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. 更新服务器端的发送邮件代码以获取附件文件,并使用post方法而不是get方法获取数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM