0

I am sending the birthday wishes email to all the users whose birthday is on the current date and also in the next 07 days.

But the email body is not picking up the first name of the user to whom that email is being sent.

The email body just says: Hello

Rather it should say: Hello {first name}

The code is:

<?php

    $query_birth = mysql_query("select email_address from table_name where DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");

    $count_birth = mysql_num_rows($query_birth);

    if(!empty($count_birth)) {

        while($row_birth = mysql_fetch_array($query_birth)) {

                $headers = 'From: admin@example.com' . "\r\n";
                $message = 'Hello  '.$row_birth['user_first_name'].'';
                mail($row_birth['email_address'], 'Happy Birthday', $message, $headers);
        }
    }
?>

Not sure what mistake I have done?

Chandrakant
  • 111
  • 2
  • 12

2 Answers2

1

You are only selecting the users email, not their user name as well.

Change

    $query_birth = mysql_query("select email_address from table_name where DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");

to

    $query_birth = mysql_query("select email_address, user_first_name from table_name where DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");
ioneyed
  • 1,052
  • 7
  • 12
-1

You should select all to make it easier so you wont run into the same error

$query_birth = mysql_query("SELECT * FROM table_name WHERE DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");
Dermot
  • 17
  • 6
  • You may not run into the error but depending on how many columns are in this table you could be grabbing unnecessary data that could cause performance issues on the database. Ideally you should never do a `SELECT *` as it is more performant to declare what fields you want (even if you select them all) as the database wont have to look up all the column references before it collates the data and returns it. see - http://stackoverflow.com/questions/3639861/why-is-select-considered-harmful – ioneyed Nov 19 '16 at 16:02
  • the user is parsing it as if they selected all, that is why this would be easier for them. there was no need to downvote this to upvote your answer :/ It hurts my reputation especially when the answer is right – Dermot Nov 19 '16 at 21:12