Link to home
Start Free TrialLog in
Avatar of booneball
booneball

asked on

PHP Mail

For some reason my mail function i have written isnt working. It doesnt send an email at all.

$subject = "Happy Birthday from Hayward Baker";
      $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
      $header = "From:" . "James.Lowley@hayward-baker.com" . " \r\n";
      //echo $name;
      mail ($email, $subject, $message, $header);
      echo "Mail Sent.";
Avatar of EMB01
EMB01
Flag of United States of America image

I don't see the $email variable being declared.  Also, change your code to this and tell me what happens:

$email = "me@mywebsite.com";
$subject = "Happy Birthday from Hayward Baker";
      $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
      $header = "From:" . "James.Lowley@hayward-baker.com" . " \r\n";
      //echo $name;
      if (mail ($email, $subject, $message, $header)) {
            echo 'mail sent';
      } else {
            echo 'mail FAILED';
      }
apart from missing $email
are there any errors on screen
Avatar of booneball
booneball

ASKER

I get a blank page if i change it as requested.
As for the version I originally posted, No other errors just not sending the email.
If you get a blank page it's because you have a syntax error.  Please send me your whole code so I can check for errors.
<?PHP
include_once("../info_db/conn.php");

 function birthday ()
  {
    list($year,$month,$day) = explode("-");
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }
    echo birthday($row['dob']);
  //}


//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

            
            //Going through all the bdates
            while($row = mysql_fetch_array($sql)){
            
            // Run the function to grab the month and day only
            $raw_bdates =($row['dob']);
            $bdates_month = substr ($raw_bdates, 8, 2);
            $bdates_year = substr ($raw_bdates, 2, 2);
            $bdates = $bdates_year.$bdates_month;
            $name=($row['name']);//your name field supposed as person_name
            
            

            
            // See what today is
            $now = date("Ymd");
            
            // Only grab the month and day from today
            $now = substr($now, 4, 7);
            //echo $bdates;
            
            
            // Seeing if there is a match then email them a birthday message
            //echo $now, $bdates."<br>";
            if($now == $bdates) {
                  echo "Happy Birthday to $name!";
      //echo $now;
      $email = $row['email'] .","; // to be checked
      //echo $email;
      
      // Preparing for email
      $subject = "Happy Birthday from Hayward Baker";
      $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
      $header = "From:" . "James.Lowley@hayward-baker.com";
      //echo $name;
      mail ($email, $subject, $message, $header);
      echo "Mail Sent.";

}
            }


//$name = $row['name'];
                   //}
    //} else {
         // echo "We cannot find that product";
            //exit();
   // }
//}

      
      ?>
   
 
It looks like you have an extra "}"  Please try this:

<?PHP
include_once("../info_db/conn.php");

 function birthday ()
  {
    list($year,$month,$day) = explode("-");
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }
    echo birthday($row['dob']);
  //}


