PHP mailer change variable based on variable from dropdown menu - php

I am working on a mailer and form. In my form I have a dropdown menu that is populated from a database. The table in the database has 3 columns: id, facility, and email. I need the mailer side to look at the facility and change $mailto to the corresponding email address from the database. I am really new and this has had me stumped for two days.
Dropdown from form.php:
$queryData = "SELECT * FROM facilities";
$result = mysql_query($queryData);
?>
<select id="cust_Facility" name="cust_Facility">
<?php
while($row = mysql_fetch_array($result))
{
?>
<option value=<?php echo($row['id']); ?>><?php echo($row['facility']); ?></option>
<?php
}
?>
</select>
<?php
?>
Form:
$emailSubject = 'Toner Request';
$mailto = '';
$email = 'Toner Sending Service';
$facilityField = mysql_real_escape_string($_POST['cust_Facility']);
$queryEmail = "SELECT * FROM facilities WHERE ";
$result = mysql_query($queryEmail);
if (isset($facilityField)) {
echo "Facility is set";
}
$body = <<<EOD
<br><hr><br>
Customer Information<br>
<br>
Facility: $facilityField <br>
EOD;
$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Success!</h2>
</body>
</html>
When I hit submit on my form it echoes $facilityField as expected. I don't know what to add after that to make it change $mailto based on what value is in $facilityField. When I set $mailto manually I receive the email as expected.
New mail:
<?php
include('db.inc');
$emailSubject = 'Toner Request';
$mailto = '';
$email = 'Toner Sending Service';
$facilityField = mysql_real_escape_string($_POST['cust_Facility']);
$queryEmail = "SELECT email FROM facilities WHERE facility = '".$facilityField."'";
$result = mysql_fetch_assoc($queryEmail);
echo "$result\n";
if (isset($facilityField)) {
echo "Facility is set";
}
$body = <<<EOD
<br><hr><br>
Facility: $facilityField <br>
EOD;
$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
//$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Success!</h2>
</body>
</html>
I made the changes above according to my understand of Pogrindis' comment. It doesn't echo $result. It just gives me the echo "Facility is set" and "Success!". What am I still missing?
The final working code:
$queryEmail = "SELECT email FROM facilities WHERE facility = '$facilityField'";
$result = mysql_query($queryEmail);
$result = mysql_fetch_assoc($result);
$mailto = $result['email'];

Your body is not in a a string. You should be getting an error.
Your $body var should be something llike :
$body = "<<<EOD
<br><hr><br>
Customer Information<br>
<br>
Facility:". $facilityField ."<br>
EOD";
Then you will be able to use it in your mail function.
At the moment however if the facilty is not set it is still trying to send the email. So an undefined error will occur.
Cleaning if your If statement should help with this.
if (isset($facilityField)) {
echo "Facility is set";
$body = "<<<EOD
<br><hr><br>
Customer Information<br>
<br>
Facility:". $facilityField ."<br>
EOD";
$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
}
else
echo "Please set facility. "
?>
Something a bit more verbose as to what is going on would be helpful also.
Also your $mailto; at the top of the page is not defined.
If you have it on the form you should have an email input (to send to this email.) Or if it to you like a contact page it should be set.
if(isset($_POST['emailFormName']))
$mailto = $_POST['emailFormName'];
The mailto is now set from the form input called emailFormName.
As your update asked how to populate the mailto based on the facility.
You should be using
$mailto = $_POST['cust_Facility'];

Related

503 Service Unavailable when passing periods in URL in PHP

