Jump to content

PHP Mailer <--> HTML Form


Smeet
 Share

Recommended Posts

Hi All,

Need your help with the situation I am stuck in.

I have created a HTML Form and using PHP file saving that data into database. But now I want to send out an generic email to each visitor who fill the form.

Attaching the code of html form and php database. It would be highly appreaciated if you can help me in updating the php file with which I can send an auto email.

 

HTML Form - 

                                        <!-- .contact-form -->

                                        <div class="contact-form">


                                            <form id="contact-form" class="validate-form" method="post"

                                                action="send.php">


                                                <!-- enter mail subject here -->

                                                <input type="hidden" name="subject" id="subject"

                                                    value="You have a new message from Smeet Mehta!">


                                                <p>

                                                    <label for="name">NAME</label>

                                                    <input type="text" name="name" id="name" class="required">

                                                </p>


                                                <p>

                                                    <label for="email">EMAIL</label>

                                                    <input type="text" name="email" id="email" class="required email">

                                                </p>


                                                <p>

                                                    <label for="message">MESSAGE</label>

                                                    <textarea name="message" id="message" class="required"></textarea>

                                                </p>


                                                <p>

                                                    <button class="submit" control-id="ControlId-5">Send</button>

                                                </p>


                                            </form>


                                        </div>

                                        <!-- .contact-form -->

 

 

PHP Code - 

<?php


$subject = filter_input(INPUT_POST, 'subject');

$name = filter_input(INPUT_POST, 'name');

$email = filter_input(INPUT_POST, 'email');

$message = filter_input(INPUT_POST, 'message');



$host = "localhost";

$username = "root";

$password = "";

$dbname = "contact";


$conn = new mysqli ($host, $username, $password, $dbname);


if (mysqli_connect_error()){

    die('Connection Error('.mysqli_connect_error().')'.mysqli_connect_error());

    }

else {

    $sql = "INSERT INTO form (subject, name, email, message) values ('$subject', '$name', '$email', '$message')" ;

    if ($conn->query($sql)){

        header("Refresh:0; url=index.html");

    }

    else{

        echo "Error: ".$sql ."

        ".$conn->error;

    }

    $conn->close();

}

?>

Please Help

Edited by Barand
code tags added
Link to comment
Share on other sites

First your code is very difficult to read. Edit the post and use the code icon (<>) at the top of the menu and specify PHP or HTML as appropriate. Then post the error message you are getting making sure you have error reporting turned on.

error_reporting(E_APLL);

If no error then explain what you are getting that is different than what you expect.

Link to comment
Share on other sites

Posted (edited)
19 hours ago, maxxd said:

You should include PHPMailer in your php file, then use it to send the email after the record has been successfully added to the database.

Hey, I have tried with PHPMailer but no luck.

Can you please review the code

 

<?php

$subject = filter_input(INPUT_POST, 'subject');
$name = filter_input(INPUT_POST, 'name');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');


$host = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";

$conn = new mysqli ($host, $username, $password, $dbname);

if (mysqli_connect_error()){
    die('Connection Error('.mysqli_connect_error().')'.mysqli_connect_error());
    }
else {
    $sql = "INSERT INTO form (subject, name, email, message) values ('$subject', '$name', '$email', '$message')" ;
    if ($conn->query($sql)){
        header("Refresh:0; url=index.html");
    }
    else{
        echo "Error: ".$sql ."
        ".$conn->error;
    }
    $conn->close();
}

include("smtp/PHPMailerAutoload.php");
$mail = new PHPMailer();
//$mail->isSMTP();
$mail->Host = "smtp.office365.com";
$mail->Port=587;
$mail->SMTPAuth = true;
$mail->Username = "email-address";
$mail->Password = "email-password";
$mail->From = $mail->$Username;
$mail->FromName = "Smeet Mehta";
$mail->AddAddres($email);

$mail->WordWrap = 1000;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;

if(!$mail->Send())
{
    echo "Message could not be sent. <p>";
    echo "Mailer Error".$mail->ErrorInfo;
    exit;
}
else
{
    echo "Message has been sent";
}


?>

 

Edited by Smeet
Link to comment
Share on other sites

OK, now we've got the part of the code that matters. It would also be very helpful if you would provide any errors that are happening, or just what is happening in general.

That being said, there are a few things; first and foremost, your mail host is smtp.office365.com, but you've turned off SMTP in PHPMailer by commenting out the $mail->isSMTP() line. Secondly, as far as I know, there isn't a `smtp/PHPMailerAutoload.php` file - the instructions tell you to include `vendor/autoload.php` and then use the PHPMailer, SMTP, and Exception namespaces. And finally, turn on verbose error reporting with the line $mail->SMTPDebug = SMTP::DEBUG_SERVER and see what's actually happening behind the scenes.

One more thing I forgot. send() doesn't return a boolean, it throws an error on failure so put all of the PHPMailer code inside a try...catch block.

Edited by maxxd
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.