0

I have written some code to make an email automatically send to the email address of the user at the click of a button. But for some reason is it not sending any emails, I have tested it with two different email addresses.

Code for button:

form action="sendellie.php" method="post">
<input type="submit" value="Buy tickets"/>
</form>

Code for processing page:

<?php
session_start();
    include 'connect.php';
     ini_set("sendmail_from", "********");

    $user_id= $_SESSION['id'];
    $sql = "SELECT username FROM user WHERE user_id = '$user_id'";

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    $uname = mysql_fetch_array($result);

     $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    $uemail = mysql_fetch_array($result);

    $user_id= $_SESSION['id'];
    $sql = "SELECT email FROM user WHERE user_id = '$user_id'";

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    $uemail = mysql_fetch_array($result);

     while($row = mysql_fetch_array($result))
     {
        $name = $uname['username'];
        $email = $uemail['email'];
        $message = "This email is to confirm you have purchased one Ellie Goulding Ticket for the O2";
        $subject = $uname['username'];
        echo "sent to"." ".$email." ".$subject." ".$message."<p />";

        mail($email, $subject, $message);
        }

        echo "Go check your mail box:";
        include 'close.php';
        ?>
    <?php echo $uemail['email'] ?><br>
    <a href="ellie.php">Back</a>
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
J S
  • 97
  • 2
  • 9
  • 1
    A lot of things can prevent an email from reaching somebody's inbox, few of which have to do with the code. Is there an error message in the logs? An error from the SMTP server? When you test this against a mock SMTP server that you control, what is the behavior? When you debug this, is the call to `mail()` reached at all? (Side note: Why do you hit the database twice to get two fields from the same record?) – David Jan 24 '14 at 14:09
  • To troubleshoot, take away anything that has nothing to do with sending mail. Instead, send a simple email to yourself and see if you get it. – Dan Bracuk Jan 24 '14 at 14:15
  • are you sure the user_id is a string? if it is a int, then do this: $sql = "SELECT username FROM user WHERE user_id = {$user_id}"; – i-bob Jan 24 '14 at 14:33
  • **By building SQL statements with outside variables, you are leaving yourself open to SQL injection attacks.** Also, any input data with single quotes in it, like a name of "O'Malley", will blow up your SQL query. Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. My site http://bobby-tables.com/php has examples to get you started, and [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has many examples in detail. – Andy Lester Jan 24 '14 at 15:27

2 Answers2

0

This should work (check notes below):

<?php
session_start();
include 'connect.php';
ini_set("sendmail_from", "********");

$user_id= $_SESSION['id'];
$sql = "SELECT username FROM user WHERE user_id = '$user_id'";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uname = mysql_fetch_array($result);

 $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);

$user_id= $_SESSION['id'];
$sql = "SELECT email FROM user WHERE user_id = '$user_id'";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);

 while($row = mysql_fetch_array($result))
 {
    $name = $uname['username'];
    $email = $uemail['email'];
$youremail = "youremail@mail.com";
$yoursubject = " Subject of the email";
    $message = "This email is to confirm you have purchased one Ellie Goulding Ticket for the O2";
    $subject = $uname['username'];

     mail( $email , $yoursubject, $message, "From:" . $youremail);

    echo "sent to"." ".$email." ".$yoursubject." ".$message."<p />";
    }

    echo "Go check your mail box:";
    include 'close.php';
    ?>
<?php echo $uemail['email'] ?><br>
<a href="ellie.php">Back</a>

Notes:

The PHP mail() Function Syntax

mail(to,subject,message,headers,parameters) 
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

I use this code to send mail try this

<html>
    <body>

    <?php
    if (isset($_REQUEST['email']))

      {

      $email = $_REQUEST['email'] ;
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail("someone@example.com", $subject,
      $message, "From:" . $email);
      echo "Thank you for using our mail form";
      }
    else

      {
      echo "<form method='post' action='mailform.php'>
      Email: <input name='email' type='text'><br>
      Subject: <input name='subject' type='text'><br>
      Message:<br>
      <textarea name='message' rows='15' cols='40'>
      </textarea><br>
      <input type='submit'>
      </form>";
      }
    ?>

    </body>
    </html>
Shinto Joseph
  • 1,081
  • 8
  • 8