我使用PHPMailer,JavaScript和PHP,通过我的代码将电子邮件发送给特定用户,下一步是将文件附加到电子邮件中.
但是我对此有两个问题:
>第一个与以下代码相关:
HTML部分:
<input type="file" name="myFile" id="myFile" required/>
我正在使用这个附件来附加文件,这是电子邮件的一部分
JavaScript部分,我的发送电子邮件的功能(到目前为止,我可以发送包含测试数据的电子邮件)
function SendMail(){
var cod="1234"; var subject="hello my friend";
$.get("SendMail.php?cod="+cod+"&subject="+subject+""); // (1)
}
在(1)中如何发送输入文件附加到我的php文件中?
喜欢,
$.get("SendMail.php?cod="+cod+"&subject="+subject+"&mail="FileRelatedWithInputID);
>在PHP中成功获取文件后,如何使用PHPMailer对其进行格式化以将其附加到电子邮件中?
就这样,我对这个问题的两个问题,非常感谢您的回答和建议.
感谢您的时间和关注.
编辑:
我的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");
解决方法:
您正在尝试使用get方法发送文件. Get方法不支持多部分.对于上传文件,您应该使用post方法,并且表单应该具有multipart / form-data.您的表格应为
<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" />
在表单提交中调用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);
}
});
}
在服务器端更新用于附件的发送邮件代码,并以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");