-2

I have to send an email, the email can be formatted and stored in database. But the problem is I have to use some variable in the email to make it a dynamic email for each user.

INSERT INTO email (greeting) VALUES ( "Hello $name"); 

$coding = mysql_query("SELECT * FROM email ") or die(mysql_error()); 
$email = mysql_fetch_array( $coding ); 

echo $email['greeting']; 

it should echo Hello PERSONS_NAME; like Hello John, But it echo Hello $name, as it is..

Does not work, Any help ???

m82amjad
  • 248
  • 2
  • 9
  • 1
    Your title says something your body says something else. – Rahil Wazir Jul 25 '14 at 17:18
  • 2
    It seems like you want an email template. Typically you'd do something like replacing a placeholder value in your string with the "live" value. So `echo str_replace('$name', $name, $email['greeting'])`. Though you don't have to use $name as the placeholder; just any string that will be unique in your email. You're not actually storing "PHP code" in the database. Is that the kind of thing you're after? – Matt Gibson Jul 25 '14 at 17:20
  • 1
    Also: please don't use the elderly mysql extension. [It's deprecated](http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli). – Matt Gibson Jul 25 '14 at 17:20
  • possible duplicate of [Send email with a template using php](http://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php) – Matt Gibson Jul 25 '14 at 17:22
  • @MattGibson It's not really a duplicate of that question because it focuses on storing the template in the database, not in code. (Arguments aside about the "right place" to put such things.) – Moshe Katz Jul 25 '14 at 17:44
  • @mosheKatz Thanks for clarifying, It is not a duplicate question. – m82amjad Jul 27 '14 at 03:44

1 Answers1

4

You will probably be better off using placeholders and then swap out the placeholder with dynamic text when sending the email:

INSERT INTO email (greeting) VALUES ( "Hello %%NAME%%");

$coding = mysql_query("SELECT * FROM email ") or die(mysql_error()); 
$email = mysql_fetch_array( $coding ); 

$email['greeting'] = str_replace('%%NAME%%', $name, $email['greeting']);

echo $email['greeting'];
cOle2
  • 4,725
  • 1
  • 24
  • 26