1

PHP Email form with attachment not working by PHPMailer help me what I'm missing. Without form and PHP code it's working in localhost but when i merge with the form it's totally not working..

 <?php


    require 'vendor/autoload.php';
    require 'classes/config.php';

    if(isset($_POST['submit'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $mat = $_POST['mat'];
    $res = $_POST['res'];
    $tech = $_POST['tech'];
    $city = $_POST['city'];
    $message = $_POST['message'];
    $content='<br><br>Name: '.$name.'<br>'.'<br>Mobile: '.$mobile .'<br>Material: '.$mat.'<br>Res: '.$res.'<br>tech: '.$tech.'<br>City: '.$city .'<br>Message: '.$message.'<br>Email: '.$email;

    $file_name = ($_FILES['image']['name']);
    $file_tmp = ($_FILES['image']['tmp_name']);
    $file_path = "upload";

    if(move_uploaded_file($file_tmp, "uploads" . $file_name)){

    $mail = new PHPMailer\PHPMailer\PHPMailer();

        $mail->isSMTP();                                          
        $mail->Host       = Config::SMTP_HOST;                    

        $mail->Username   = Config::SMTP_USER;                     
        $mail->Password   = Config::SMTP_PASS;
        $mail->Port       = Config::SMTP_PORT;                               
        $mail->SMTPSecure = 'tls'; 
        $mail->SMTPDebug = 2;  
        $mail->SMTPAuth   = true;   
        $mail->isHTML(true); 
        $mail->CharSet = 'UTF-8';
        $mail->setFrom('mail');
        $mail->addAddress('mail');
        $mail->Subject = 'Got a Enquiry';
        $mail->Body    = $content;
        $mail->addAttachment($file_path, $file_name);

        if($mail->send()){

          echo 'Super';
        } else {

        echo 'nothing:' .$mail->ErrorInfo;

    }
    }
    }

    ?>



<form class="montform" id="reused_form" enctype=&quot;multipart/form-data&quot; >

                        <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 bzd-upload-1-1">
                            <div class="col-lg-12 frm-bg">
                                <img src="img/upload-icon.png" class="img-responsive">
                                 <p>Drag and Drop Files<br/>
                        or Upload Here</p>
                          <p class="file">

                                    <label for="file_attach">
                               <h6>Browse</h6>
                            </label>
                            <input name="image" type="file" id="file" class="feedback-input">
                        </p>
                            </div>



                        </div>

                         <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 bzd-form-1-1">


                        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
                            <label class="form-1-1">Name</label>

                        <p class="name">

                            <input name="name" type="text" class="feedback-input" required placeholder="Name" id="name" />

                        </p>
                             </div>


                       <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">  
                           <label class="form-1-1">Email</label>
                        <p class="email">

                            <input name="email" type="email" required class="feedback-input" id="email" placeholder="Email" />

                        </p>
                             </div>

                        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">     
                            <label class="form-1-1">Mobile No</label>
                        <p class="name">

                            <input name="phone" type="tel" class="feedback-input" required placeholder="Phone" id="phone" />

                        </p>
                             </div>

                         <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">
                             <label class="form-1-1">Material</label>
                         <p class="name">

                            <input name="mat" type="tel" class="feedback-input" required placeholder="Material" id="mat" />

                        </p>
                             </div>

                         <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">  
                             <label class="form-1-1">Resolution</label>
                         <p class="name">

                            <input name="res" type="tel" class="feedback-input" required placeholder="Resolution" id="res" />

                        </p>
                             </div>

                        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">   
                            <label class="form-1-1">Technology</label>
                         <p class="name">

                            <input name="tech" type="tel" class="feedback-input" required placeholder="Technology" id="tech" />

                        </p>
                             </div>

                        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group">  
                            <label class="form-1-1">City</label>
                        <p class="name">

                            <input name="city" type="text" class="feedback-input" required placeholder="City" id="city" />

                        </p>
                             </div>

                       <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 form-group"> 
                           <label class="form-1-1">Message</label>
                        <p class="text">

                            <textarea name="message" class="feedback-input" id="comment" placeholder="Message"></textarea>

                        </p>
                             </div>

                              <div class="submit">
                            <button type="submit" class="button-blue">SUBMIT</button>
                            <div class="ease"></div>
                        </div>

                        </div>    

                    </form>

When I click the submit button it's loading... No error displayed and also i'm not getting mail. I am also search some more articles but still it is not resolved.. kindly help me in this..

ROOT
  • 11,363
  • 5
  • 30
  • 45
chanwill3
  • 11
  • 3
  • sorry i don't understand – chanwill3 Mar 03 '20 at 07:18
  • 2
    Change your form line to this : `
    " class="montform" id="reused_form" enctype="multipart/form-data">` you dont have post method in your form, it doesnt post your values to php page, I just updated answer.if your php codes are not in same page! change this `` to your page.php in action
    –  Mar 03 '20 at 10:33
  • 2
    `var_dump($file_name);` see if its not empty and correct your path `move_uploaded_file($file_tmp, "uploads" . $file_name)){` and search for uploading files. here some examples https://stackoverflow.com/questions/18929178/move-uploaded-file-function-is-not-working –  Mar 03 '20 at 10:44
  • 1
    I corrected your upload codes in answer check down the bottom. –  Mar 03 '20 at 12:36

2 Answers2

3

You are checking this:

if(isset($_POST['submit'])){

but your form does not contain an input named submit, so this condition will never be true and your sending code will not be run.

You have this submit button:

<button type="submit" class="button-blue">SUBMIT</button>

but it has no name or value attribute. Change it to this:

<button type="submit" name="submit" class="button-blue">SUBMIT</button>
Synchro
  • 35,538
  • 15
  • 81
  • 104
1

You should check your error_log file it would show you whats going wrong.

Actualy $mail->SMTPDebug = 2; would create troubleshoots of errors too, so maybe your emails going to spam box.

Here is the code working on my localhost :

require_once "vendor/autoload.php";

$mail = new PHPMailer\PHPMailer\PHPMailer(true);
//Or $mail = new PHPMailer; 
//Enable SMTP debugging. 
$mail->SMTPDebug = 3; 
//Set $mail->SMTPDebug = 0; on live sites.                        
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "useremail@gmail.com";                 
$mail->Password = "password";                           
//If SMTP requires TLS encryption then set it
//$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;                         
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "name@gmail.com";
$mail->FromName = "Full Name";

$mail->smtpConnect(
    array(
        "ssl" => array(
            "verify_peer" => true,
            "verify_peer_name" => true,
            "allow_self_signed" => false
        )
    )
);
//This won't require any server settings on your localhost.

$mail->addAddress("Recepient@email.com", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

Change credentials to yours, and see if your emails not going to spam box.

Change your form line to this : <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="montform" id="reused_form" enctype="multipart/form-data"> you dont have post method in your form, it doesnt post your values to php page.

NOTE : if your php codes are not in same page! change this <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> to your page.php in action.

Change Your upload codes to :

$file_name = ($_FILES['image']['name']);
$file_tmp = ($_FILES['image']['tmp_name']);
$file_path = "upload/";

if(move_uploaded_file($file_tmp, $file_path.$file_name)){
Community
  • 1
  • 1
  • Hi, i have make the change
    but not working...
    – chanwill3 Mar 03 '20 at 08:56
  • 1
    Did you try `` whats being said in other answer ? do that and then try my answer. and what means not working ? do you get any error ? or what is in error_log file ? or what troubleshoot says ? see here for debug https://stackoverflow.com/a/22662582/12232340 –  Mar 03 '20 at 09:55
  • Yes I have tried it & I'm not getting any error!.. The form simply loading & my inputs are in URL Like "(http://localhost/test/?image=3.JPG&name=Bala&email=krishnanbkcl%40gmail.com&phone=9940061266&mat=bihba&res=dv&tech=bebedb&city=bgb&message=bgbgbg&name=)" – chanwill3 Mar 03 '20 at 10:01
  • 3
    did you check error log file ? put this on top of your page `error_reporting(E_ALL); ini_set('display_errors', 1);` see usaqe here : https://phpdelusions.net/articles/error_reporting it should display errors now and please check your spam box. –  Mar 03 '20 at 10:04
  • Now I receive the mail except the attachment. The error is "(Could not access file: upload)" – chanwill3 Mar 03 '20 at 10:39
  • 4
    You are uploading file to uploads diresctory! `if(move_uploaded_file($file_tmp, "uploads" . $file_name)){` and trying to access it from `$file_path = "upload";` your directories doesnt match both should be `upload` or `uploads` I dont know your correct path. –  Mar 03 '20 at 10:57
  • Hi, I have chamged the uploads to upload but still i'm not getting the attachment and also i have pasted error_reporting(E_ALL); – chanwill3 Mar 03 '20 at 11:25
  • I'm not getting any error but the attachment was not sending – chanwill3 Mar 03 '20 at 11:26
  • 3
    Thank You So much I figured out the issue and solved now my form is working fine... – chanwill3 Mar 03 '20 at 12:00
  • Glad you solved it Can you please check answer as accepted please, if it helped. –  Mar 03 '20 at 12:06
  • 1
    $file_name = ($_FILES['image']['name']); $file_tmp = $_FILES['image']['tmp_name']; $path = 'upload'; $file = $path . basename($_FILES['image']['name']); if(move_uploaded_file($_FILES['image']['tmp_name'], $file)) "Could pls help me in that my uploaded file not placed in 'upload ' folder instead that my uploaded file placed in main project folder. $mail->addAttachment($file, $_FILES['image']['name']); – chanwill3 Mar 03 '20 at 12:16
  • Don't disable TLS certificate verification, and especially don't advise others to do so. Google does not use invalid certificates, so if it's not validating, it's **your server that's at fault**, so you should fix that instead of making your application vulnerable to person-in-the-middle attacks. – Synchro Mar 03 '20 at 12:51
  • 2
    @Synchro *it's working in localhost* dont think I need ssl or auth for securing. also its commented *This won't require any server settings on your localhost* on localhost. and commented for tls too. but thanks for warning. –  Mar 03 '20 at 12:54
  • You're not sending to `localhost`, you're sending to `smtp.gmail.com`. You do most definitely need both encryption and authentication to send through gmail, and it's not commented out in the code you posted. Setting those options will suppress error messages due to verification failure, giving you the false impression that "it's working on localhost" - and that is a very bad thing to do as it undermines much of the point of using TLS in the first place. – Synchro Mar 03 '20 at 13:00