ianhaney50 Posted June 27, 2015 Share Posted June 27, 2015 Hi I am trying to make the form add the data to the mysql database which it does perfect but am trying to get the code to send a auto repsonse email to the email address of the user who just submitted their email address to store within the database but the email is not being sent The coding I got is below <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); ?> <?php $title = "The Tax Elephants"; $pgDesc=""; $pgKeywords=""; include ( 'includes/header.php' ); ?> <!--CONTENT--> <div id="column-center"> <div id="content"> <div class="text"> <h2>Welcome to Our Pre-launch Page</h2> </div> <div class="counter"> <h3 class="top-text">Because us elephants are perfectionists. We are just doing some more improvements and ensuring our website is perfect for you.</h3> <br /> <div class="love-text"> <img src="images/elephants-family.png" alt="" title="" /> <br /> Lots of Love <br /> Tusky, Honey & Eloise </div> <h3>Estimated Time Remaining Before Launch:</h3> <div id="defaultCountdown"></div> <div class="details"> <h3>Enter your email address below to be notified of website updates</h3> <form action="" method="post"> <div id="email_input"> <input type="email" name="email" id="email" size="30" placeholder="Enter your email address"> <input type="submit" name="submit" id="submit" value="SUBMIT" size="80" /> </div> </form> <!-- End Subscription Form --> <br /><br /> <h3>Click below to share our page with your friends:</h3> <div class="social"> <!-- Facebook --> <a href="http://www.facebook.com/sharer.php?u=http://www.taxelephants.uk" target="_blank"><img src="./images/facebook.png" alt="Facebook" /></a> <!-- Twitter --> <a href="http://twitter.com/share?url=http://www.taxelephants.uk&text=Tax Elephants&hashtags=websitedesign" target="_blank"><img src="./images/twitter.png" alt="Twitter" /></a> <!-- Email --> <a href="mailto:?Subject=Tax Elephants &Body=I%20saw%20this%20and%20thought%20of%20you!%20 http://www.taxelephants.uk"><img src="./images/email.png" alt="Email" /></a> </div> </div><!--end details--> </div> </div> </div> <?php if(isset($_POST["submit"])){ $servername = ""; $username = ""; $password = ""; $dbname = ""; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // escape variables for security $email = mysqli_real_escape_string($conn, $_POST['email']); $sql = "INSERT INTO subscribers (email) VALUES ('$email')"; header("location:add.php"); $conn->query($sql); $email = $conn->insert_id; $result = $conn->query($sql); //define the receiver of the email $to = '$email'; //define the subject of the email $subject = 'Test email'; //define the message to be sent. Each line should be separated with \n $message = "Hello World!\n\nThis is my first mail."; //define the headers we want passed. Note that they are separated with \r\n $headers = "From: webmaster@example.com"; //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; if ($result !== false) { echo"fail"; } $conn->close(); } ?> <!--CONTENT--> <?php include( 'includes/footer.php' ); ?> I have tried moving the email script part right at the end after the close of the mysql, also moved it above the header location line but nothing seems to work, am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/ Share on other sites More sharing options...
fastsol Posted June 27, 2015 Share Posted June 27, 2015 2 main things I see quickly. 1. Remove the @ from in front of the mail(). You should honestly never use @ to suppress php errors. 2. Variables are not evaluated when inside 'single quotes' like you have the $email for the $to. Remove the single quotes around $email. Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515098 Share on other sites More sharing options...
ianhaney50 Posted June 27, 2015 Author Share Posted June 27, 2015 Ahh ok what does the @ in front do, does it open it up to attacks or something or is just not good practice? Will do both them two things now Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515100 Share on other sites More sharing options...
ianhaney50 Posted June 27, 2015 Author Share Posted June 27, 2015 I've done them two things Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515101 Share on other sites More sharing options...
fastsol Posted June 27, 2015 Share Posted June 27, 2015 http://php.net/manual/en/language.operators.errorcontrol.php Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515102 Share on other sites More sharing options...
ianhaney50 Posted June 27, 2015 Author Share Posted June 27, 2015 Ahh ok had a quick read through and understood the bit bout disabling error reporting the php is not producing any errors which is good but is still not sending any email to the email address I sign up with but is storing the email address in the database Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515105 Share on other sites More sharing options...
Solution fastsol Posted June 27, 2015 Solution Share Posted June 27, 2015 Move the header() down below the closing of the db connection. Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515106 Share on other sites More sharing options...
ianhaney50 Posted June 27, 2015 Author Share Posted June 27, 2015 Done that I have managed to get it working with the following code <?php if(isset($_POST["submit"])){ $servername = ""; $username = ""; $password = ""; $dbname = ""; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // escape variables for security $email = mysqli_real_escape_string($conn, $_POST['email']); $sql = "INSERT INTO subscribers (email) VALUES ('$email')"; header("location:add.php"); $conn->query($sql); $email = $conn->insert_id; $result = $conn->query($sql); //define the receiver of the email $to = $_POST['email']; //define the subject of the email $subject = 'Thank you for signing up'; //define the message to be sent. Each line should be separated with \n $message = "Thank you for signing up to receive regular updates on the launch of our website."; //define the headers we want passed. Note that they are separated with \r\n $headers = "From: noreply@taxelephants.uk"; //send the email $mail_sent = mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; if ($result !== false) { echo"fail"; } $conn->close(); header("location:add.php"); } ?> I just added in $_POST['email']; on the $to line and is working Is that ok to be in there as before I did not have it in there and the email would not send Quote Link to comment https://forums.phpfreaks.com/topic/297066-send-auto-response-email-after-adding-data-to-database/#findComment-1515108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.