//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

           
            //Going through all the bdates
            while($row = mysql_fetch_array($sql)){
           
            // Run the function to grab the month and day only
            $raw_bdates =($row['dob']);
            $bdates_month = substr ($raw_bdates, 8, 2);
            $bdates_year = substr ($raw_bdates, 2, 2);
            $bdates = $bdates_year.$bdates_month;
            $name=($row['name']);//your name field supposed as person_name
           
           

           
            // See what today is
            $now = date("Ymd");
           
            // Only grab the month and day from today
            $now = substr($now, 4, 7);
            //echo $bdates;
           
           
            // Seeing if there is a match then email them a birthday message
            //echo $now, $bdates."<br>";
            if($now == $bdates) {
                  echo "Happy Birthday to $name!";
      //echo $now;
      $email = $row['email'] .","; // to be checked
      //echo $email;
     
      // Preparing for email
      $subject = "Happy Birthday from Hayward Baker";
      $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
      $header = "From:" . "James.Lowley@hayward-baker.com";
      //echo $name;
      mail ($email, $subject, $message, $header);
      echo "Mail Sent.";

}


//$name = $row['name'];
                   //}
    //} else {
         // echo "We cannot find that product";
            //exit();
   // }
//}

     
      ?>
that gives me a blank page again
ok so if i change the email to a correct email address
$email = $row['email'] .","; // to be checked
to $email = "Michael.Hampton@hayward-baker.com"; Then still no email is sent.

So i dont think its the database connection/field thats causing it
Hi Booneball,
why are you adding the "," behind the email address you are sending too, I'd remove the comma.

further, I'd do a bit debugging:

add "  echo "test1";   " on different places in your code and see where it stops giving output (only put 1 at a time for easier debugging)

>>  add "  echo "test1";   " on different places in your code and see where it stops giving output (only put 1 at a time for easier debugging)

Yes, this is how I test apps, too.  Change your code to this:


<?PHP
include_once("../info_db/conn.php");
 echo "test1";
 function birthday ()
  {
    list($year,$month,$day) = explode("-");
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }
    echo birthday($row['dob']);
  //}
 echo "test2";

//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

            echo "test3";
            //Going through all the bdates
            while($row = mysql_fetch_array($sql)){
           
            // Run the function to grab the month and day only
            $raw_bdates =($row['dob']);
            $bdates_month = substr ($raw_bdates, 8, 2);
            $bdates_year = substr ($raw_bdates, 2, 2);
            $bdates = $bdates_year.$bdates_month;
            $name=($row['name']);//your name field supposed as person_name
           
            echo "test4";

           
            // See what today is
            $now = date("Ymd");
           
            // Only grab the month and day from today
            $now = substr($now, 4, 7);
            //echo $bdates;
           
           
            // Seeing if there is a match then email them a birthday message
            //echo $now, $bdates."<br>";
            if($now == $bdates) {
                  echo "Happy Birthday to $name!";
      //echo $now;
      $email = $row['email'] .","; // to be checked
      //echo $email;
      echo "test5";
      // Preparing for email
      $subject = "Happy Birthday from Hayward Baker";
      $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
      $header = "From:" . "James.Lowley@hayward-baker.com";
      //echo $name;
      mail ($email, $subject, $message, $header);
      echo "Mail Sent.";

}
 echo "test6";

//$name = $row['name'];
                   //}
    //} else {
         // echo "We cannot find that product";
            //exit();
   // }
//}
 echo "test7";
     
      ?>

Where you last see the "test#" echoed on screen will be where your error is near.
test12011
test2test3test4Happy Birthday to James Lowley!test5Mail Sent.test6test7test4test6test7test4test6test7test4test6test7
Okay, good.  So, the mail was sent.  Check your mail server (inbox or spam folder) because the mail should have been sent.  The comma that webtronik pointed out would also have been problematic.
Also, if you're not getting any emails, Change your code to this so we can see exactly what/ where it's trying to send:


<?PHP
include_once("../info_db/conn.php");

 function birthday ()
  {
    list($year,$month,$day) = explode("-");
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }
    echo birthday($row['dob']);
  //}


//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

           
            //Going through all the bdates
            while($row = mysql_fetch_array($sql)){
           
            // Run the function to grab the month and day only
            $raw_bdates =($row['dob']);
            $bdates_month = substr ($raw_bdates, 8, 2);
            $bdates_year = substr ($raw_bdates, 2, 2);
            $bdates = $bdates_year.$bdates_month;
            $name=($row['name']);//your name field supposed as person_name
           
           

           
            // See what today is
            $now = date("Ymd");
           
            // Only grab the month and day from today
            $now = substr($now, 4, 7);
            //echo $bdates;
           
           
            // Seeing if there is a match then email them a birthday message
            //echo $now, $bdates."<br>";
            if($now == $bdates) {
                  echo "Happy Birthday to $name!";
      //echo $now;
      $email = $row['email'] .","; // to be checked
      //echo $email;
     
      // Preparing for email
      $subject = "Happy Birthday from Hayward Baker";
      $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
      $header = "From:" . "James.Lowley@hayward-baker.com";
      //echo $name;
echo 'email:'.$email;
echo 'subject:'.$subject;
echo 'message:'.$message;
echo 'header:'.$header;
die();
      mail ($email, $subject, $message, $header);
      echo "Mail Sent.";

}


//$name = $row['name'];
                   //}
    //} else {
         // echo "We cannot find that product";
            //exit();
   // }
//}

     
      ?>

Open in new window

Let's start with the basics: I get a blank page...

That probably means you have a PHP parse error and you have error reporting turned off (a fatal combination for a programmer).  Add these two instructions to the top of ALL OF YOUR SCRIPTS.  Joomla is not exactly your friend when it comes to finding PHP errors, so we have to do a little extra work.

error_reporting(E_ALL);
ini_set('display_errors', TRUE);


Next, please use the code snippet feature when you post code here at EE.  It gives us line numbers and unispace fonts, so the code is easier to read and discuss in this online forum.

Third, what you are dealing with here is called a "data-dependent" issue.  The programming operates upon variable data in predictable ways, but the content of the data can cause unpredictable results.  Whenever you are confronted with something like that, you want to visualize the data.  The best tool for that is a built-in PHP function, shown here.  The output is much easier to read if you echo "<pre>" before using var_dump().
http://php.net/manual/en/function.var-dump.php

You might want to consider getting a foundation in PHP (aside from Joomla) and these are two good learning resources to help you get started.
http://php.net/tut.php
http://www.sitepoint.com/books/phpmysql4/
test1
Notice: Undefined variable: dob in C:\Inetpub\joomla\php\systems\birthdays.php on line 21

Warning: Wrong parameter count for explode() in C:\Inetpub\joomla\php\systems\birthdays.php on line 12

Notice: Undefined variable: row in C:\Inetpub\joomla\php\systems\birthdays.php on line 16
2011test2
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Inetpub\joomla\php\systems\birthdays.php on line 28
test3test4Happy Birthday to James Lowley!test5email:James.Lowley@hayward-baker.comsubject:Happy Birthday from Hayward Bakermessage:HAVE A GREAT BIRTHDAYJames Lowleyheader:From:James.Lowley@hayward-baker.com
There are many errors in your code, give me a sec and I'll reprogram it to make it work how I think you want.
I cleaned up your code and commented out some lines that it didn't seem like you were using.  Try this and tell me what it does:


<?PHP
include_once("../info_db/conn.php");

/*  DOESN'T LOOK LIKE YOU'RE USING THIS SO I COMMENTED IT OUT
 function birthday ()
  {
    list($year,$month,$day) = explode("-");
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }
    echo birthday($row['dob']);
*/

//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
    
//Going through all the bdates
while($row = mysql_fetch_array($sql)){
   
	// Run the function to grab the month and day only
	$raw_bdates =($row['dob']);
	$bdates_month = substr ($raw_bdates, 8, 2);
	$bdates_year = substr ($raw_bdates, 2, 2);
	$bdates = $bdates_year.$bdates_month;
	$name=($row['name']);//your name field supposed as person_name
   
	// See what today is
	$now = date("Ymd");
   
	// Only grab the month and day from today
	$now = substr($now, 4, 7);
	//echo $bdates;
   
	// Seeing if there is a match then email them a birthday message
	//echo $now, $bdates."<br>";
	if($now == $bdates) {
		  echo "Happy Birthday to $name!";
		  //echo $now;
		  $email = $row['email'] .","; // to be checked
		  //echo $email;
		 
		  // Preparing for email
		  $subject = "Happy Birthday from Hayward Baker";
		  $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
		  $header = "From:" . "James.Lowley@hayward-baker.com";
		  //echo $name;
		  
		echo 'email:'.$email;
		echo 'subject:'.$subject;
		echo 'message:'.$message;
		echo 'header:'.$header;
		die();
		
		  mail ($email, $subject, $message, $header);
		  echo "Mail Sent.";
	}
}
?>

Open in new window

still no difference email still isnt being sent. Do i assume that its an error on the server?
No, the email isn't supposed to be sent yet.  I'm killing it before that happens.  I'm trying to verify the data being sent to the php mail() function.  If you want to try to send it, just remove the die() statement like so:
<?PHP
include_once("../info_db/conn.php");

/*  DOESN'T LOOK LIKE YOU'RE USING THIS SO I COMMENTED IT OUT
 function birthday ()
  {
    list($year,$month,$day) = explode("-");
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }
    echo birthday($row['dob']);
*/

//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
    
//Going through all the bdates
while($row = mysql_fetch_array($sql)){
   
	// Run the function to grab the month and day only
	$raw_bdates =($row['dob']);
	$bdates_month = substr ($raw_bdates, 8, 2);
	$bdates_year = substr ($raw_bdates, 2, 2);
	$bdates = $bdates_year.$bdates_month;
	$name=($row['name']);//your name field supposed as person_name
   
	// See what today is
	$now = date("Ymd");
   
	// Only grab the month and day from today
	$now = substr($now, 4, 7);
	//echo $bdates;
   
	// Seeing if there is a match then email them a birthday message
	//echo $now, $bdates."<br>";
	if($now == $bdates) {
		  echo "Happy Birthday to $name!";
		  //echo $now;
		  $email = $row['email'] .","; // to be checked
		  //echo $email;
		 
		  // Preparing for email
		  $subject = "Happy Birthday from Hayward Baker";
		  $message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
		  $header = "From:" . "James.Lowley@hayward-baker.com";
		  //echo $name;
		  
		echo 'email:'.$email.'<br />';
		echo 'subject:'.$subject.'<br />';
		echo 'message:'.$message.'<br />';
		echo 'header:'.$header.'<br />';
		
		  mail ($email, $subject, $message, $header);
		  echo "Mail Sent.";
	}
}
?>

Open in new window

Warning: mail() [function.mail]: Could not execute mail delivery program 'C:\php\sendmail\sendmail.exe -t' in C:\Inetpub\joomla\php\systems\birthdays.php on line 64
Mail Sent.
Fatal error: Cannot redeclare birthday() (previously declared in C:\Inetpub\joomla\php\systems\birthdays.php:27) in C:\Inetpub\joomla\php\systems\birthdays.php on line 25
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks like Rays got this, I'm gonna back off.  If you can't reach a solution with Ray I'll step back in; I'll be monitoring it.  Ray will help you, though.
Thanks Ray!