Jump to content

send auto response email after adding data to database


ianhaney50
Go to solution Solved by fastsol,

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.