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
}
Related
I want to set a Dynamic Header on mail header when i send mail. I don't want to do it with SMPT server and if it will be in codeigniter, so it will be greate. You will get idea what exactly i want by given image bellow.
from: Google < dynamic email#gmail.com >
My Code
<?php
if(isset($_POST['send'])) {
//Email information
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
//send email
mail($email, "$subject", $comment, "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
<form method="post" name="testmail">
Email: <input name="email" type="text" />
Subject: <input name="subject" type="text" />
Message:<textarea name="comment" rows="15" cols="40"></textarea>
<input type="submit" name="send" value="Submit" />
</form>
<?php } ?>
use like this:
mail(to,subject,message,headers,parameters);
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 have a form where an user enters into a message, and the message gets sent to the recipient on the other end. I have tried this script multiple times, scoured tutorials, yet I can't seem to find what's wrong. Any ideas?
HTML Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<?php
if(isset($sent))
echo 'Your message has been sent. '; ?>
<label for="Name">Name</label><br />
<input type="text" class="textbox" size="35" id="Name" name="Name" <?php if(isset($name)) echo "value=\"$name\"";?> /><br />
<label for="Service">Service</label><br />
<input type="text" size="35" class="textbox" id="Service" name="Service" <?php if(isset($subject)) echo"value=\"$subject\"";?> /><br />
<label for="Email">Email</label><br />
<input type="text" size="35" class="textbox" id="Email" name="Email" <?php if(isset($from)) echo"value=\"$from\""; ?> /><br />
<label for="message">Message</label><br />
<textarea rows="95" cols="100" id="message" name="message"><?php if(isset($message)) echo"$message"; ?></textarea><br />
<button type="submit">Send Message</button>
</form>
PHP:
if(isset($_POST['Name']) && isset($_POST['Email']) && isset($_POST['Service']) && isset($_POST['message'])) {
$name = $_POST['Name'];
$from = $_POST['Email'];
$subject = $_POST['Service'];
$to = "emailtestertora#gmail.com";
$message = $_POST['message'];
mysql_query("INSERT INTO `Contact`(`Name`, `Email`, `Message`, `Service`) VALUES('$name', '$from', '$message', '$subject')");
$headers = "From:".$from;
if(mail($to,$subject,$message,$headers))
$msgsent = true;
}
Thanks!
(Apologies as this should go as a comment, but it'll be easier layed out in the textbox)
Firstly, debug with the following code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
if (mail('emailtestertora#gmail.com', 'test', 'test')) {
echo 'Mail Sent';
} else {
echo 'Mail Failed';
}
?>
This script will give you an error message when sending the e-mail that will help you debug.
BUT, the important part of this comment, is that the e-mail script above is open to spam (as well as SQL injection). I would strongly encourage you to use a one of the functions/classes that are available that will help you cut out the security holes holes in your mail script.
If you are determined to roll-your-own then great, but please read up about e-mail spam header injection before letting this script on a server. Spammers can send thousands of e-mails very quickly when they find an open script like this, they regularly test automatically so you must clamp down.
(And read up about PHP Database object - PDO - at the same time to save the MySQL injection.)
Your service provider may block your mail.
If you are in dedicated server you may need to configure a mail server
Thanks
Sreeraj
Use this method
this is a php email script which dose not need any smpt connection
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Please forgive the tongue-in-cheek title, but I've been trying for the last hour to get my contact form to work properly. It sends the email just fine but it leaves out all the relevant data (name, email, etc.)
I've modified a PHP contact form tutorial, but I don't know where I've gone wrong.
The HTML:
<form name="form1" method="post" action="send_contact.php">
<fieldset>
<h3>Name</h3>
<input name="name" type="text" id="name">
<h3>Email (required)</h3>
<input name="email" type="text" id="email">
<h3>Phone (required)</h3>
<input name="telephone" type="text" id="telephone">
<h3>Desired appointment time/date</h3>
<input name="time" type="text" id="time">
<input type="submit" name="Submit" value="Submit">
</fieldset>
</form>
The PHP:
<?php
// customer name
$customer_name = "$name";
// customer email
$mail_from = "$email";
// customer telephone
$customer_telephone = "$telephone";
// desired appointment time
$appointment_time = "$time";
// subject
$subject = "Appointment for $customer_name";
// message
$message = "$customer_name would like to book an appointment for $appointment_time";
// header
$header = "from: $customer_name <$mail_from>";
// recipient
$to = 'my#emailaddress.com';
$send_contact = mail($to,$subject,$message,$header);
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>
You don't need quotes.
$customer_name = "$name";
$customer_name = $name;
You should really use post to grab the data.
$customer_name = $_POST['name'];
you need to be looking in the super global $_POST for your variables. for example
$customer_name = $_POST['name'];
if your posting the data you need to get it from the post: I would trim it also
$customer_name = trim( $_POST['name'] );
I've been creating a blog using tutorials around the web and I have a working comments system but what I would like is if when the user adds a comment that I get an email. I'd really love if you could explain how exactly I could go about implementing a notification. Any help is greatly appreciated.
Current Form:
<form id="commentform" method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<input type="text" name="name" id="name" title="Name (required)" /><br />
<input type="text" name="email" id="email" title="Mail (will not be published) (required)" /><br />
<input type="text" name="url" id="url" title="Website" value="http://" /><br />
<br />
<textarea title="Your Comment Goes Here" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
Process.php:
<?php
if (isset($_POST['submit_comment'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.");
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*#([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
}
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
header("Location: post.php?id=" . $entry);
}
else {
die("Error: you cannot access this page directly.");
}
?>
If what you are looking for code in PHP that can send an email, below is one from here for you to look at. Insert the code to send email just before or after you INSERT the comment into your database.
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Requirements:
For the Mail functions to be
available, PHP must have access to the
sendmail binary on your system during
compile time. If you use another mail
program, such as qmail or postfix, be
sure to use the appropriate sendmail
wrappers that come with them. PHP will
first look for sendmail in your PATH,
and then in the following:
/usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib.
It's highly recommended to have
sendmail available from your PATH.
Also, the user that compiled PHP must
have permission to access the sendmail
binary.