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

后端 未结 1 549
一个人的身影
一个人的身影 2021-01-20 16:13

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 abou

相关标签:
1条回答
  • 2021-01-20 17:02

    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");
    
    0 讨论(0)
提交回复
热议问题