I'm using a simple php code to send mail from my website
<?php
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "contato#pedrogps.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thanks for your message";
?>
when i submit the email, it brings to another page (mail.php) with the "thanks for your message" message.
What can i do to display this message inside my html page without switching pages.
There are two ways one is use AJAX and another one:
make form action empty like
<form name="form1" action="" method="post">
before form start or top of the page write this php code:
if(isset($_POST)){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "contato#pedrogps.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thanks for your message"; // store this msg in a variable and display wherever you required to echo
}
If you do not want the webpage to load, when submitting, you'll need some JavaScript.
<form id="mailForm">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" value="Send">
</form>
<div id="result"></div>
<script>
$('#mailForm').submit(function(e) {
e.preventDefault();
var formData = $('#mailForm').serialize();
var url = "mail.php";
var posting = $.post(url, formData);
posting.done(function(data) {
$("#result").append(data);
});
});
</script>
Remember to load the jQuery library, read more about jQuery here: https://learn.jquery.com/about-jquery/how-jquery-works/. Read more about the jQuery.post() here: https://api.jquery.com/jquery.post/
If you do not care if the webpage loads, when submitting, then you can easily use the include function in PHP.
<form id="mailForm" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" name="mail" value="Send">
</form>
<div id="result">
<?php
if(isset($_POST['mail']))
{
include("mail.php");
}
?>
</div>
Maybe you should check some more things than only the $_POST['mail'] is set, but you'll get the idea.
Related
So I have used the code:
<form action="../scripts/mail.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 the php file linked is
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone Message: $message";
$recipient = "email#live.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
The error when testing is: {"code":"MethodNotAllowedError","message":"POST is not allowed"}
What am I doing wrong?
There is no $phone variable defined yet you're trying to use it. Also see this in relation to Adobe products: https://community.adobe.com/t5/dreamweaver/quot-code-quot-quot-methodnotallowederror-quot-quot-message-quot-quot-post-is-not-allowed-quot-php/td-p/8967727?page=1
Try this, just to be double paranoid:
<form method="post" action="../scripts/mail.php">
<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>
mail.php :
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email Message: $message";
$recipient = "email#live.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
}
Is it a webpage or website?
If it is a web page you can install xampp(link - https://www.apachefriends.org/download.html). you have to move your file into "xampp/htdocs/" and then open "xampp control panel", click the start button in apache. Then go to your web browser and type as "localhost/yourfile.php/".Then the error message will not show.
if you want to send email watch this video -https://www.youtube.com/watch?v=KA2UB3pxEtg&t=26s
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.
For some reason the $name, $email and $message variables aren't being passed into the $msgcontents and $headers variables. After I fill out the form and click submit the message in my inbox looks like this:
Subject: Message from Contact Form
From: Unknown sender
Date: Mon, November 17, 2014 7:44 pm
To: myemail#myemail.com
Name:
Email:
Message:
I've used var_dump() to see if my variables are being populated after clicking submit and they are but for some reason they are not being picked up by $msgcontents and $headers. Where am I going wrong?
Here's my code:
<?php
$name = trim(htmlspecialchars($_POST['name'], ENT_QUOTES));
$email = trim($_POST['email']);
$message = trim(htmlspecialchars($_POST['message'], ENT_QUOTES));
$to = "myemail#myemail.com";
$subject = "Message From Contact Form";
$msgcontents = "Name: $name <br> Email: $email <br> Message: $message";
$headers = array("MIME-VERSION: 1.0",
"Content-type: text/html; charset=iso-8859-1",
"From: $name <$email>",
"Reply-To: contact#info84.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
$mailsent = mail($to, $subject, $msgcontents, $headers);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" novalidate>
<input type="text" name="name" placeholder="Your Name">
<input type="email" name="email" placeholder="Your Email">
<textarea name="message" placeholder="Your Message"></textarea>
<input type="submit" name="submitform" value="send">
</form>
<?php
var_dump($to);
?>
<br>
<?php
var_dump($subject);
?>
<br>
<?php
var_dump($msgcontents);
?>
<br>
<?php
var_dump($headers);
?>
<br>
<?php
var_dump($name);
?>
<br>
<?php
var_dump($email);
?>
<br>
<?php
var_dump($message);
?>
</body>
</html>
I think you check the message sended from first page load (without the form)
Try to wrap your email sending code, like this:
if (isset($_POST['submitform'])) {
//...
mail(...);
//...
}
Then, when page loads no message been sended, but when you click a submit button, mail been sended
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 have a simple php form for contact purposes. However, it won't send the email or go to the correct page after submit. It redirects to the mail.php instead.
My contact form named contact.php is as follows:
<form id="contact-form" action="mail.php" method="POST">
<fieldset>
<label><span class="text-form">Your Name:</span> <input type="text" name="name"></label>
<label><span class="text-form">Your Email:</span><input type="text" name="email"></label>
<label><span class="text-form">Your Contact No:</span><input type="text" name="contact"></label>
<div class="wrapper">
<div class="text-form">Your Message:</div>
<textarea name="message" rows="6" cols="25"></textarea><br />
<div class="clear">
</div>
<div class="buttons">
<a class="button" href="#"><input class="button" type="submit" value="Send"></a>
<a class="button" href="#"><input class="button" type="reset" value="Clear"></a>
</div>
</fieldset>
</form>
And the php code named mail.php is as follows:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location:contact.php");
?>
What am i doing wrong. It just wont send the message to email or redirect to the correct page??
Your syntax is ever so slightly wrong. &contact on line 5 should be $contact. I assume you're on a production server, so error reporting would be disabled and you wouldn't get any warnings.
Try using # before mail function
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("location: contact.php");
?>
this will let you pass even if mail function produce any issue. Also please check if there are any other header are not sent using http://php.net/manual/en/function.headers-sent.php function.
I would strongly suggest to use PHPMailer or some other class to send any kind of email.
Please check the contact.php file whether some redirection is happening from contact.php to mail.php
I think there may be an issue sending mail from your server. The mail.php file is probably tripping up on the mail function and not doing the redirect.
Create a new file with just the php mail function in there (with your email address and a test message) and browse it from your web browser, does it send?
Try to use ob_start() at the start of script, and exit(); after header("Location: contact.php");
smth like this ...
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: contact.php");
exit();
?>
Change the mail.php to the following:
<?php
mail("info#whatever.co.za", "Contact form message", $_POST['message'], "From: ".$_POST['name']." <".$_POST['email'].">");
header("Location: contact.php");
?>