I am working on a website where you can provide contact details and submit it to a PHP page. These contact details will be sent to an email address using the native PHP mail() function. However, everything seems fine and works with the following code:
<?php
$salutation = $_GET["salutation"];
$name = $_GET["name"];
$plan = $_GET["plan"];
$desc = $_GET["desc"];
$to = "example#email.com";
$subject = $desc;
$msg = "
<html>
<body>
<p>Test body</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
?>
When you submit the form, the end of the URL bar changes to:
page.php?salutation=dhr&name=Kees+van+Voorthuizen&plan=basic&desc=Test+Description
As I said, this works properly and no errors were thrown.
But when I add an <input name="email" id="email" type="email"> to the contact details page inside the <form> tag and I submit the form using my own email address, the server returns an HTTP error 503. PHP code using the email input field:
<?php
$salutation = $_GET["salutation"];
$name = $_GET["name"];
$email = $_GET["email"]; // <--- The only thing that has changed
$plan = $_GET["plan"];
$desc = $_GET["desc"];
$to = "example#email.com";
$subject = $desc;
$msg = "
<html>
<body>
<p>Test body</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
?>
The URL now ends in:
page.php?salutation=dhr&name=Kees+van+Voorthuizen&email=my%40email.com&plan=basic&desc=Test+Description
I suspect that the error is being thrown when I include a period ('.') inside the email field. I've tried manually changing the URL bar using my%40emailcom instead of my%40email.com and it worked. Notice that I removed the period between email and com.
Any ideas?

php mail() sending two different messages on submit to different parties

I modified the following script. Everything seems to work except the person who submits the form('to2') gets an email with ('message2' - with single space lines) followed by ('message' - with double space lines).
The recipients of ('message') works as it should in that they only get ('message' - with single space lines).
MY OBJECTIVE is for ('to2') only to receive ('message2') without being followed by ('message'). I tried positioning mail('message2') string in different locations of the scripts' logic, but, I keep getting the same results or broken. Any help with logic is appreciated.
Here is the script...
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=500">
<title>EXAMPLE</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
$ip_address = $_SERVER['REMOTE_ADDR'];
$Subject = 'TEST';
$Subject2 = 'SUCCESS';
$A1 = $_POST['A1'];
$Name = $_POST['Name'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$message = $_POST['message'];
$message2 = $_POST['message2'];
// Type in your Email address to receive the mail
$to = 'me#example.com';
$to2 = $Name.' <'.$Email.'>';
if($Name == "" or $Phone == "" or $Email == "" ) {
echo 'One or more fields has not been filled out.<br>
Click on your browser back button once and try again.';
}
elseif(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
echo 'The Email address could not be validated.<br>
Click on your browser back button once and verify your Email address.';
}
else { // All checks passed
$headers = "From: ".$Name." <".$Email.">\r\n";
$headers .= "Bcc: [VENDOREMAILS]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers2 = "From: Support <me#example.com>\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=utf-8\r\n";
$message = "
Subject: $Subject<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
";
$message2 = "
Subject: $Subject2<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
"
.nl2br($message);
$sendMail = mail($to, $Subject, $message, $headers);
if($sendMail) {
echo "THANK YOU FOR YOUR SUBMISSION";
mail($to2, $Subject2, $message2, $headers2);
}
else {
echo "An error occured and the mail could not be sent.<br>
Please try again";
}
}
}
else {
header("location:example.html");
}
?>
</body>
</html>
Remove the code
.nl2br($message);
from the end of the definition of $message2. I think that should do it.

Issues developing companion php for natural language form

