Forum Moderators: coopster

Message Too Old, No Replies

Php mail() function

How to pass variables to mail

         

Paul in South Africa

7:58 pm on Feb 1, 2003 (gmt 0)

10+ Year Member



I am fairly new to php so forgive me if this is a stupid question.

I have a page that is built from a number of queries which I need to have the user email to the site owner in html format. I can pass the variable(s) that the page is made up from to the subject of the email e.g. $subject="Application for the position of $result[name]"; but I cannot get this or any other variable to display in the body of the message. I have tried everything I can think of and all I can get in the body is $result[name].

jatar_k

8:19 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Paul_in_South_Africa

how are you trying to access the variables? Is this a post to the mailing script?

I assume you have looked at this?
mail() function [php.net]

have you tried just echo'ing what the value of the vars to see if it is a passing/assignment problem as opposed to a mail function problem?

Paul in South Africa

8:33 pm on Feb 1, 2003 (gmt 0)

10+ Year Member



The code I am using goes something like:

$to .= "A Person <aperson@domain.com>";
$subject = "Application for the position of $result[name]";
$headers .= "From: $candidate[first]$candidate[last]<email@domain.com>\n";
$headers .= "X-Sender: <aperson@aperson.com>\n";
$headers .= "X-Mailer: PHP\n"; //mailer
$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
$headers .= "Return-Path: <aperson@domain.com>\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

$message = '<head>
<style type="text/css">
.normalText { font-family: Tahoma, Verdana, Arial, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
.smallText { font-family: Tahoma, Verdana, Arial, sans-serif; font-size: 10px; font-weight: normal; color: #000000}
body { margin-top: 0px; margin-bottom: 0px; background-color: #29166F}
.buttonText { font-family: Tahoma, Verdana, Arial, sans-serif; font-size: 10px; font-weight: normal; color: #FFFFFF; background-color: #29166F}
.bullets { font-family: CommonBullets; font-size: 14px; font-weight: bold; color: #D52C41}
hr { line-height: 3px; color: #29166F; width: 100%; height: 4px}
.lowercase
{text-transform: lowercase;}
</style>
</head>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="5" bgcolor="#FFFFFF">
<tr><td valign="top" class="normalText"><div align="center">
<img src="http://www.domain.com/images/Logo.jpg" alt="Company Name" width="250" height="92"></div>
<br><div align="center">
Application for the position of <b>$result[name]</b> in $resultjob[city], $result[countryname]<br>
by <b>$candidate[salutation]&nbsp;$candidate[first]&nbsp;$candidate[last]
</b><br><br></div>
and more html until closing tags
';

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

I get a prfectly formatted email with everything as I want it in the to and subject areas but in the body of the email I get $candidate[first] instead of its value.

jatar_k

8:37 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$result[name]</b> in $resultjob[city], $result[countryname]<br>
by <b>$candidate[salutation]&nbsp;$candidate[first]&nbsp;$candidate[last]

I assume this is where the problem is

are these passed from a form via post? Can you echo these values to test they are being assigned properly?

andreasfriedrich

8:39 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Single quoted strings are not subject to variable interpolation. Use either double quotes or heredoc syntax to delimit the $message string.

You might want to use either the $_POST or $_GET superglobal.

Andreas

Paul in South Africa

8:41 pm on Feb 1, 2003 (gmt 0)

10+ Year Member



No, they are values obtained from a mysql database (from a number of different tables). They do exist earlier on the page because they display as they should in a browser.

jatar_k

8:45 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you use single quotes in your var assignment it won't resolve the vars.

So this
$message = '<head>
should be
$message = "<head>

and you will need to escape quotes within the message

<added>andreas was quicker, I was trying to find a doc to point you to

andreasfriedrich

8:51 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just replace


$message = '<head>
--your html--
';

with


$message = <<<END
<head>
--your html--
END;

and everything will work ok.

my guess is this, but I am searching for supporting docs, if you use single quotes in your var assignment it won't resolve the vars.

Thatīs what Single quoted strings are not subject to variable interpolation is about. The php docs call it Variable parsing [php.net].

Andreas

jatar_k

8:53 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Single quoted strings are not subject to variable interpolation

yep, but I had the reply window open and didn't see your post ;)

I found another good one
[zend.com...]

<added>Is it just me or is the site search on php.net getting worse and worse. It seems to find nothing except specific functions when I use the search.

andreasfriedrich

8:56 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That was strange Adam because I saw your edit in post 7 just now. ;)

I shouldnīt have tried to be too quick. ;)

<added>No itīs not just you. They are trying to compete with the site search here and succeeded in finding even less ;)</added>

Paul in South Africa

9:03 pm on Feb 1, 2003 (gmt 0)

10+ Year Member



My thanks to both of you. I have got most of it working like I want it now, all that is left is to work out how to put an if($somevariable!= 0) in the middle of it and get that to work as I want it to. Once again, my sincere thanks.

PS I agree about the search, I've been looking for most of the afternoon and evening.

jatar_k

9:07 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could assign the message to the var until you hit the variable part and then do a couple of these.

if($somevariable!= 0) {
$message .= $somevariable;
} else {
do something else;
}

and then cat the rest of the message.

andreasfriedrich

9:21 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to consider removing the HTML from your code altogether and either using a template system or just include'ing it in a file, where you fill in the fields with <?=$field?>.

This will make maintaining your code and script much easier than having it all mixed up in one file.

Paul in South Africa

9:22 pm on Feb 1, 2003 (gmt 0)

10+ Year Member



Thanks. I had just worked that one out for myself and it gave me the result I expected. Sometimes trying to teach yourself does work, but I would have probably been looking at errors all night without your help.

Thanks andreasfriedrich, normally I would try to do that, and probably will once I have got it to work. All semms to be going according to plan now.