开发者

how to print variables coming from an sql db when using the mail function PHP

I have a mail function that sends out an email with content from an sql db.

The message that will be included in the email will be taken from the sql db. The message in the sql db has variables in it.....

example:

In the database:

Hey $name, $owner_name here

Everything works fine, except i cant get PHP to print out that var when it sends the email.

So the final email that goes out is:

Hey $name, $owner_name here

When it should be:

Hey BOB, Johnny here

Here's the mail function code;

get_email_settings($db_host, $db_user, $db_pswrd, $db_name, $email, $pro_msg,$pro_subject);



mail($email_usr, $pro_subject, $pro_msg, "From: $my_email"); 

Now I've also tried: (NOTE: the double quotes)

mail($email_usr, "$pro_subject", "$pro_msg", "From: $my_email");

And I've also tried inserting the content into the DB with double quotes so: "Hey $name, $owner_name here" ins开发者_高级运维tead of Hey $name, $owner_name here...

Both do the same thing...

Any Ideas?


The reason it isn't is because PHP isn't evaluating those -- it's not being parsed, just retrieved from the database. You should probably use placeholders rather than putting actual variables in there though. For example, use Hey [NAME], [OWNER_NAME] here and then you can just do a replace on those to get what you want.

If you really want to do it that way, take a look at eval()


have you tried this

$pro_msg_with_vars = "";
eval('$pro_msg_with_vars = $pro_msg;');

mail($email_usr, $pro_subject, $pro_msg_with_vars, "From: $my_email");


You're probably looking for eval:

http://php.net/manual/en/function.eval.php

I would do something like that: keep in db text "Hello %nickname%, %me% here" and then with php use str_replace(...);


Can you please give code for the actual line that is not working?

If I understand you correctly, you are inserting this message as a string into a database, is this correct? If so, then the problem lies where you save the data to the database.

Does the actual data in the database say BOB, or $name?

Try updating the db with:

$query = "INSERT INTO outbound_messages (id, message)VALUES(1, 'Hi {$name}, {$owner_name} here, yada yada');
mysql_query($query);


PHP won't recognize those as variables, so you're going to want to manually parse it.

The code is fairly simple, but long enough that I'll just summarize instead of write it.

Essentially, you're going to want to parse through your string and look for anything that has your unique identifier in it (yours is "$"). Each time you find one, extract that string, and compare it to an array.

So if you have in your database: "Hello, $name. I heard you just turned $age last week. Happy Birthday, dude!"

you're going to want to build an array (I assume this will also be generated from a database) that looks like this.

$db_vars = array( "name" => "Joe Bob", "age" => 97 );

That way you can just replace your identified strings with $db_vars[$extracted_string];


OK, I got it working.

For anyone else who has this issue, and wants a slightly more clearer answer, here it is:

All I done was a multiple str_replace using arrays.

I'm feeling generous, so I'll go into some detail for you...

example of an str_replace:

$my_text = "my name is [NAME] and my email is [EMAIL]"

$find = array('[NAME]',[EMAIL]);

$and_replace = array('BOB', 'BOB@BOB.com');

// then I just run the str_replace into a variable:

$final_text = str_replace($find, $and_replace, $my_text);

and the final text would be "my name is BOB and my email is BOB@BOB.com"

so as you can see, str_replace searched in the variable $my_text and looked for [NAME] and [EMAIL]. It then replaced them with either BOB (for [NAME]) and BOB@BOB.com (for [EMAIL]). BUT when you do this, make sure the replace variable ($and_replace) has the data in the right order... for example, if i did this $and_replace = array('BOB@BOB.com','BOB'); instead of this $and_replace = array('BOB', 'BOB@BOB.com'); then it would replace [NAME] with BOB@BOB.com and [EMAIL] with BOB. So make sure you get the right order....

So in my case, using str_replace with a string coming from an SQL db:

It's piratically identical to the example i wrote above, instead you just don't create the string variable ($my_text). You just use the variable that has the db content in it (in my case $pro_msg)

so:

$find = array('$name','$owner_name');//<< As some of the other guys said, you should use something like [NAME] instead of $name....

$and_replace = array("$name", "$owner"); //<<<**NOTE: IF REPLACING WITH OTHER VARIABLES, USE DOUBLE QUOTES**


$final_text = str_replace($find, $and_replace, $pro_msg);

So it went into $pro_msg (which contains a string from my sql db) and searched for $name and $owner_name and then replaced it with the appropriate...

ready for mailing...

hope that helped someone out there...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