PHP mail form is not working - php

I m having a problem with my email php form, when I click in the submit button an other page shows up saying There was a problem with your e-mail ()
I don't no what I am doing wrong?
here is my code:
html code
<!-- Subscription Form -->
<form class="email" action="form.php" method="post">
<input class="get_notified" type="text" placeholder="Enter your email address ..."/>
<button type="submit" class="go" /></form>
<!-- End Subscription Form -->
</div>
</div>
</body>
php code
<?php
$to = "email#mydomain.com";
$from = "email#mydomain.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}

You'll need to add a name="email" field to your HTML form in order for PHP to be able to fetch it using $_POST['email']
<input class="get_notified" name="email" type="text" placeholder="Enter your email address ..."/>

Related

Error Missing file part Php Google cloud platform POST

I'm simply trying to "post" text from a subscribe form to a simple email php file but Google Cloud gives this error:
<Error>
<Code>InvalidArgument</Code>
<Message>Invalid argument.</Message>
<Details>Missing file part</Details>
</Error>
I have tried a few things including writing directly to the bucket in Google Cloud but I would rather have text emailed to an address. Why is there a missing file part?
here is my form code:
<form action="email.php" method="post" enctype="multipart/form-data">
<input type="text" name="email" size="15" style="font-size:29px;" id="email" required placeholder="Email">
<input value="RSVP" class="button" name="button" style="font-size:29px;" type="submit">
</form>
Here is my php file:
<?php
$to = "email#gmail.com"; //
$from = "no-reply#website.com"; //
$headers = "From: " . $from . "rn";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo 'Your e-mail (' . $_POST['echo $this->mail'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
?>

PHP email and url validation

So I've made myself a little contact form with php, css, and html. But when I try to add a email validation it still sends the email and doesn't change the style of the input to red (Like I would like it to). Another issue I'm having is the button redirecting to the top of the page (which I do not want it to do). Last I can I make the input keep the text rather than remove it once submitted
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style";
}
?>
Try this for email validation in php
<?php
if (isset($_POST) && !empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style>";
}
}
?>
As per our chat conversation. I am adding jquery ajax function according to your form requirement.
You need to create new file email.php and put your php code into this separate php file
<script>
var url = 'email.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) { // success
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else { // error
$('#email-input').css('color', 'red');//in case of email error
alert('ERROR MESSAGE');//form is invalid
}
}
})
</script>
To handle JSON request you need to send JSON object in response. So change you php code snippet like this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}

Issue with simple PHP Contactform

So I have some issues with my PHP Contactform on a HTML site.
Code below is what I have written in HTML. (that's not te problem)
<form action="sendmail.php" method="POST" class="contact-form">
<input type="text" placeholder="Amount" name="amount">
<input type="text" placeholder="Name" required name="name">
<input type="text" placeholder="Email Address" required name="email">
<div class="validation">
<button class="btn" name="submit">Send request</button>
// END HTML code. (BELOW is starts PHP).
$email_to = "domain#website.com";
$amount = $_POST["amount"];
$name = $_POST["name"];
$email = $_POST["email"];
$email_subject = "DOMAINNAME";
$headers = "From: " . $email . "\n";
$headers .= "Reply-To: " . $email . "\n";
$message = 'Name: ' . $name . ', email: ' . $email . ', amount: ' . $amount;
ini_set("sendmail", $email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);}
{
header("Location:https://website.com");
} else {
echo "There has been an error sending your comments. Please try later.";
}
What do I wrong? I only wanna receive the --> name, amount and mail.
Only when I press submit on my HTML site, it's send me to website.com/sendmail.php (HTTP ERROR 500).
Thank you guys.

Simple subscriber email form - PHP/HTML [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to create a simples subscriber email form , where one user inserts his email, and me and the client should receive a e-mail.
Email to me saying that got a new subscription.
Email to client saying that we will contact him shortly.
I know very little about php, i ask for help about what is wrong on this code and how to make it better in order to make what i want.
HTML:
<div class="mail2">
<!-- Subscription Form -->
<form action="form/form.php" method="post">
<h1>Try Now!</h1>
<input name="email" class="email" type="text" placeholder="Enter your email address ...">
Get started for free
<a class="top" href="#top">Top</a>
</form>
</div>
PHP:
<?php
$to = "office#site.com";
$from = "no-reply#site.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
Edit your html code..
<div class="mail2">
<!-- Subscription Form -->
<form action="form/form.php" method="post">
<h1>Try Now!</h1>
<input name="email" class="email" type="text" placeholder="Enter your email address ...">
<input type="submit" value="Get started for free">
<a class="top" href="#top">Top</a>
</form>
</div>
I would have to say first your HTML has an issue. With the Get started now. You're not actually submitting the form, you're just linking back to the page you're on.
So first change the HTML:
<div class="mail2">
<!-- Subscription Form -->
<form action="form/form.php" method="post">
<h1>Try Now!</h1>
<input name="email" class="email" type="text" placeholder="Enter your email address ...">
Get started for free
<a class="top" href="#top">Top</a>
</form>
</div>
Secondly, here's a neater version of the PHP with some improvements.
$to = "office#site.com";
$from = "no-reply#site.com";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $from;
$headers[] = "Reply-To: " . $from;
$headers[] = "X-Mailer: PHP/".phpversion();
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, implode("\r\n", $headers)))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
If the rest doesn't work, make sure that sendmail is installed and setup correctly on the server.
Just to point out that if the mail check returns false it might not specifically be the e-mail that's at fault as your output error message suggests.

Confirmation Email will not send

