I recently got some help in another topic and was able to receive an email from my contact form, but all of the information save for the message text was excluded, and it was sent from "Apache". Is there any reason this might be happening?
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information, thank you"
if($send_contact){
echo "We've received your contact information, thank you";
}
else {
echo "Error, please try again";
} }
?>
<form action = "../mail.php"method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit" name="submit">
</form>
Wrong variable name you have used.
Please change the last param of the mail call from $header to $mailHeader
$send_contact=mail($to,$subject,$message,$header);
to
$send_contact=mail($to,$subject,$message,$mailHeader);
Append all information what you need before sending. Use the below code,
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$messageContent = $name."\n".$company."\n".$email."\n".$phone."\n".$message."\n";
$send_contact=mail($to,$subject,$messageContent,$mailHeader);
Edit this
send_contact=mail($to,$subject,$message,$header);
to
send_contact=mail($to,$subject,$message,$mailheader);
and you can also send like this
send_contact = main($to,$subject,$formcontact,$mailheader);
if you want...and
sending mail in php is not a one-step process. mail() returns true/false, but even if it returns true, it doesn't mean the message is going to be sent. all mail() does is add the message to the queue(using sendmail or whatever you set in php.ini)
there is no reliable way to check if the message has been sent in php. you will have to look through the mail server logs.
Related
I've found some similar questions to my query, but none that fully match my situation.
I had a basic mail() PHP setup for use on the contact form on my website but, turns out this is blocked on my hosting server to avoid spam/abuse.
The web host recommends using SMTP as a workaround, but the tutorial they link to doesn't seem to provide a working solution for me!
This is my HTML:
<form action="php/mail.php" method="POST">
<p class="form-field-title">Name</p>
<input class="contact-field focus-hide" type="text" id="name" name="name" required autocomplete="off">
<p class="form-field-title">Email Address</p>
<input class="contact-field focus-hide" type="email" id="email" name="email" required autocomplete="off">
<p class="form-field-title">Phone</p>
<input class="contact-field focus-hide" type="text" id="number" name="number" autocomplete="off">
<p class="form-field-title">Message</p>
<textarea class="contact-field focus-hide" id="message" name="message" data-gramm_editor="false" required autocomplete="off"></textarea>
<input class="contact-button" type="submit" value="Send Message">
</form>
And this is my original (non-SMTP) mail.php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$formcontent="Name: $name \nEmail: $email \nPhone Number: $number \nMessage: $message";
$recipient = "email#email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ../thanks.html');
exit();
?>
I have the email server details for my web hosting, but none of the SMTP solutions I've found are working for me!
Is anyone able to help me? I've seen a few things about using PEAR, but their website doesn't seem to be working...?
Thanks!
If your form is in an email, I think you can't post data (maybe I'm wrong). But you can send it to your script with method get.
<form action="php/mail.php" method="GET">
</form>
Then update your php :
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$number = $_GET['number'];
$message = $_GET['message'];
$formcontent="Name: $name \nEmail: $email \nPhone Number: $number \nMessage: $message";
$recipient = "email#email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ../thanks.html');
exit();
?>
It should do the job.
I'm building a site in wordpress and usually use the contactform7 plugin to handle forms, but am trying to learn how to set up a form without having to rely on the plugin. I've had success with this recently, but on this latest site my content is being sent to the recipient. I get an email, but none of the values from the form are being sent.
Here's my form (I know I have to also learn about validation/ sanitization, that will be another topic!):
<form action="<?php echo home_url('/'); ?>sent/" method="POST" class="col-sm-7">
<input id="name" placeholder="name:" name="name" type="text" class="form-control" required></label>
<input id="email" placeholder="email:" name="email" type="text" class="form-control" required>
<textarea id="message" placeholder="message:" name="message" class="form-control" rows="8" required></textarea>
<button id="contact-submit" type="submit" class="btn form-control">Submit</button>
</form>
When the user presses submit the page redirects to url/sent/ in that file I have this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "myemail#email.com";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Now when I get the email in my inbox it shows up like this:
From: , ,
Message:
That's it. Is there something obvious I'm missing here? Any help is, as always, greatly appreciated.
thanks!
Added note: It seems that when the submit button is pressed and the page redirects, it's pulling a 404, though the url it's redirecting to IS correct, in fact, even if I just refresh the page without changing the url it drops the 404 and loads the page content. I guess this is why the content is not being sent, but then why is this happening?
As it turns out this problem is due to an issue with wordpress. Wordpress does not allow you to use the name="name" - this causes a 404 and the form values are not sent. I changes the name to 'form_name' and it works perfectly now.
Thanks for your help, hope this can help someone else too!
Thanks to Tom Elliot at http://www.webdevdoor.com for the help!
http://www.webdevdoor.com/wordpress/submitting-form-wordpress-redirects-404-page/
You should always check to see if the values are set
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "myemail#email.com";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
echo "message sent!";
}else{
die("Error!");
}
}else{
echo "email or name is not set";
}
Also try to change your button to <input type="submit"> even though it might not make a difference.
Other than that the code looks fine
My form is sending a blank email (field data empty) to me every time i visit my site or refresh it.
I had a look at a few posts mentioning 'Post/Redirect/Get', but that confused me more than helped, and im not sure if it pertains to my exact issue.
This was a basic responsive form i found online and had to find the PHP to actually send the form data. (Changing it to suit my needs)
I'm hoping there's just an error i've missed somewhere but i cant seem to find it.
Any help would be greatly appreciated. Thanks in advance.
Here is the basic form (minus css):
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
HTML:
<form id="contact-form" action="/" method="post">
<h3>Say Hello!</h3>
<h4>Fill in the form below, and I'll get back to you as soon as i can. Thanks!</h4>
<div>
<label>
<span>Name: (required)</span>
<input placeholder="Please enter your name" type="text" name="name" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: (required)</span>
<input placeholder="Please enter your email address" type="text" name="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Telephone: (required)</span>
<input placeholder="Please enter your number" type="text" name="phone" tabindex="3" required>
</label>
</div>
<div>
<label>
<span>Message: (required)</span>
<textarea placeholder="Include all the details you can" type="text" name="message" tabindex="5" required></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
use isset
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>
it will post your email only when it submitted by any ..
i have post this one for other in stackoverflow link code link
this is my code.let try , i wish it will help you
<?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);
?>
I'm trying to set up a simple contact form. Everything is styled correctly, but when I hit submit it doesn't take me anywhere, just attempts to open contact.php. I think there's something missing in the code that actually sends the message out. I'm sure it's something fairly simple that I'm missing, but this is a little over my head. Any help is appreciated.
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit">
</form>
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
EDIT: I was able to receive an email finally after adding the complete url for mail.php...however none of the information except for the message was included. The sender was listed as Apache...how can I assure that information entered in the forms will be included in the email? Thanks for all the help thus far.
since you are specifying code within the same page, you can omit action in your form.
Also, put a condition to run PHP once the script has been run.
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
} }
?>
<form action = "<?= $_SERVER["PHP_SELF"]; ?>"method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit" name="submit">
</form>
Hope it helps!
EDIT: I am not sure what is the current filename but to keep it dynamic, I have mentioned $_SERVER["PHP_SELF"] which is not a recommended usage.
However, Try it! :)
Very simple email form code. Worked once, but won't work anymore, even at different addresses. That is to say the emails no longer show up, but I'm not erroring out eather.
<form action="/mail-us.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
And here is the PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "blah#x-matic.net";
$subject = "X-Matic Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error sending email!");
?>
Note, I tested the email out with my gmail account (from gmail to email, instead of from form to email) and it worked.
Use SMTP mail when in localhost, and switch to mail() once the site is in a webserver (i.e your webhost)
At least that is what i do.
Unless you want to install Pegasus Mail on your computer, the mail() function will not work on your localhost computer, as your computer does not have a mail server installed.
Either that, or use SMTP.
if(isset($_POST['submit']))
{
$to = "xxx#google.com";
$subject = "Email from Sender; // quotation marks end ;
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Mail sending Failure!";
}
?>
try this