Forum Moderators: coopster

Message Too Old, No Replies

$variables within a $variable

trying to combine several $variables into one

         

Terabytes

4:44 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



I have several variables such as:
$name
$address
$city
etc, etc.... taken from a form...

I'm attempting to email these variables in this way:
$to = "somone@domain.com"; $subject = "information im sending"; $email = $_REQUEST['email'] ; $phone = $_REQUEST['phone'] ; $headers = "From: $email"; $sent = mail($to, $subject, $phone, $headers) ;


I have several variables to include in my email, however I'm seeing that I'm only allowed (5) variables for the mail request. I'd like to replace the $phone variable with all my other variables and make this work out...

This leads to me to believe that I can somehow merge all my variables into one "new" variable and simply submit that to acheive my desired result.

(yes I know there's no checking going on at the moment, I simply need to get my process figured out first)

I'm a total noob at php and it's taken me about 2 weeks just to get this far so any help would be appreciated on this...

Thanks in advance!
Tera

Matthew1980

6:24 pm on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TeraBytes,

You need to build a variable and concatenate the vars into that variable (pseudo code, but the jist is there):-

$message = "My name is".$name."\n\r";
$message .= "My interests are:".$interests."\n\r";

then in the mail function:-

mail($to_address, $subject, $message, $mailheaders);

Oh, and I would avoid using $_REQUEST if I were you, fine to develop with, but not for release, too many vulnerabilities.

Hope that makes sense, if your unsure, post the code (if its not a HUGE code dump) and we can sort you out from there - we were all noob's at one point or other ;)

Cheers,
MRb

Terabytes

7:09 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



Hi! Thanks for the info!

Basically I have a few variables in a form:

name, company, phone, email, address, city, state, zip

These "post" to another page containing this code:

<?php
if(isset($_POST['state'])) {
$to = "user@domain.com"; $subject = "information I need"; $email = $_REQUEST['email'] ; $phone = $_REQUEST['phone'] ; $headers = "From: $email"; $sent = mail($to, $subject, $phone, $headers) ;
$state = strip_tags($_POST['state']);
header("Location: {$state}.htm");
exit();
}
?>

I'm redirecting to a page based upon the state the user enters from a drop-down menu and sending the info...

I'm just a tad confused on how to add my data and send it all..

Do I construct it like this?
$to = "user@domain.com";
$subject = "Information I Need";
$message = "Name:".$name."\n\r";
$message .= "Company:".$company."\n\r";
$message .= "Phone:".$phone."\n\r";
$message .= "Email:".$email."\n\r";
$message .= "Address:".$address."\n\r";
$message .= "City:".$city."\n\r";
$message .= "State:".$state."\n\r";
$headers = "From: $email";
mail($to_address, $subject, $message, $mailheaders);

I'm thinking it needs "something" before the "mail(" part

and, how do I add a <br> so that it prints correctly on the email output instead of one big line?

thanks for all your help...

Matthew1980

7:30 pm on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TeraBytes,

Well, your pretty close with the code you have posted, formatted nicely, you just need to stipulate in the headers the mime type of the email (plain text or html) then from that the \n\r will act as line breaks, and hey presto you have new lines in the email :) I am just giving simplistic examples here though, you could format it entirely in html, but then again, not everyone likes to get emails drenched in html, I consider the plain text version to be the better one.. Just my preference there though :)

Right, headers it is:-

$to = "user@domain.com";
$subject = "Information I Need";
$message = "Name:".$name."\n\r";
$message .= "Company:".$company."\n\r";
$message .= "Phone:".$phone."\n\r";
$message .= "Email:".$email."\n\r";
$message .= "Address:".$address."\n\r";
$message .= "City:".$city."\n\r";
$message .= "State:".$state."\n\r";
$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders = "From: ".$email."\n\r";

//this part is optional, you can just have the mail function without the if :)
if(mail($to_address, $subject, $message, $mailheaders)){
//success mail sent
}
else{
//error occured
}

Not to confuse you too much, but I would seriously consider sanitising the data you are putting in the email, after you have done the catch (if(isset($_POST['state']))) you can have the first line as $_POST = array_map('strip_tags', $_POST); to clean (strip out unwanted html style tags) of all the elements of the $_POST array that are set when the form is submitted (so long as this call is placed before you assign the $_POST to the vars), makes your email a little safer, there are other methods, but for this snippet, this will do nicely.

Hope that helps,

Cheers,
MRb

Terabytes

7:58 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



ok... tried your advise and here's what I have, however, I receive no email from the form now... any idea?

<?php
if(isset($_POST['state'])) {
// PHP mail function here
$to = "user@domain.com";
$subject = "information i need";
$message = "Name:".$name."\n\r";
$message .= "Company:".$company."\n\r";
$message .= "Phone:".$phone."\n\r";
$message .= "Email:".$email."\n\r";
$message .= "Address:".$address."\n\r";
$message .= "City:".$city."\n\r";
$message .= "State:".$state."\n\r";
$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders = "From: ".$email."\n\r";
mail($to_address, $subject, $message, $mailheaders);
// After mailing, do this
header("Location: {$state}.htm"); // redirect
exit(); // stop script execution
}
?>

Terabytes

8:02 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



got it! was the "$to"... thanks so much for your help....

Tera!

Matthew1980

8:35 pm on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TeraBytes

No problem, glad I could help.

Cheers,
MRb

Matthew1980

9:37 pm on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TeraBytes,

<?php
if(isset($_POST['state'])) {
// PHP mail function here
$to = "user@domain.com";
$subject = "information i need";
$message = "Name:".$name."\n\r";
$message .= "Company:".$company."\n\r";
$message .= "Phone:".$phone."\n\r";
$message .= "Email:".$email."\n\r";
$message .= "Address:".$address."\n\r";
$message .= "City:".$city."\n\r";
$message .= "State:".$state."\n\r";
$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders = "From: ".$email."\n\r";
mail($to_address, $subject, $message, $mailheaders);
// After mailing, do this
header("Location: {$state}.htm"); // redirect
exit(); // stop script execution
}
?>


Change the bolded text to this:-

$mailheaders .= "From: ".$email."\n\r";

Else the 'From' email won't be added properly, or it will overwrite the previously joined parts :)

Almost missed that - but not quite...

Cheers,
MRb

Terabytes

11:05 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



Thanks so much for everything!
I hope someday I can help somebody else with their PHP issues...

have a great weekend!
Tera

rocknbil

3:34 am on Aug 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wait a sec. :-)

You do not **need** to concatenate for scalar variables with double quotes, only with single quotes. This works just as well, makes debugging easier, and the newlines are "built in."

$message = "
Name: $name
Company: $company
Phone: $phone
Email: $email
Address: $address
City: $city
State: $state
";

But note that if you want text/html,

$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/html; charset=UTF-8\r\n";

You will need to use html.

$message = "
<p>Name: $name</p>
<p>Company: $company</p>
<p>Phone: $phone</p>
<p>Email: $email</p>
<p>Address: $address</p>
<p>City: $city</p>
<p>State: $state</p>
";

Matthew1980

9:43 am on Aug 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,
>>RocknBil:TIMTOWTDI :-p

I was just showing the easier way IMO, but your way is just as effective. I know that with single quotes you don't need to concatenate, but this is the habit of which I am in, I just find it easier to read & understand..

There you are TeraBytes, a couple of ways, you could now add a user option of text or html style email!

Happy coding,
Cheers,
MRb