Forum Moderators: coopster

Message Too Old, No Replies

passing variables from MySQL to php email function

passing variables to email

         

Mitch888

8:10 pm on Jan 14, 2004 (gmt 0)

10+ Year Member



I am trying to send an email using php. Below is my code. It all works fine accept for the $message variable. It does not pass the variables $first $last.

<?
$first = "Mike",
$Last = "Daniel";
$email[body] is populated from MySQL database that contains: Hello $first $last

$to = "name@domain.com";
$from = "anothername@domain.com";
$subject = "Testing";
$message = "$email[body]";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($to, $subject, $message, $headers);
?>

The email body I receive looks like this: [Hello $first $last] I need it to look like this [Hello Mike Daniel].

Can you help?

Thank you

Timotheos

9:13 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It needs to be like this.

$first = "Mike"; // Semicolon at end
$last = "Daniel"; // Variable names are case sensitive

Mitch888

9:19 pm on Jan 14, 2004 (gmt 0)

10+ Year Member



sorry for that, that was a typo on my part. Even with the semicolon and the l instead of L. the variables still dont work.

ergophobe

9:24 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just to be sure, is this also a typo or shorthand:

email[body]

or did you mean

email['body']

If you turn error reporting to a higher level on your test server, you will get an error for an undefined index. In general, PHP tries to make sense of such things, but the results are unpredictable.

Tom

Timotheos

9:27 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh ok. Well if I understand you correct the greeting "Hello $first $last" is stored in the database? If that's correct then they won't get parsed by PHP.

Just store the body in the database and tag it onto the $email variable.

$email = "Hello $first $last";
$email .= $body;

Mitch888

12:26 am on Jan 15, 2004 (gmt 0)

10+ Year Member



yes it's in the database and i opologize for my clumsiness and all the typo :) i will try your method. hope it will work

Mitch888

5:11 am on Jan 15, 2004 (gmt 0)

10+ Year Member



The "Hello $first $last" is in a MySQL database and I query it by:

$sql_mail="select * from email where id= '5'";
$result_mail = $db->EXECUTE($sql_mail);
$i_mail = mysql_fetch_array($result_mail);

then if I echo "$i_mail[bodytxt]"; i will get “Hello $first $last”

I can echo the content in the database but I cant seem to have it parsed by php and the email i recieve will contain Hello $first $last. I need the $first to be replaced with mike and $last to be replaced with Daniel so my email should NOT be Hello $first $last instead it should be Hello Mike Daniel.

The MySQL database “emails” contains two fields “id” and “body” as such:

# Table structure for table `emails`
#

CREATE TABLE emails (
id int(3) NOT NULL auto_increment,
body longtext NOT NULL,
PRIMARY KEY (id),
FULLTEXT KEY body (body)
) TYPE=MyISAM;

#
# Dumping data for table `emails`
#

INSERT INTO emails VALUES (1, ' Hello $first $last ');

My php code goes something like this:
<?
include_once “sql_connect.php";

$db = new db;
$connect=$db->CONNECT();

$sql_mail="select * from emails where id= '5'";
$result_mail = $db->EXECUTE($sql_mail);
$i_mail = mysql_fetch_array($result_mail);

$first = "Mike";
$last = "Daniel";

$to = "name@domain.com";
$from = "anothername@domain.com";
$subject = "Testing";
$message = "$email[body]";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($to, $subject, $message, $headers);
?>

the above code works and I will receive an email with “Hello $first $last”. The result I am trying to achieve is “Hello Mike Daniel”.

I hope my explanation is clear.

Thx :)

Timotheos

7:19 am on Jan 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot put variables into the database and expect PHP to parse them. I'm sure somebody else can explain this better then me.

First change your body text in your database to just say "Hello ".

Now for your message you should have
$message = $i_mail['body'] . $first . " " . $last;

Mitch888

7:46 am on Jan 15, 2004 (gmt 0)

10+ Year Member



I kind of suspected that I can't put variables in db. However, I was hoping that there was a way.

Thank you for your help as now I am certain that it can't be done so I will use a different method.

thx :)

coopster

1:53 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



eval [php.net] should get the job done for you.

eval("\$message = \"$email['body']\;")";

...not sure if I have the syntax correct there yet? Try it. If that doesn't work, assign the array value to a variable first, then run it through the eval function, I know that will work:

$message = $email['body'];
eval("\$message = \"$message\";");

Timotheos

4:32 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Coopster you're amazing! Finding stuff like that in the misc section of the manual. All in all though I think with Mitch888's application that he needs to change his design scheme.

Mitch888

8:01 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



Coopster, you are AMAAAZING. It works :) :) :) you can’t believe how happy I am. I’ve been researching all over the Internet. This is the best forum on the net.

Timotheos you are also right. I do need to change my design scheme.

Thank you guys for all the help.