Forgive me, I am new at this and the different php I have tried hasn't really worked at all. I've found examples for Natural Language forms, but none with working PHP and I'm familiar with PHP when used with traditional forms, but I'm having problems putting the two together.
The general form layout goes like this:
My name is [your name].
Today, I am [whatchya gonna do?].
I'm doing this because [c'mon,why?].
It's important that I [what you said you'd do] because [the big picture].
Shoot me an email with my awesome new declaration at [your email]
Button 1 (sends email)
Button 2 (copies all text and input fields to clipboard -- not as important right now)
Button 1, I want to send a copy to myself with a hard coded email address and also send a copy to the user with the email address they've entered.
This is a little messy right now, as I am simply trying to get it to work... again, I have no included PHP because at this point -- I've confused myself so much that I don't know what to include.
<form method="post" action="todayiam.php">
<div class="naturallanguageform">
<div class="nlfinner">
<p class="line">
<span class="copy">My name is </span>
<span class="inputcontainer"><input class="textinput" name="name" value="" placeholder="[your name]">.</span>
<span class="copy">Today, I am </span>
<span class="inputcontainer"><input class="textinput" name="todayiam" value="" placeholder="[whatchya gonna do?]">.</span>
<span class="copy">I'm doing this because </span>
<span class="inputcontainer"><input class="textinput" name="why" value="" placeholder="[c'mon, why?]">.</span>
<span class="copy"> It's important that I </span>
<span class="inputcontainer"><input class="textinput" name="whatusaid" value="" placeholder="[what you said you'd do]"></span>
<span class="copy"> because </span>
<span class="inputcontainer"><input class="textinput" name="because" value="" placeholder="[the big picture]">.</span>
</p>
<span class="copy">Shoot me an email with my awesome new declaration at</span>
<span class="inputcontainer"><input class="textinput" name="email" value="" placeholder="[your email]"></span>
<p class="line">
<button class="button">Send to E-mail</button>
</p>
<p class="line">
<button class="button">Copy to post in comments</button>
</p>
</div>
</div>
</form>
Any assistance will be greatly appreciated.
Update:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['todayiam']);
// set here
$subject = "Contact form submitted!";
$to = 'email#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: index.html');
?>
Another update
I have changed $headers = "From: $email\r\n"; to $headers = "From: email#gmail.com" . "\r\n" ; to set a static from address (my support email) and the email is still identifying as from CGI Mailer. I have verified that it's not a caching issue and the correct files are being used.
<?php
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
// We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
$name = $_POST['name'];
$todayIam = $_POST['todayiam'];
$why = $_POST['why'];
$whatYouSaid = $_POST['whatusaid'];
$because = $_POST['because'];
$email = $_POST['email'];
// We define the variables for using in mail()
$to = 'email#gmail.com';
$to .= ', '.$email; // You wanted them to recieve a copy
$subject = "Contact form submitted!";
// You can put a lot more headers, check out the mail() documentation
$headers = "From: email#gmail.com" . "\r\n" ;
$headers .= "Content-type: text/html\r\n";
// Compose a $message from all the variables
$message = "My name is $name. ";
$message .= "Today, I am $todayIam.";
$message .= "I'm doing this because $why.";
$message .= "It's important that I $whatYouSaid";
$message .= "because $because.";
if (mail($to, $subject, $message, $header)) {
// Mail was successfully sent! Do something here
}
}
?>
Before you posted your answer, I was working on this script:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['todayiam']);
// set here
$subject = "Contact form submitted!";
$to = 'email#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: index.html');
?>
The $email = trim(strip_tags($_POST['email'])); field was pulling the user's email and using it for the sent from as noted in the $header... so it was working fine. With the new script, that you posted, I can't get it to work with my hard coded email OR the user's email as the FROM. Initially, I really wanted to understand what the differences were between the script, why one worked and the other didn't, but now... I'd really just like my email to be hard coded as the FROM. I'll worry about the differences later. As I said before, I really have tried to get this to work in many different forms... I am sure it's something simple that I am over looking as a novice to PHP. Thanks.
Right, so after a bit of discussion from comments, I decided to post an answer instead, giving a bit more detail where it's more readable.
Your PHP code is missing the combination of all fields into $message. This can easily be done, as you can put a variable inside a string in PHP. I'll show you how. I'm also going to show you how you can avoid undefined indexes.
<?php
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
// We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
$name = $_POST['name'];
$todayIam = $_POST['todayiam'];
$why = $_POST['why'];
$whatYouSaid = $_POST['whatusaid'];
$because = $_POST['because'];
$email = $_POST['email'];
// We define the variables for using in mail()
$to = 'email#gmail.com';
$to .= ', '.$email; // You wanted them to recieve a copy
$subject = "Contact form submitted!";
// You can put a lot more headers, check out the mail() documentation
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// Compose a $message from all the variables
$message = "My name is $name. ";
$message .= "Today, I am $todayIam.";
$message .= "I'm doing this because $why.";
$message .= "It's important that I $whatYouSaid";
$message .= "because $because.";
if (mail($to, $subject, $message, $header)) {
// Mail was sucsessfullly sent! Do something here
}
}
?>

I need senders email id in email subject

