I have a PHP form for my event page. Right now it works perfectly to email to myself, and I can CC it to whomever I like without problems. HOWEVER, I'd like to ONLY send it to a specific person if a certain event is selected from a drop box on my form, and CC it to me. This way the organization that is holding the event can receive a copy of the completed form. I would like to also send a customized message to the person it's being sent to if possible. So for example:
If the person filling out the form selects the event, "Heart Walk," then the form results should be sent to "Bob" at "bob#email.com" with a message saying, "Hi Bob, you have received a new volunteer for your Heart Walk event. Below is a copy of the registration."
And of course I get a copy.
Can I do an if/then statement or an array for the $sendtoemail command? And then copy it to me by putting the following:
$headers .= "Cc: me#myemail.com\r\n";
What would be the best way to do this? Thanks for your help! :)
EDIT :
Here is my code:
<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
//send an email from the server TO YOUR EMAIL
$fromname = $_REQUEST['firstname'];
$fromemail = $_REQUEST['email'];
$subject = "NMVN EVENT REGISTRATION";
$message = " <b>EVENT:</b> ".$_REQUEST['pickevent'];
$message .= " <br><br> <b>First Name:</b> ".$_REQUEST['firstname'];
$message .= " <br> <b>Last Name:</b> ".$_REQUEST['lastname'];
$message .= " <br> <b>Cell Phone:</b> ".$_REQUEST['cellphone'];
$message .= " <br> <b>Alternative Phone:</b> ".$_REQUEST['altphone'];
//This is the person who is going to receive the email
$sendtoname = "Mike Gandy";
$sendtoemail = "me#myemail.com";
//Email header stuff
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromname <$fromemail>\r\n";
$headers .= "To: $sendtoname <$sendtoemail>\r\n";
$headers .= "Reply-To: $fromname <$fromemail>\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: Created by Mike Gandy";
//this next line creates and sends the email
mail($sendtoemail, $subject, $message, $headers);
?>
I was thinking about replacing this:
$sendtoname = "Mike Gandy";
$sendtoemail = "me#myemail.com";
With this:
if ($_REQUEST['pickevent'] == 'HeartWalk'))
//if "HeartWalk" is filled out, send to email
{
//send email
$sendtoname = "Bob";
$sendtoemail = "bob#email.com";
}
elseif ($_REQUEST['pickevent'] == 'RaceCure'))
//if "RaceCure" is filled out, send to email
{
//send email
$sendtoname = "Susan";
$sendtoemail = "susan#email.com";
}
elseif ($_REQUEST['pickevent'] == 'Bowling'))
//if "Bowling" is filled out, send to email
{
//send email
$sendtoname = "John";
$sendtoemail = "john#email.com";
}
But additionally I need to require that an event be selected, so the "else" would be a popup that says "Pick an event!". Does that help?
LAST EDIT :
Ok, so I got the validation working. Thank you. I am replacing this:
$sendtoname = "Mike Gandy";
$sendtoemail = "me#myemail.com";
With this:
switch(strtolower($event){
case 'HeartWalk':
$sendtoname('Bob')
$sendtoemail('bob#email.com');
break;
case 'RaceCure':
$sendtoname('Susan')
$sendtoemail('susan#email.com');
break;
case 'Bowling':
$sendtoname('John')
$sendtoemail('john#email.com');
break;
}
Adding this:
$headers .= "Cc: somebody#domain.com" . "\r\n";
And changed the beginning of my message to this:
$message = "Dear $sendtoname, <br><br>A new volunteer has registered for your "$_REQUEST['pickevent'];
$message .= " event. Below is a copy of their registration: ".$_REQUEST['blank'];
$message .= " <br><br> <b>First Name:</b> ".$_REQUEST['firstname'];
And included this in my form:
<input type="hidden" name="blank" value="">
Yes? No?
Use the select HTML tag to create the drop down menu:
<select name="event">
<option value="heartwalk">Heart Walk</option>
<option value="other">Other</option>
</select>
When handling the post, use the $_REQUEST["event"] for decision like:
switch ($_REQUEST["event"])
{
case 'heartwalk':
$sendtoemail = "heartwalk#example.com";
break;
default:
$sendtoemail = "other#example.com";
break;
}
For validation, you can start with JavaScript validation on client, when submitting the form. For example refer to: https://www.tutorialspoint.com/javascript/javascript_form_validations.htm
Of course, some validation on server side is recommended too. Depending on how robust you want your system to be, you may start with simply not doing anything to redirecting browser back to the form and displaying message on the page.
You could try a switch statement.
switch(strtolower($event){
case 'heart walk':
$sendtoemail('bob#email.com');
break;
case 'some other walk':
$sendtoemail('theotherguy#email.com');
break;
default:
$sendtoemail('info#email.com');
break;
}
Related
I want to send diifferent email to both admin and user.I dont know how to do that.
The code which i am sharing with you is sending same email to both admin and user.Please help me ..
Here is my code ;
$car = $_POST['category'];
$pick = $_POST['text1'];
$drop = $_POST['text2'];
$source = $_POST['text3'];
$email= $_POST['text4'];
$to="$email";
$subject="Web Enquiry";
$message="Hi,". "\r\n" . "\r\n" .
"You've received an email with following details, from the inquiry made at the website- mail#silvertaxi.com" ."\r\n"."\r\n".
"Car Category:"." "."$car"."\r\n".
"Source Location:"." "."$pick"."\r\n".
"Destination Location:"." "."$drop"."\r\n".
"Day and Time.:"." "."$source". "\r\n".
"Email:"." "."$email". "\r\n" ."\r\n".
"Thanks & Regards,". "\r\n" .
"Web Admin"."\r\n" ;
$headers ="From:$email\n";
$headers .= 'Cc: admin#email.com' . "\r\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
if(mail($to, $subject, $message,$headers))
{
echo "Your Message has been sent." ;
} else {
echo "";
}
You should call mail() function twice; one for user and another for admin. Remove the line
$headers .= 'Cc: admin#email.com' . "\r\n";
from your code.
Then, define different messages for user and admin as you want.
$to="$email";
$subject="Web Enquiry";
$message="....." //your message to user goes here
$to_admin = "admin#email.com";
$subject_admin = "...."; //subject for mail to admin
$message_admin = "....." //your message to admin goes here
Use the mail() function twice to send different emails.
if((mail($to, $subject, $message,$headers) && mail($to_admin, $subject_admin, $message_admin, $headers))
{
echo "Your Message has been sent." ;
} else {
echo "";
}
firstly you need to check he is user or admin. If he is user you can add a condition statement for its another for admin. Like this:
if($user_role == "user"){
//mail for user
.....
}
if($user_role == "admin"){
//mail for admin
.....
}
If you have any question feel free to ask.
I'm creating a simple mail form with checkboxes, a couple of input tabs, and a text input section. It uses PHP to retrieve the information then email it to a specific email. Right now I have it emailing to my own yahoo email just for testing. When I test the site on my hosting account jacobbuller.com/testsites/peacock/contact.php the form works perfectly and forwards the email from my generic "theski" server email. But when I upload the site to the actually live hosting account for peacockautoservice.com the contact form works - it executes and sends a ?msg=1 variable in the url - but I never receive the email in my Yahoo account...
Here's the PHP I am using to send the email.
<?php ob_start();
<?php
$required_field = array('name', 'email', 'message');
foreach($required_field as $fieldname) {
if (!isset($_POST[$fieldname])) {
$errors[] = $fieldname;
}}
if (empty($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$phone = $_POST['phone'];
$services = IsChecked('checkboxes', 'factory');
$services .= IsChecked('checkboxes', 'timing belt');
$services .= IsChecked('checkboxes', 'brakes');
$services .= IsChecked('checkboxes', 'computerized');
$services .= IsChecked('checkboxes', 'steering and suspension');
$services .= IsChecked('checkboxes', 'heating and air');
$services .= IsChecked('checkboxes', 'electrical');
$services .= IsChecked('checkboxes', 'other');
$body = "Customer:" . $name;
$body.= "Phone Number:" . $phone;
$body.= "Contact:" . $contact;
$body.= "Services:" . $services;
$body.= "Message:" . $message;
$to = "jcbbuller#yahoo.com";
$subject = "Peacock Auto Services Inquirey";
$from = $email;
$mailed = mail($to, $subject, $body, $from) or die("Error!");
}
if($mailed) {
redirect_to("../contact.php?msg=1");
}
?>
<?php
// IsChecked FUNCTION - Detemines what checkbox are/aren't checked on form.
function IsChecked($postname, $value){
if(!empty($_POST[$postname])) {
foreach($_POST[$postname] as $job) {
if ($job == $value) {
$project = " ". $value . " ";
return $project;
}
}
}
} //END IsChecked FUNCTION
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
?>
<?php ob_end_flush(); ?>
Please let me know if you see something wrong with the PHP above or if you know why their GoDaddy hosting account is not executing the email. I tried using their customer service network but they said that they can't help me with my code...
Your issue is mainly at server end. Mail function is working because of your check on it, if it had failed, it would have given you notification. So, mails are going definitely. If mail server is working properly at your production server, then check for SPAM folder at yahoo mail server. I would suggest you to ask your hosting provider to enable SPF and DKIM records because most of email providers requires sender authentication (if it is not a spam) and these records are helpful in it.
I can also see that your not using any headers, so I would suggest you to use extended headers to avoid providers identifying you as a spammer. I use below mentioned headers and my emails never go in spam on anyprovider, but again it depends on IP reputation of the server as well.
$headers .= "Reply-To: Awaraleo <email#domain.org>\r\n";
$headers .= "Return-Path: Awaraleo <email#domain.org>\r\n";
$headers .= "From: Awaraleo <email#domain.org>\r\n";
$headers .= "Organization: Awaraleo\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
and then use it like
mail($to,$subject,$message,$headers);
Here email#domain.org should be a valid email address created on the domain where this form is implemented.
Another authentic way is to use SMTP Authentication in coding.
I have a contact form for my website and am hoping to modify it so that a confirmation email is sent to the user when they click submit. Can anybody advise me on the best way to do this?
My php is pretty simple:
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" &&
$msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$EmailFrom = "someone#somewhere.co.uk";
$EmailTo = "someone#somewhere.co.uk";
$Subject = "Online contact form";
$full_name = Trim(stripslashes($_POST['full_name']));
$Phone_Num = Trim(stripslashes($_POST['Phone_Num']));
$email_addr = Trim(stripslashes($_POST['email_addr']));
}
// prepare email body text
$Body = "";
$Body .= "full_name: ";
$Body .= $full_name;
$Body .= "\n";
$Body .= "Phone_Num: ";
$Body .= $Phone_Num;
$Body .= "\n";
$Body .= "email_addr: ";
$Body .= $email_addr;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");
?>
First we get the mailto function to work with localhost and email client:
Check this link on stackoverflow:
URL link: send email to owner by collecting information provided by user through form?
Then I recommend using Swiftmailer. http://swiftmailer.org/docs/sending.html They got the best manual.
As others have already suggested the "if" statement is useless since it is achieving nothing. I think your idea was to verify if the fields that you have in your contact form are filled or not. If the fields are unavailable you should throw some error which you are not doing.
Also, if you are to send a confirmation mail to the user who clicks the submit button then the $EmailTo variable should take the email from $msg_email or $msg2_email which according to the code is not done here.
Check this simple snippet this might help you:
if ($_POST['submit']) {
if ($_POST['name1] == '')
echo "Please provide the first name";
if ($_POST['name2] == '')
echo "Please provide the last name";
if ($_POST['email1] == '')
echo "Please provide the email";
if ($_POST['email2] == '')
echo "Please provide the alternate email";
if ($_POST['message] == '')
echo "Please provide the Message";
//Send email to your inquiry mail with above details
// Confirmation mail
$message =<<<EOM
Dear $_POST['name1'],
Thank you for your inquiry.
Our executives will get back to you ASAP.
Thanks,
Sales
EOM;
$to = $_POST['email1'];
$subject = "Acknowledgement of Inquiry";
$headers = "Content-Type: text-plain";
$headers .= "From: sales#yourcompany.co.uk";
mail($to, $subject, $message, $headers);
}
Right now I have this:
$sendtoname = "John";
$sendtoemail = "john#email.com";
And this:
$message = "Dear ".$sendtoname;
$message .= ",<br><br>A new volunteer has registered for your ".$_REQUEST['pickevent'];
So that I receive a message that says,
"Dear John,
A new volunteer has registered for your event..."
But "John" does not appear; it shows up blank. How do I correct this?
EDIT :
Ok, here is my full code:
<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
//send an email from the server TO YOUR EMAIL
$fromname = $_REQUEST['firstname'];
$fromemail = $_REQUEST['email'];
$subject = "NMVN EVENT REGISTRATION";
$message = "Dear ".$sendtoname;
$message .= ",<br><br>A new volunteer has registered for your ".$_REQUEST['pickevent'];
$message .= " event. Below is a copy of their registration: ".$_REQUEST['blank'];
$message .= " <br><br><br> <b>First Name:</b> ".$_REQUEST['firstname'];
$message .= " <br> <b>Last Name:</b> ".$_REQUEST['lastname'];
$message .= " <br> <b>Cell Phone:</b> ".$_REQUEST['cellphone'];
$message .= " <br> <b>Alternative Phone:</b> ".$_REQUEST['altphone'];
//This is the person who is going to receive the email
switch ($_REQUEST["pickevent"])
{
case 'Heart Walk 4/20/13':
$sendtoname = "Bob";
$sendtoemail = "bob#email.com";
break;
case 'Bowling Fundraiser 5/4/13':
$sendtoname = "John";
$sendtoemail = "john#email.com";
break;
}
//Email header stuff
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromname <$fromemail>\r\n";
$headers .= "To: $sendtoname <$sendtoemail>\r\n";
$headers .= "Bcc: me#myemail.com" . "\r\n";
$headers .= "Reply-To: $fromname <$fromemail>\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: Created by Mike Gandy";
//this next line creates and sends the email
mail($sendtoemail, $subject, $message, $headers);
?>
The form is responding correctly otherwise, whereas it is sent to the correct person and BCC'd to me. All the other messages work fine, too. The only thing that doesn't work is that the $sendtoname doesn't appear in the message. It appears in the header just fine though.
Move your switch to before $message, as you are trying to use $sendtoname before it is declared.
//This is the person who is going to receive the email
switch ($_REQUEST["pickevent"])
{
case 'Heart Walk 4/20/13':
$sendtoname = "Bob";
$sendtoemail = "bob#email.com";
break;
case 'Bowling Fundraiser 5/4/13':
$sendtoname = "John";
$sendtoemail = "john#email.com";
break;
}
$message = "Dear ".$sendtoname;
$message .= ",<br><br>A new volunteer has registered for your ".$_REQUEST['pickevent'];
$message .= " event. Below is a copy of their registration: ".$_REQUEST['blank'];
$message .= " <br><br><br> <b>First Name:</b> ".$_REQUEST['firstname'];
$message .= " <br> <b>Last Name:</b> ".$_REQUEST['lastname'];
$message .= " <br> <b>Cell Phone:</b> ".$_REQUEST['cellphone'];
$message .= " <br> <b>Alternative Phone:</b> ".$_REQUEST['altphone'];
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your#email.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: thanks.html');
The question is, weather or not you can insert a php if statement inside the html email body or the best way to add it in there. I'm creating a detailed inventory and once submitted, it needs to be email to an email. But I don't want to email the full Inventory (including empty inputs), It should only email the fields that have something other than 0. I was thinking:
if ($armChair > 0) { echo 'Arm Chairs: ' . $_POST['armChair']; } ]
But it doesn't seem to actually work... any ideas?
This is a badly constructed question.
Yes, you can definitely do that but you need to do it in a way that is sane. We can't tell by what you posted here since you just plugged in "HTML" where the body is defined.
$body = "Hi there,\r\n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."\r\n" : "";
$body.= "some more text";
If you mean overwrite a segment of $message, yes you can do that as well using something like machine tags {INVENTORY} or the likes...
$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."\r\n", $message) : "";
which of course requires the string {INVENTORY} somewhere in your $message var