I'm writing to create a contact form on my website and then get the information sent to my inbox, however it is not working whatsoever. Please take a look at my code below (PHP is not my thing) and let me know where i've gone wrong.
Here's the PHP script:
<?php
$to = 'example#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];
$project = $_POST['project'];
$range1 = $_POST['range1'];
$range2 = $_POST['range2'];
$body = <<<EMAIL
Hi, my name is $name.
I'd like to discuss the possibility of working together on a $project.
My budget for the project is £$range1 and I would like to complete the project within $range2 Month(s).
Additional Information:
$message
Regards, $name
<hr>
$name
$email
$tel
EMAIL;
$header = "From: $email";
if($_POST['send']){
mail($to, $subject, $body, $header);
$feedback = 'Thank you for your message, we will get back to you shortly.';
}
?>
And here's the HTML form:
<form id="form_id" name="form_name" action="" method="post">
<div>
<input type="text" name="name" id="name" placeholder="Name" required/>
</div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
</div>
<div>
<input type="tel" name="tel" id="tel" placeholder="Phone" required/>
</div>
<div>
<select required id="project">
<option selected>Select type of project…</option>
<option value="Responsive Website">Responsive Web Design</option>
<option value="Graphic Design">Graphic Design</option>
<option value="Motion Graphics">Motion Graphics</option>
</select>
</div>
<div>
<label for="range1">Budget: </label>
<input type="range" name="range1" id="range1" min="400" max="2000" step="50" value="6" required onchange="rangevalue.value=value"><output id="rangevalue">400</output>
</div>
<div>
<label for="range2">Timeframe: </label>
<input type="range" name="range2" id="range2" min="1" max="12" step=".5" value="1" required onchange="rangevalue1.value=value"><output id="rangevalue1">1</output>
</div>
<div>
<label for="message">Additional Information: </label><br/>
<p>(Please use this space to tell us about your company, the main objectives of the proposed website and anything else you think might be useful)</p>
<textarea name="message" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
Thanks for the help. FYI this can be achieved easily with WordPress through Contact Form 7 (or a similar plugin).
<input type="submit" name="submit" value="submit" />
You have to change this line to:
<input type="submit" name="send" value="submit" />
if($_POST['send']){
actually checking if submit button is clicked...
And, yes - if html and php are on different pages, you have to set proper form action link...
I think you should take a look at this or this, be aware that despite W3Schools may serve as a basics tutorial because of the friendly-user examples, always complement with other resources look here why.
Then you can take a look at this answers, this is just because you need some more basis to understand everything you are doing.
I can't see your $_POST['send'] name in the form, or it's just too late and I'm tired:
Don't know, but maybe, you want this in your form before the submit:
<input type="hidden" name="send" >
Then:
I already posted this answer HERE, but here it is:
First i think your missing some headers look at those
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
2nd check if its true or false when calling the mail function
if( mail($email_to, $email_subject, $email_message, $headers)!==true)
{
die('Fail to send');
}
die('Success');
}
You should take a look at the isset() function to make sure all your variables are set from the form.
Related
I am working on a contact form but every time I try actually running it on my website I keep getting the error message "error with sending email". I'm not trying to do anything complex. I just want it to send the forms to my email. Thank you for helping. Also sorry about any bad formatting as this is my first stack overflow post.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['Name'];
$email = $_POST['Email'];
$subject = $_POST['Subject'];
$query = $_POST['Message'];
$email_from = $name.'<'.$email.'>';
$to="email#website.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email:
$email
<br>
Subject:
$subject
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:https://website.com/messagesuccess");
exit();
}
else {
echo "error with sending email";
exit();
}
?>
HTML:
<div style="margin-top:48px">
<form action="action.php" target="_blank" method="POST">
<p><input class="w3-input w3-border" type="text" placeholder="Name" required name="Name" id="Name"></p>
<p><input class="w3-input w3-border" type="email" placeholder="Email" required name="Email" id="Email"></p>
<p><input class="w3-input w3-border" type="text" placeholder="Subject" required name="Subject" id="Subject"></p>
<p><input class="w3-input w3-border" type="text" placeholder="Message" required name="Message" id="Message"></p>
<p>
<button class="w3-button w3-black" type="submit">
<i class="fa fa-paper-plane"></i> Send Message
</button>
</p>
</form>
</div>
I can't figure out what I am doing wrong. I keep getting the php error message yet all the fields seem to be in place? What am I missing?? Also huge apology since this is my first ever stackoverlfow post and I am a total PHP and stack overflow question asking noob.
I am a PHP noob, so i don't know what to write in my message variable. I need my data from the form to be sent to the email. I got a textarea, 2 inputs (name, email). Want the text from input to be sent to my email. Here code i have:
<?
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'rayetzkiillya#gmail.com';
$subject = 'Обратный звонок';
$message;
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Отправитель <from#example.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>
<form action="send.php" class="postcard" method="post">
<span>Your message:</span>
<textarea type="text" value="" required></textarea>
<div id="stripe1"></div>
<div id="stripe2"></div>
<img src="./images/Seal_of_the_United_States_Department_of_the_Post_Office.svg. png" alt="Oops" id="seal" />
<img src="./images/stamp.jpg" alt="Stamp" id="stamp" />
<div class="inputs">
<div class="inputs" id="input1"><label for="to" type="text" id="to">to: </label> <input type="text" value=" Me" readonly><div id="stripe3"></div></div>
<div class="inputs" id="input2"><label for="from" type="text" id="from">from: </label> <input type="text" id="input2"><div id="stripe3"></div></div>
<div class="inputs" id="input3"><label for="email" type="text" id="email">email: </label> <input type="text" id="input3"><div id="stripe3"></div></div>
<button type="button" class="btn btn-primary" id="send_button">Send</button>
</form>
You can make your message an HTML string, or just plain text.
For plain text, you can do something like
$message .= "name: ".$_POST['name']."\r\n";
$message .= "Message: ".$_POST["theMessage"];
However you need to name your inputs in the HTML as well,
so
<input type="text" id="input2" name="name">
<textarea type="text" value="" required name="theMessage"></textarea>
Or similar.
For the from address, you have to use change your header line
change
$headers .= "From: Отправитель <from#example.com>\r\n";
to
$headers .= "From: ".$_POST["name"]." <".$_POST["from"].">\r\n";
And obviously name your input accordingly:
<input type="text" id="input3" name="from">
Also, as suggested by others, you should be sanitizing/validating these values before using them.
So I have built my website and I planned on using a php based contact page to send emails to my email address I made this and I receive an e-mail but no content or summary...
so here is my html:
<form action="Php/sendemail.php" method="POST">
<div id="form-inner">
<input type="text" class="input" name="name" id="name" placeholder="Name">
<input type="email" class="input" name="email" id="email" placeholder="E-mail">
<textarea class="input textarea" name="message" id="message" placeholder="Your message here"></textarea>
<input type="submit" class="button" value="Send message" id="button">
</div>
</form>
and here is my php:
$subject = "Contact form submitted!";
$to = 'email#example.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: /Contact.php');
?>
I really do not understand why this is happening if you can please help ! Thnaks
This is because you are not geeting the value from the $_POST superglobal:
$body = <<<HTML
$message
HTML;
should be:
$message = $_POST['message'];
$body = <<<HTML
$message
HTML;
Of course there are other ways to do this like just putting $_POST['message']; in the message body but this illustrates how to get the submitted form values.
(You will also want to do things like clean up (i.e. trim(), etc) the submitted values and also defend against header injections as well).
I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.
After researching several questions and try multiple options, I couldn't make it work still!
I have this contact form that won't be sent.
<form id="contact" method="post" action="process.php">
<fieldset>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Your name" title="Your name" class="required">
<label for="email">E-mail:</label>
<input type="email" name="email" placeholder="yourmail#domain.com" title="Your e-mail address" class="required email">
<label for="phone">Phone number:</label>
<input type="tel" name="phone" placeholder="+34 111 22 33 44" title="Your phone number">
<p> </p>
<input type="radio" name="rsvp" value="si">
<span class="destacar-contacto">ACCEPT</span> the invitation<br>
<input type="radio" name="rsvp" value="no">
<span class="destacar-contacto">REJECT</span> the invitation<br>
<p> </p>
<p class="negrita-contacto">If you're coming: what would you like better?</p>
<input type="radio" name="menu" value="carne"> Calf sirloin with foie<br>
<input type="radio" name="menu" value="pescado"> Marinade salmon with dill<br>
<input type="radio" name="menu" value="vegetariano"> Fungus risotto<br>
<label for="mas">Is someone coming with you? Let us know their name and their prefered menu here:</label>
<textarea name="mas"></textarea>
<label for="message">Aditional message:</label>
<textarea name="message"></textarea>
<input type="submit" name="submit" class="boton" id="submit" value="Enviar" />
</fieldset>
</form>
And this is the PHP function:
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$rsvp = strip_tags($_POST['rsvp']);
$menu = strip_tags($_POST['menu']);
$mas = strip_tags($_POST['mas']);
$message = strip_tags($_POST['message']);
mail( "formulario#ourdreamjourney.com", "rsvp",
"Name: $name\nEmail: $email\nPhone: $phone\nRsvp: $rsvp\nMenu: $menu\nMas: $mas\nMessage: $message\n",
"From: Our Dream Journey <mail#hotmail.com>" );
I tried randomly to send just "Name: $name" and I got an e-mail! But then put all the other options back and nothing again...
Someone can help me please?
Thank you so much in advance! :)
You can use phpMailer class. Download this library from here. Many example with to send email.
http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list
Edit:
Try this code
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From:Our Dream Journey <mail#hotmail.com>' . "\r\n";
if(mail( "formulario#ourdreamjourney.com", "rsvp",nl2br("Name: $name\nEmail: $email\nPhone: $phone\nRsvp: $rsvp\nMenu: $menu\nMas: $mas\nMessage: $message\n"),
$headers )) echo "Sent"; die;
Try to add all the information to the header element:
$headers = "From: myplace#here.com\r\n";
$headers .= "Reply-To: myplace2#here.com\r\n";
$headers .= "Return-Path: myplace#here.com\r\n";
$headers .= "CC: sombodyelse#noplace.com\r\n";
$headers .= "BCC: hidden#special.com\r\n";
Maybe there also other options that I don't remember like MIME
Maybe another problem is that some of the variables break the string(close the string). Try to print it somewhere,a file with file_put_contents or just echo it