Page 1 of 1

HELP!! PHP mail not working

Posted: Sun Jan 09, 2011 10:44 am
by jeffyyy
OK, I had this working on an older version of my site and I really just copied and pasted most of the code to a new version of my site, and now my code inserts messages into the database, but it's not sending messages to the user's e-mail address. Thanks for your help!

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
 $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
 $tag = $_POST['state'] . $_POST['tag'];
?>
<?php
include("include/connect.php");
?>
<?php
$sql="INSERT INTO messages (name, tag, message, ip)
VALUES
('$_POST[name]','$tag', '$_POST[message]', '$ip')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
  ?>
  <head><link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body>

<div id="header">
    	<a href="index.php"><div id="logo"></div></a>
        <div id="content_topimage"></div>
        <div id="content">

Your message has been sent. Please <a href="main.php">click here</a> to return to your account.
</div>
<div id="content_bottomimage"></div>
<?php 
$tag = $_POST['state'] . $_POST['tag'];
$reply = $_POST['reply'];
$message = $_POST['message'];
$sender = $_POST['name'];
$count_query = mysql_query("SELECT COUNT(username) FROM users WHERE tag LIKE '".mysql_real_escape_string($tag)."'");
 $rows = mysql_fetch_array($count_query);
 
 if ($rows[0] != 0) {
        $query = mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($tag)."'"); 
        while ($array = mysql_fetch_array($query)) {
                $mail_address = $array['email'];
        }
        $to = $mail_address;
        $subject = "{$sender} 'tagged' you!";
        $message = $message;
        $headers = "From: noreply@tagtexter.com" . "\r\n" . "Reply-To: {$reply}";
       
        $mail = mail($to, $subject, $message, $headers);
        if ($mail) { echo ''; }
        //edit for production
        else {
                echo $mail_address;
        }
 }
else {
 // Display alternate message informing visitor or whoever
 // that there was no email address matching the tag
} ?>
</body>
</html>

Re: HELP!! PHP mail not working

Posted: Sun Jan 09, 2011 4:52 pm
by social_experiment
In this piece of code, what is the outcome, do you see an email address or none?

Code: Select all

<?php
$mail = mail($to, $subject, $message, $headers);
        if ($mail) { echo ''; }
        //edit for production
        else {
                echo $mail_address;
        }
 }
?>

Re: HELP!! PHP mail not working

Posted: Sun Jan 09, 2011 5:11 pm
by jeffyyy
No, there is not visible outcome, nor is there supposed to be. If I add in another echo for the email and tag, both will show up properly. Everything else on the script works, just the actual sending of mail doesnt work.

Re: HELP!! PHP mail not working

Posted: Mon Jan 10, 2011 6:22 am
by social_experiment
jeffyyy wrote:No, there is not visible outcome, nor is there supposed to be.
According to your script, on failure you should see an email address echoed to the browser. If nothing is echoed, it means your script is working. The other options is that the email address is not assigned to your '$to' variable.

Code: Select all

<?php
$mail = mail($to, $subject, $message, $headers);
        // on success nothing will echo
        if ($mail) { echo ''; }
        //edit for production
        // if your mail is NOT sent, the email address
        // should be submitted.
        else {
                echo $mail_address;
        }
 }
?>