Getting trouble on adding multiple checkbox value within mail body due to my low knowledge in PHP.
I know that it possible to show/echo checkbox by array with foreach loop but i don't know to echo it within mail body. I want to echo it within $message.
HTML Code sample-
<input type="checkbox" name="color[]" value="Value1"> Title1
<input type="checkbox" name="color[]" value="Value2"> Title2
<input type="checkbox" name="color[]" value="Value3"> Title3
<input type="checkbox" name="color[]" value="Value4"> Title4
<input type="checkbox" name="color[]" value="Value5"> Title5
PHP Code-
<?php
$to = "arifkpi#gmail.com";
$fromEmail = "arif#arif-khan.net";
$fromName = "Arif Khan";
$subject = "Contact Email";
$message = "Hey, Someone Sent you a Contact Message through your Website.
Details Below-
Name: $_POST[fname] $_POST[lname]
Email Address: $_POST[email]
Contact Number: $_POST[contact1] $_POST[contact2] $_POST[contact3]
Zip Code: $_POST[zip]
Best Time To Contact: $_POST[besttime]
Payment Plan Options: $_POST[payment_plan]
MUA: $_POST[mua]
Shoot Concept:
Shoot Concept(Other): $_POST[shootother]";
$headers = "From:" . $fromName . " " . $fromEmail;
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent to: $to";
}
else{
echo "Error in Email sending";
}
?>
You can simply do,
$colors = isset($_POST['color']) ? implode(",",$_POST['color']) : '';
And now you can use this $colors (you will get all selected colors as comma separeted) in your email message body part.
$message = "Hey, Someone Sent you a Contact Message through your Website.
Details Below-
Name: $_POST[fname] $_POST[lname]
Colors: $colors
Email Address: $_POST[email]
Contact Number: $_POST[contact1] $_POST[contact2] $_POST[contact3]
Zip Code: $_POST[zip]
Best Time To Contact: $_POST[besttime]
Payment Plan Options: $_POST[payment_plan]
MUA: $_POST[mua]
Shoot Concept:
Shoot Concept(Other): $_POST[shootother]";
Try this
if(isset($_POST['color'])) {
$message.= implode(',', $_POST['color']);
}
Related
I've already spend hours on this simple code. Email arrives, but fields like name email newsletter are empty. It must be something stupid but it drives me crazzy that it's not working when it's just simple form.
Also, if you know, can you please help me prevent user from clicking on form twice and sending multiple emails. Thank you a lot!
<?php
$name = strip_tags(htmlspecialchars($_GET['name']));
$email_address = strip_tags(htmlspecialchars($_GET['email']));
$message = strip_tags(htmlspecialchars($_GET['message']));
$newsletter = strip_tags(htmlspecialchars($_GET['newsletter']));
// Create the email and send the message
$to = 'xxx#xxx.com';
$email_subject = "Message from from : $name";
$email_body = "Message from form arrived.\n\n"."Message details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $newsletter\n\nMessage:\n$message";
$headers = "From: form#xxx.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
?>
<form method="GET">
<span>Name</span>
<input type="text" name="name" placeholder="Your name"><br />
<span>E-mail</span>
<input type="email" name="email" placeholder="E-mail"><br />
<div>Message</div>
<textarea type="text" name="message" rows="8" cols="80"></textarea><br />
<input type="checkbox" name="newsletter" id="newsletter">
<label for="newsletter">I wold like to get email infromations</label><br />
<button type="submit">Send</button>
<span class="message"></span>
</form>
You may prefer HEREDOC syntax when working with lengthy strings that include variables, like you would use to generate email content. If your variables are being populated correctly, then this should work.
$email_body = <<<EOT
Message from form arrived.
Message details:
Name: $name
Email: $email_address
Phone: $newsletter
Message:
$message
EOT;
Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
<?php
$errors = '';
$myemail = 'EMAIL#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
after that you can addded your futher code
Try add in top file:
error_reporting(E_ALL);
and edit your code, see:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
http://www.php.net/manual/en/function.error-reporting.php
http://www.php.net/errorfunc
http://php.net/manual/pt_BR/function.set-error-handler.php
http://www.php.net/register_shutdown_function
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
The problem is with your From field in your $headers variable.
you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.
The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.
I've already mentioned it in details here.
I am trying to make a form to ask people if they are attending an event. I have searched multiple threads and forums. I also resorted to google and read through a few tutorials and I am unable to correlate the correct answers to what I need. I am trying to figure out how to send an e-mail to with a message based on the radio button selected. Please help me if you can. It is greatly appreciated.
<FORM method="post" name="RSVPform" action="respond.php" target="_blank">
Name(s): <input name="name" size="42" type="text" /><br/><br/>
Will you be attending the event?<br/><br/>
<input checked="checked" name="answer" type="radio" value="true" /> Yes, I(we) will attend.<br/><br/>
If yes, how many will be attending? <input name="number" size="25" type="text" /><br/><br/>
<input name="answer" type="radio" value="false"/>Sadly, we are unable to attend. <br/><br/> <input name="Submit" type="submit" value="Submit" />
</FORM>
This is the php I've been trying to use.
<?php
$to = "myemail#email.com";
$name = $_REQUEST['name'] ;
$answer = $_REQUEST['answer'] ;
$subject = "RSVP from: $name";
$number = $_REQUEST['number'] ;
$headers = "RSVP";
$body = "From: $name, \n\n I(we) will be attending the event. There will be $number of us. \n\n Thanks for the invite.";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
else
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
?>
I'm not sure how to change the $body message depending on the radio button selected. Is this possible?
You need a condition in the PHP, but note that in your HTML "true" and "false" will be sent as strings not booleans, so are both truthy, but you can check the actual string.
Anywhere after $answer = $_REQUEST['answer'] ; append/modify/write your email body, e.g.
if ($answer=='true') {
$body='Yay you\'re coming';
}else{
$body='Ah screw you then';
}
Here's the absolute basic of what you'd need. Just swap your variables based on the posted answer.
if($_POST['answer'] == "true")
{
// user is coming
}
else
{
// user is not coming
}
I have set up a contact form, and set it to email the response to an email account. Part of the form is a series of checkboxes, and I need to get these to display in the email as a list. This is the code I have below, which at the moment returns 'Array' instead of the values of the checkboxes. Any suggestions?
HTML:
<h3>Service required:</h3>
<input type="text" id="name" name="name" placeholder="Name" required />
<input type="email" id="email" name="email" placeholder="Email" required />
<input class="check-box styled" type="checkbox" name="service[]" value="Service / repairs" /><label> Service / repairs</label>
<input class="check-box styled" type="checkbox" name="service[]" value="MOT" /><label> MOT</label>
<input class="check-box styled" type="checkbox" name="service[]" value="Cars for sale" /><label> Cars for sale</label>
Here's the php:
<?php
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
}
$formcontent= "From: $name \n Service(s) required: $service \n";
$recipient = "name#email.com";
$subject = "You have a new message from $name";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as we can.";
?>
Thanks,
Jason
You should join (for example implode with ', ') your array elements to a string.
<?php
$formcontent= "From: $name \n Service(s) required: ".implode(", " ,$service)." \n";
?>
Why don't you loop through the array to get the desired results into a String?
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
$service_string = "";
for($i=0;$i<count($service);$i++)
{
if($i!=0)
{
$service_string = $service_string . ", ";
}
$service_string = $service_string . $service[$i];
}
}
You will then get an output of a comma separated list of each ticked item as $service_string.
Since several checkboxes are stored in $_POST['service'], it is an array itself and has become two-dimensional. Its different indexes are accessible like this: $_POST['service'][0].
To do something with $_POST['service'], you can use foreach to loop through all indexes:
foreach($_POST['service'] as $post){
//Do stuff here
}
Alternatively, use implode() to simply concatenate all indexes.
Your input type checkbix must have unique names. Otherwise last checkbox will be found in $_POST. Or you can loop through as discuss above. Make your email html format and write a string of html to $formcontent. e.g.
$formcontent = "<html><head></head><body>";
$formcontent .= "<ul><li>".$_POST["checkbox1"]."</li>";
$formcontent .= "<li>".$_POST["checkbox2"]."</li>";
$formcontent .= "</ul></body></html>";
To write email in html format see mail function on php website.
I am a web designer, and dont really know much about PHP. I have a form, and I want the values to be sent to three email addresses.
Here is my HTML:
<form id="player" method="post" action="process.php">
<label for="name">Your Name</label>
<input type="text" name="name" title="Enter your name" class="required">
<label for="phone">Daytime Phone</label>
<input type="tel" name="phone" class="required">
<label for="email">Email</label>
<input type="email" name="email" title="Enter your e-mail address" class="required email">
<input type="submit" name="submit" class="button" id="submit" value="I'd like to join Now" />
</form>
I have somehow found a PHP code that should send the data to ONE email address only, but I dont even know if it works or not.
Here is the code for that:
<?php
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
// Send Message
mail( "you#youremail.com", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
"From: Forms <forms#example.net>" );
?>
Thanks
Add headers
<?php
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
$headers .="From: Forms <forms#example.net>";
$headers .="CC: Mail1 <Mail1#example.net>";
$headers .=", Mail2 <Mail2#example.net>";
// Send Message
mail( "you#youremail.com", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
$headers );
?>
You should be able to separate email addresses with commas in the first parameter of the mail() function, i.e.
mail('email1#example.com, email2#example.com', $subject, $message, $headers);
Or sepcific CC and optionally BCC addresses as per Ahmad's answer.
The mail function (which is used in the code that you posted) allows you to specify multiple recipients. See the PHP documentation of that function for details: http://php.net/manual/en/function.mail.php
Edit: You basically need to replace the "you#youremail.com" part with a list of addresses, separated by commas, e.g.:
mail("you#youremail.com,somebody#domain.com,anotherone#domain.com", ...
use
$to = "email#email.com"
$to .= ", anotheremail#email.com";
this will help you to create multiple recipient.