HELP!! PHP mail not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

HELP!! PHP mail not working

Post 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>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HELP!! PHP mail not working

Post 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;
        }
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: HELP!! PHP mail not working

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HELP!! PHP mail not working

Post 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;
        }
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply