Current location: Hot Scripts Forums » Programming Languages » PHP » (Not) Processing variables in mail message


(Not) Processing variables in mail message

Reply
  #1 (permalink)  
Old 04-20-04, 09:50 AM
rfo00457 rfo00457 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Netherlands
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
(Not) Processing variables in mail message

I have a script that e-mails a message that comes out of a (Mysql) database.

It looks like about this (without code not important to this problem):

$dfg="FF";
$vmailtxtnr=310;
$sql5= "select * from mailtekst
where mailtxtnr='$vmailtxtnr'";
$res5 = mysql_query($sql5);
if ($row5 = mysql_fetch_array($res5)) {
do {
$vmailsubject=$row5["mailsubject"];
$vmailtxt=$row5["mailtxt"];
}
while ($row5 = mysql_fetch_array($res5));
}
else {
print "";
}

$to="$a"
$subject="$b $vmailsubject";
$message="$vmailtxt";
$headers="$e";

mail($to, $subject, $message, $headers);

Suppose that in the field mailtxt in the result contains:

"jhagsdjhgasd $dfg kjhskfjhasd"
or
"jhagsdjhgasd".$dfg."kjhskfjhasd"

the variable $dfg does not get processed in the mail that is sent, but is printed like it is stored in the database. The result in the mail is:

jhagsdjhgasd $dfg kjhskfjhasd
jhagsdjhgasd".$dfg."kjhskfjhasd

instead of

jhagsdjhgasd FF kjhskfjhasd

Does anyone know a way to have text and variables in the database that gets processed when the mail gets sent by the script?

Thanx,

Rob
Reply With Quote
  #2 (permalink)  
Old 04-20-04, 11:06 AM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
From my understanding (I think i read it correctly)...

Remove the quotes from your variables. ($to $subject $message $headers)
Add a semi colon to at the end of the line with the $to variable declared.
Also you need to join the 2 variables $b & $vmailsubject as $b . " " . $vmailsubject if you want a space between them.

Hopefully I understood that correctly.
Reply With Quote
  #3 (permalink)  
Old 04-20-04, 04:28 PM
rfo00457 rfo00457 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Netherlands
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thanx for your quick response!

The semi colon at the end of the line was a slip of my fingers and keyboard.

When I remove the quotes, the result is the same, the variables are not processed in the message.

I changed the following:

$message=$vmailtxt;

And the content of $vmailtxt is:

jhagsdjhgasd $dfg kjhskfjhasd

or

"jhagsdjhgasd".$dfg."kjhskfjhasd"

the result in the sent message is:

jhagsdjhgasd $dfg kjhskfjhasd

or

"jhagsdjhgasd".$dfg."kjhskfjhasd"

while the variable $dfg contains FF

Maybe I am not clear about the problem.

The field in the database contains plain text and variables. I would like to have the variables processed when they are put in the $message variable.
Reply With Quote
  #4 (permalink)  
Old 04-20-04, 04:46 PM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
It seems like you're missing a step or two. Are you showing the whole code or just a snippet? I'm not seeing where $dfg is added to $message at all.

Is this going to be used to send multiple emails or just one per each run?

I have to head out now, I'll check back later.
Reply With Quote
  #5 (permalink)  
Old 04-20-04, 05:02 PM
rfo00457 rfo00457 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Netherlands
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
It is a piece of the script where the problem is.

$dfg is declared in the first line.

I would like to search the content of a field. This field contains AND plain text AND variables like $dfg.

I would like to use it to send 1 mail at a time, but the content of the mail could be different depending on the values that I give to the variables at the time of sending.

*****SCRIPT*****

$dfg="FF";
$vmailtxtnr=310;
$sql5= "select * from mailtekst
where mailtxtnr='$vmailtxtnr'";
$res5 = mysql_query($sql5);
if ($row5 = mysql_fetch_array($res5)) {
do {
$vmailsubject=$row5["mailsubject"];
$vmailtxt=$row5["mailtxt"];
}
while ($row5 = mysql_fetch_array($res5));
}
else {
print "";
}

$to="$a";
$subject="$b $vmailsubject";
$message="$vmailtxt";
$headers="$e";

mail($to, $subject, $message, $headers);
*****END SCRIPT*****

Maybe it helps when I post the complete script:

TEST.PHP

<?php
include ("dbconnect.php");
include ("variabelenvoormail.php");
include ("mailtest.php");
?>

VARIABELENVOORMAIL.PHP

<?php
include("dbconnect.php");

$sql= "select klantnummer from stagehis
where inzetnummer='$vinzetnummer'";
$res = mysql_query($sql);
if ($row = mysql_fetch_array($res)) {
do {
$vklantnummer=$row["klantnummer"];
$vdatum=$row["datum"];
$vtijd=$row["tijd"];
}
while ($row = mysql_fetch_array($res));
}
else {
print "";
}

$sql2= "select * from stagehis
left join klanten
on stagehis.klantnummer = klanten.klantnummer
where inzetnummer='$vinzetnummer'";
$res2 = mysql_query($sql2);
if ($row2 = mysql_fetch_array($res2)) {
do {
$vemailontvanger=$row2["emailfact"];
$vklantnaam=$row2["naamkl"];
$vklantadres=$row2["adreskl"];
$vklantpc=$row2["pckl"];
$vklantwpl=$row2["woonplkl"];
$vfactnaam=$row2["naamfact"];
$vfactadres=$row2["adresfact"];
$vfactpc=$row2["pcfact"];
$vfactwpl=$row2["woonplfact"];
$vemailkl=$row2["emailkl"];
$vtelefoonkl=$row2["telefoonkl"];
$vmobnumkl=$row2["mobnumkl"];
$vleeftijdkl=$row2["leeftijdkl"];
$vemailfact=$row2["emailfact"];
$vtelefoonfact=$row2["telefoonfactfact"];
}
while ($row2 = mysql_fetch_array($res2));
}
else {
print "";
}

$sql3= "select * from stageinfo
left join stagehis
on stageinfo.functienummer = stagehis.functienummer
where inzetnummer='$vinzetnummer'";
$res3 = mysql_query($sql3);
if ($row3 = mysql_fetch_array($res3)) {
do {
$vfunctie=$row3["functie"];
$vfunctienummer=$row3["functienummer"];
}
while ($row3 = mysql_fetch_array($res3));
}
else {
print "";
}

$sql4= "select * from stageinfo
left join bedrijf
on stageinfo.bedrijfsnummer = bedrijf.bedrijfsnummer
where functienummer='$vfunctienummer'";
$res4 = mysql_query($sql4);
if ($row4 = mysql_fetch_array($res4)) {
do {
$vbedrijfsnaam=$row4["bedrijfsnaam"];
$vtelnumalg=$row4["telnumalg"];
$vbezoekstraat=$row4["bezoekstraat"];
$vbezoekpc=$row4["bezoekpc"];
$vbezoekplaats=$row4["bezoekplaats"];
$vbezoekland=$row4["bezoekland"];
$vnaamcp=$row4["naamcp"];
$vfotogr=$row4["fotogr"];
$vlunch=$row4["lunch"];
$vgeheim=$row4["geheim"];
$vemailcp=$row4["emailcp"];
}
while ($row4 = mysql_fetch_array($res4));
}
else {
print "";
}

$sql5= "select * from mailtekst
where mailtxtnr='$vmailtxtnr'";
$res5 = mysql_query($sql5);
if ($row5 = mysql_fetch_array($res5)) {
do {
$vmailsubject=$row5["mailsubject"];
$vmailtxt=$row5["mailtxt"];
}
while ($row5 = mysql_fetch_array($res5));
}
else {
print "";
}


?>

MAILTEST.PHP

<?php

/* subject */
$subject = "$vinzetnummer $vmailsubject";

$message= $vmailtxt;

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

$headers .= "To: $vemailontvanger, \r\n";
$headers .= "From: www.xxx.nl <info@xxx.nl>\r\n";
$headers .= "Cc: \r\n";
$headers .= "Bcc: info@xxx.nl\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>

I would like to execute test.php?vinzetnummer=1&vmailtxtnr=1

In the table mailtekst is a record which contains:

mailtxtnr=1
mailtxt=Dear $vklantnaam
mailsubject=Confirmation

So that is processes like Dear Mr. Johnson, if $vklantnaam contains "Mr. Johnson"

And that does not happen!
Reply With Quote
  #6 (permalink)  
Old 04-20-04, 08:40 PM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
So are you doing this inside a single script file or not? I see multiple names listed. I wasn't sure if that was just for reference.

You should consolidate your script. I think that could resolve the variable issues (make it one script instead of three by moving the code into a single file). Have you tried it that way.

Aside from that I can't see anything off hand that looks troublesome. Let me know if that doesn't work for you and I'll have another look at it.
Reply With Quote
  #7 (permalink)  
Old 04-20-04, 10:00 PM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
I think I know where you are comming from with the problem..

You have this stored in the database:
"Hello $name, This is a message about $topic"

You want to read this from the database and expand the variables, as you are getting literally the above still sent in the emails.

If this is the case, what you want is something like:

PHP Code:

$txt 'Hello $name, I am writing to you about $subject'

// Really the above would be coming from the database.
// I am deliberately using single quotes in the above to get literal $ signs.

$name 'John';
$subject 'Something';

$txt preg_replace("/\$(\w+)/i""${$1}",$txt);

print 
$txt
The above code searches for a litteral $ sign, followed by letters, numbers, or underscores (\w means match any of those), and replaces it with contents of the real variable with that name. (e.g. '$name' from the database, will be replaced with whatever you set $name as in your script)

If I'm way off in what I think you were asking, let me know.. also if the above code doesn't work, tell me.. (I didn't have time to test it)
Reply With Quote
  #8 (permalink)  
Old 04-21-04, 06:32 AM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
Correction to the code (after testing):

PHP Code:

<?

$txt 
'Hello $name, I am writing to you about $subject';

// Really the above would be coming from the database.
// I am deliberately using single quotes in the above to get literal $ signs.

$name 'John';
$subject 'Something';

$txt preg_replace("/\\$(\w+)/e""\${\$1}"$txt);

print 
$txt;
?>
Reply With Quote
  #9 (permalink)  
Old 04-21-04, 07:02 AM
rfo00457 rfo00457 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Netherlands
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I have tried this in my script, but it is not working. It still prints the name of the variable instead of its content.

Is it possible that the content of $message is seen as totally HTML without processing? I have tried also to put <? $xx ?> in the field, but no result either.

But you understand exactly what I mean.

Last edited by rfo00457; 04-21-04 at 07:04 AM.
Reply With Quote
  #10 (permalink)  
Old 04-21-04, 07:24 AM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rfo00457
I have tried this in my script, but it is not working. It still prints the name of the variable instead of its content.

Is it possible that the content of $message is seen as totally HTML without processing? I have tried also to put <? $xx ?> in the field, but no result either.

But you understand exactly what I mean.
You tried my example in your script ? Can you post the modified code with that included..
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
formmail problem gscraper Perl 12 08-27-04 03:06 AM
From header in mail() <?Wille?> PHP 0 02-03-04 02:02 PM
adding more variables to the mail() result spinicrus PHP 6 08-28-03 04:55 PM
Problem with CDONTS javadude2003 ASP 23 08-07-03 11:09 PM


All times are GMT -5. The time now is 10:27 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.