My php script is running well but i want to send senders address in email subject .Please guide me how to do it.
Please tell me what should i write in "$emailSbuject = "New Subscription from $emailFeild\r\n";"
This is my php script:
<?php
/* subject and email varialbles*/
$emailSbuject = "New Subscription from $emailFeild\r\n";
$webMaster = 'help#sample.com';
$emailSbuject2 = 'Thank you';
$client = ' $emailFeild\r\n';
/*gathering data variables*/
$emailFeild = $_POST['email'];
// admin message body
$body= <<<EOD
New subscriber is $emailFeild
EOD;
$textMessage = <<<EOD
<p style="margin-left:5px;font-family:Calibri"><img alt="" src="http://www.intaxfin.com/images/Intaxfin_logo.png"></p>
<p style="font-family:Calibri">Thank you for subscribing with us. Somebody will get back to you as soon as possible.</p>
<p style="font-size:x-small;color:#0099FF;font-family:Calibri">This e-mail was automatically sent by Administration Directory and is for your reference. Please do not reply to this e- mail address.<br>
Powered by sample</p>
EOD;
$headers = "From: $emailFeild\r\n";
$header = "From: $noreply#sample.com\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";
$success = mail($webMaster,$emailSbuject,$body,$headers);
$success1 = #mail($emailFeild,$emailSbuject2,$textMessage,$header);
/*Result*/
$theResults = <<<EOD
EOD;
echo "$theResults";
header("Location: thankyousubscribe.html");
exit;
?>
$emailFeild = $_POST['email'];
$emailSbuject2 = "New Subscription from $emailFeild";
NOTE : $client has been initiated with $emailFeild variable which is initiated after that line. Please initiate the variable first and then use it.

How to send an email to 2 persons from a table at the same time using php

I'm getting trouble with my php code.
I have a table named: prof_ales with 3 columns: cnp, evaluat and evaluator.
And a button "Send Email".
When the client press the button I want to send an email to 2 specific email adresses which are already saved into my table "prof_ales" in the column "evaluator".
Here is my code, and for now it's working, but the message is send only at the last email adress.
<input class="buttom" name="submit" id="submit" tabindex="5" value="Send Email" type="submit">
<?php
if(isset($_POST['submit'])) {
$interogare = ("SELECT * FROM prof_ales WHERE cnp='".$_SESSION['sess_user']."'");
$rezultat = mysql_query($interogare);
while ($rand = mysql_fetch_assoc($rezultat)) {
$ev= $rand['evaluat'];
$to = $rand['evaluator'];
}
$message = "test email ";
$subject = "Received from $ev";
$body = <<<EMAIL
Etc etc
$message
Bla Bla
EMAIL;
$header = "From: quabits.ro";
}
if($_POST){
mail($to, $subject, $body, $header);
echo "Sent";
}
?>
As I said I want to send to both(or more) email adresses any time when button is pressed. Tks.
As already said above you need to put the call of the mail() function inside the while()-loop. Using correct formating makes the code easier to read:
while ($rand = mysql_fetch_assoc($rezultat)) {
$ev= $rand['evaluat'];
$to = $rand['evaluator'];
$message = "test email ";
$subject = "Received from $ev";
$body = "Test";
$header = "From: quabits.ro";
if($_POST) {
mail($to, $subject, $body, $header);
}
}
You must put the code to send the email in the while loop. So it would be:
<?php
$connect = mysqli_connect("","","","");
if(isset($_POST['submit'])) {
$interogare = ("SELECT * FROM prof_ales WHERE cnp='".$_SESSION['sess_user']."'");
$rezultat = mysqli_query($interogare,$connect);
while ($rand = mysqli_fetch_assoc($rezultat)) {
$ev= $rand['evaluat'];
$to = $rand['evaluator'];
$message = "test email ";
$subject = "Received from $ev";
$body = <<<EMAIL
Etc etc
$message
Bla Bla
EMAIL;
$header = "From: quabits.ro";
}
if($_POST){
mail($to, $subject, $body, $header);
echo "Sent";
}
}
?>
The code you wrote just meant:
select the first email from database and save it into a variable.
Replace the value into the variable with the second email. Send a mail
to the address in the variable.
The improved version means:
select the first email from database and save it into a variable. Send a mail to the address in the variable.
Replace the value into the variable with the second email. Send a mail
to the address in the variable.

Categories