So i have this contact form, everything sends fine after the submit button is pressed. However the confirmation email does not seem to send...
I couldnt find the answer on here anywhere, or i would not have asked
The Confirmation email code
<?php
$your_email = "jp.vaughan#icloud.com"; // email address to which the form data will be sent
$subject = "Contact Email";
$thanks_page = "/contact/thankyou.html";
if (isset($_POST["submit"])) {
$nam = $_POST["name"];
$ema = trim($_POST["email"]);
$org = trim($_POST["organisation"]);
$com = $_POST["comments"];
$loadtime = $_POST["loadtime"];
if (get_magic_quotes_gpc()) {
$nam = stripslashes($nam);
$ema = stripslashes($ema);
$org = stripslashes($org);
$com = stripslashes($com);
}
$error_msg=array();
if (empty($nam) || !preg_match("~^[a-z\-'\s]{1,60}$~i", $nam)) {
$error_msg[] = "The name field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}
if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
$error_msg[] = "Your email must have a valid format, such as name#mailhost.com";
}
if (empty($org) || !preg_match("~^[a-z\-'\s]{1,60}$~i", $org)) {
$error_msg[] = "The Organisation must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}
$limit = 1000000000000000000000000000000000000;
if (empty($com) || !preg_match("/^[0-9A-Za-z\/-\s'\(\)!\?\.,]+$/", $com) || (strlen($com) > $limit)) {
$error_msg[] = "Your message must contain only letters, digits, spaces and basic punctuation ( ' - , . )";
}
$totaltime = time() - $loadtime;
if($totaltime < 7) {
echo("<p>Please fill in the form before submitting!</p>");
echo '</ul>
<form method="post" action="', $_SERVER['PHP_SELF'], '">
<input placeholder="Your Name*" name="name" type="text" id="name" value="'; if (isset($_POST["name"])) {echo $nam;}; echo '">
<input placeholder="Your Email Address*" name="email" type="email" id="email"'; if (isset($_POST["email"])) {echo $ema;}; echo '">
<input placeholder="Your Organisation*" name="organisation" type="text" id="organisation" value="'; if (isset($_POST["organisation"])) {echo $org;}; echo '">
<textarea placeholder="Your Message" name="comments" rows="5" cols="50" id="comm">'; if (isset($_POST["comments"])) {echo $com;}; echo '</textarea>
<input type="hidden" name="loadtime" value="', time(), '">
<input type="submit" name="submit" value=" SEND" id="submit">
</form>';
exit;
}
if ($error_msg) {
echo '
<p>Unfortunately, your message could not be sent. The form as you filled it out is displayed below. Make sure each field is completed. Please address any issues listed below:</p>
<ul class="err">';
foreach ($error_msg as $err) {
echo '<li>'.$err.'</li>';
}
echo '</ul>
<form method="post" action="', $_SERVER['PHP_SELF'], '">
<input placeholder="Your Name*" name="name" type="text" id="name" value="'; if (isset($_POST["name"])) {echo $nam;}; echo '">
<input placeholder="Your Email Address*" name="email" type="email" id="email"'; if (isset($_POST["email"])) {echo $ema;}; echo '">
<input placeholder="Your Organisation*" name="organisation" type="text" id="organisation" value="'; if (isset($_POST["organisation"])) {echo $org;}; echo '">
<textarea placeholder="Your Message" name="comments" rows="5" cols="50" id="comm">'; if (isset($_POST["comments"])) {echo $com;}; echo '</textarea>
<input type="hidden" name="loadtime" value="', time(), '">
<input type="submit" name="submit" value=" SEND" id="submit">
</form>';
exit();
}
$email_body =
"Name of sender: $nam\n\n" .
"Email of sender: $ema\n\n" .
"Organisaition: $org\n\n" .
"COMMENTS:\n\n" .
"$com" ;
if (!$error_msg) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
die ("Thank you. Your message has been sent to the appropriate person.");
exit();
}
$sendto = $_POST["email"]; // this is the email address collected form the form
$ccto = "jp.vaughan#icloud.com"; //you can cc it to yourself
$subjectCon = "Email Confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
$header = "From: auto-confirm#moibrahimfoundation.org\r\n";
// This is the function to send the email
if(isset($_POST['submit'])) {
mail($sendto, $subjectCon, $message, $header);
}}
?>
you are exiting the code after sending the first email.
if (!$error_msg) {
if (mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
die ("Thank you. Your message has been sent to the appropriate person."); //you are exiting here
exit(); //additional exit here. the second email won't be sent if there is no error.
}
$sendto = $_POST["email"]; // this is the email address collected form the form
$ccto = "jp.vaughan#icloud.com"; //you can cc it to yourself
$subjectCon = "Email Confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
$header = "From: auto-confirm#moibrahimfoundation.org\r\n";
// This is the function to send the email
if(isset($_POST['submit'])) {
mail($sendto, $subjectCon, $message, $header);
should be
$success='';
if (!$error_msg) {
if (mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>")){
$success="Thank you. Your message has been sent to the appropriate person.";
}else{
$success="Your message cannot be sent";
}
}
$sendto = $_POST["email"]; // this is the email address collected form the form
$ccto = "jp.vaughan#icloud.com"; //you can cc it to yourself
$subjectCon = "Email Confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
$header = "From: auto-confirm#moibrahimfoundation.org\r\n";
// This is the function to send the email
if(isset($_POST['submit'])) {
mail($sendto, $subjectCon, $message, $header);
die($success);
if your first mail is also not sending please check the mail configuration in your php.ini and verify that you can send mail through it.
use this tool for checking ur mail is sending or not...also it will show all parameters of mail function...
Test Tool

Categories