php contact form sending mail to server admin [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have little issue about my contact form. it doesn't send mail the server admin.
<?php
session_start();
// get the data from the form
$admin_email = 'server#admin.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$subject = 'Contact Form';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['www.mywebsite.com'];
// check if the fields are empty
if(empty($email) or empty($name) or empty($email) or empty($message)){
$output = "All fields are required!";
}else{
if(md5($captcha) == $img_session){
$header = "From: $email"."\r\n"
.'Content-Type: text/plain; charset=utf-8'."\r\n";
$message = "
New entry from $subject!
Name: $name
E-Mail: $email
Message:
$message
This message was sent from http://$website";
if(mail($admin_email, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
$output = "Your message was sent!<br />Thank you!";
}
}else{
$output = "Wrong captcha code!";
}
}
echo $output;
?>
UPDATED
the wrong part is if $admin_email is not match the contact form email address wrong captcha error pops up? I don't know why. I mean if I don't write contact form email line, same address with admin_email I can't send mail??? What I am trying to do here: send mail to the guest (your mail sent), and also $admin_email: server#admin.com (You have email) $messege.
Form
<form action="" id="contact_form" method="POST">
<p>Name:</p>
<input type="text" name="name" placeholder="Enter name" required=""/>
<p>Email:</p>
<input type="email" name="email" placeholder="Enter email" required=""/>
<p>Message:</p>
<textarea name="message" rows="10" cols="30" required=""></textarea>
<p>Captcha:</p>
<img src="captcha.php" id="captcha"/>
<input type="text" name="captcha" placeholder="Enter code" required=""/>
<input type="submit" value="Submit" />
</form>

If you want to send both email to user and admin, try to call the mail function twice, for example
if(mail($user_email_address, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
$output = "Your message was sent!<br />Thank you!";
}
if(mail($admin_email_address, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
$output = "You've got a new message from #user_email_address <br /> ";
}

Related

PHP Contact Form doesn't deliver mails [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
My contact form won't work. The messages aren't sent to the given email address.
The form successes but the emails aren't delivered. The other question has another code using if tags, which won't work for my script.
The html code I used is:
<form class="form" id="form1" action="mail.php" method="post">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="NAME" id="name" />
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="E-MAIL" />
</p>
<p class="text">
<textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="WHAT'S UP?"></textarea>
</p>
<div class="submit">
<input type="submit" name="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
The PHP script I use is:
<?php
// We create a variable for name value
$name = $_POST['name'];
// We create a variable for email value
$email = $_POST['email'];
// We create a variable for message value
$message = $_POST['text'];
// We provide an e-mail address from which the email is sent
$from = "xyz#adress.com";
// Provide the e-mail address on which you want to receive messages
$to = "xyz#gmx.de";
// Provide the subject of the e-mail
$subject = "Contact form from xyz.com";
// We prepare the message body
$emailbody = "";
$emailbody .= "Message: " . $message . "\n";
// We add UTF-8 to the header of our message
$header = "";
$header .= "From:" . $from . " \n";
$header .= "Content-Type:text/plain;charset=utf-8";
// Sending message
$success = mail($subject, $emailbody, $header);
// Redirect after sending the message
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=de/confirmation_signup.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=de/error.html\">";
}
?>
Anybody has a clue what is going wrong inside the form?
You are passing vars to mail function wrong;
Change this:
mail($subject, $emailbody, $header);
To this:
mail($to,$subject, $emailbody, $header);
See: http://php.net/manual/en/function.mail.php

PHP Mail Form not sending [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I know this is a popular question. However no other questions can give me the answer I'm looking for.
Have a contact form (mail() being using. And I can't get it to send. A coder that helped me create it somehow got his to send, because I received a few messages in my inbox the mail was coded to send to. I copy and pasted the code, and I'm testing it locally, but it's not sending the mail.
Is the problem it's not sending because I'm testing it locally and its not live and hosted yet? or does the problem rely in my code, and if so, where?
***Not including the validation code, but I have it there...
FORM:
<form method="post" action="">
<input type="text" name="name" placeholder="*Name" value="<?php echo $_POST['name']; ?>">
<input type="tel" name="phone" placeholder="*Phone Number" value="<?php echo $_POST['phone']; ?>">
<input type="email" name="email" placeholder="*Email" value="<?php echo $_POST['email']; ?>">
<input type="text" name="invoice" placeholder="Invoice Number (optional)" value="<?php echo $_POST['invoice']; ?>">
<textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="*Please enter your comments here..."><?php echo htmlentities($_POST['comments'], ENT_COMPAT,'ISO-8859-1', true);?></textarea>
<button type="submit">Submit</button>
</form>
PHP:
if(!empty($_POST)){
$POST = filter_post($_POST);
$invoice = array_splice($POST,3,1);
$MSG = check_empty($POST);
$email = test_input($_POST["email"]);
if(!array_filter($MSG)){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$MSG[] = "Invalid Email Format (test#provider.com)";
}
else{
$POST['invoice'] = $invoice['invoice'];
if(send_mail($POST)){
header('Location: messageSent.php');
}
else{
$MSG[] = "Email Failed. Please Try Again.";
}
}
}
}
function send_mail($POST){
extract($POST);
$to = '7servicerepairs#gmail.com';
$sbj = 'New Question For Se7en Service!';
$msg = "Name: $name \n Phone: $phone \n Email: $email \n Invoice #: $invoice \n Comments: $comments";
$headers = "From: $email";
return(mail($to, $sbj, $msg, $headers));
}
You will need an smtp server in your localhost. Otherwise you won't be able to send it.

send html form with php can't solve

I have big problem with sending easy html form with php.
My problem is when all fields are empty it still send message.
I don't why this code still send empty form??
<form id="form1" name="form1" method="post" action="forma.php">
<p>
<label for="ime">Ime:</label>
<input type="text" name="ime" id="ime" />
</p>
<p>
<label for="prezime">Prezime:</label>
<input type="text" name="prezime" id="prezime" />
</p>
<p>
<label for="email">e-mail:</label>
<input type="text" name="email" id="email" />
</p>
<p>
<label for="poruka">Poruka:</label>
<textarea name="poruka" cols="40" rows="10" id="poruka"></textarea>
</p>
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
My php code:
<?php
if (isset($_POST['submit']))
{
$ime= $_POST['ime'];
$prezime= $_POST['prezime'];
$email= $_POST['email'];
$poruka= $_POST['poruka'];
$email_from = 'neki#email.com';
$email_subject = "forma sa sajta";
$email_body = "Ime: $ime. \n". "Prezime: $prezime \n". "email: $email \n". "poruka: $poruka" ;
$to = "myemail#gmail.com";
mail ($to, $email_subject, $email_body);
echo "Message is sent";
}
else {
echo "Message is not sent";
}
?>
So again, when i fill fields message is sent. It is ok, i received email.
But when i just click submit (without filling fields) it still send message to my email.
What is wrong with this code? I try everything i know, but without success.
Thank you.
The problem is that you are only checking to see if "submit" is set.
Your if statement should read something like:
if(isset($_POST['submit']) && all_other_fields_are_valid($_POST)){...}
function all_other_fields_are_valid($fields)
{
//logic to decide what fields and values you require goes here
}
You need to check all required field. only check submit it will attempt mailing:
if (isset($_POST['submit']
, $_POST['ime']
, $_POST['prezime']
, $_POST['email']
, $_POST['poruka']))
Additionally you can validate from the client side using new HTML5 required attribute:
<input type="text" name="ime" id="ime" required />
This way you don't waste server resources for bad formed requests.
Actually since they are sent as empty variables, they will still evaluate to true in isset(), even if there is no text in the input fields when they are submitted.
if($_POST['ime'] && $_POST['prezime'] && $_POST['email'] && $_POST['poruka']) {
// do stuff here
}
As long as all of these have values and none of the values are 'false' or '0', this will evaluate to true only if somebody puts text in all of the input fields.
This will check if all required POST vars are set, if they're empty and give an error if so:
<?php
if (isset($_POST['submit'], $_POST['ime'], $_POST['prezime'], $_POST['email'], $_POST['poruka']))
{
$error = "";
if($_POST['ime'] == ""){
$error .= "ima was empty!<br />";
}
if($_POST['prezime'] == ""){
$error .= "prezime was empty!<br />";
}
if($_POST['email'] == ""){
$error .= "email was empty!<br />";
}
if($_POST['poruka'] == ""){
$error .= "poruka was empty!<br />";
}
if($error == ""){
$ime= $_POST['ime'];
$prezime= $_POST['prezime'];
$email= $_POST['email'];
$poruka= $_POST['poruka'];
$email_from = 'neki#email.com';
$email_subject = "forma sa sajta";
$email_body = "Ime: $ime. \n". "Prezime: $prezime \n". "email: $email \n". "poruka: $poruka" ;
$to = "myemail#gmail.com";
mail ($to, $email_subject, $email_body);
echo "Message is sent";
} else {
echo $error;
}
} else {
echo "Message is not sent";
}
?>

contact form, message is not recived

I need to make a contact form and I did, but when Im sending test message Im not reciving that message, I have check my email addres and test message never get there i also looked into spam folder but still no luck how to fix this? here is my code
<?php
$formMessage = "";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
if ($_POST['username']) { // If the form is trying to post value of username field
// Gather the posted form variables into local PHP variables
$senderName = $_POST['username'];
$senderEmail = $_POST['email'];
$senderMessage = $_POST['msg'];
// Make sure certain vars are present or else we do not send email yet
if (!$senderName || !$senderEmail || !$senderMessage) {
$formMessage = "The form is incomplete, please fill in all fields.";
} else { // Here is the section in which the email actually gets sent
// Run any filtering here
$senderName = strip_tags($senderName);
$senderName = stripslashes($senderName);
$senderEmail = strip_tags($senderEmail);
$senderEmail = stripslashes($senderEmail);
$senderMessage = strip_tags($senderMessage);
$senderMessage = stripslashes($senderMessage);
and here is part which im not sure about
// End Filtering
$to = "test#gmail.com";
$from = "contact#your_website_here.com";
$subject = "You have a message from your website";
// Begin Email Message Body
$message = "
$senderMessage
$senderName
$senderEmail
";
// Set headers configurations
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "Content-type: textrn";
// Mail it now using PHP's mail function
mail($to, $subject, $message, $headers);
$formMessage = "Thanks, your message has been sent.";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
} // close the else condition
} // close if (POST condition
?>
here is html form
<form id="form1" name="form1" method="post" action="contact.php">
<?php echo $formMessage; ?>
<br />
<br />
Your Name:<br />
<input name="username" type="text" id="username" size="36" maxlength="32" value="<?php echo $senderName; ?>" />
<br />
<br />
Your Email:
<br />
<input name="email" type="text" id="email" size="36" maxlength="32" value="<?php echo $senderEmail; ?>" />
<br />
<br />
Your Message:
<br />
<textarea name="msg" id="msg" cols="45" rows="5"><?php echo $senderMessage; ?> </textarea>
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Send Now" />
</form>

My PHP Contact Form is not working

I am creating a website and I was making a contact form with PHP. It is turning up no errors on the page itself but the email never shows up in the inbox or spam folder of my email. The code I have for it is:
$_NAME = $_POST["name"];
$_EMAIL = $_POST["reply"];
$_SUBJECT = $_POST["subject"];
$_MESSAGE = $_POST["message"];
$_MAILTO = "myemail#gmail.com";
$_SUBJECT = "Contact Form";
$_FORMCONTENT = "From: ".$_NAME." Subject: ".$_SUBJECT." Message: ".$_MESSAGE;
$_MAILHEADER = "Reply To: ".$_EMAIL;
mail($_MAILTO, $_SUBJECT, $_FORMCONTENT, $_MAILHEADER);
Any ideas what the problem is?
EDIT --
Here's the HTML form:
<form id="contact" name="contact" action="contact2.php" method="post">
<input type="text" class="name" name="name" id="name" placeholder="Your name (optional)" onfocus="placeholder=''" onblur="placeholder='Your name (optional)'" /><br><br>
<input type="text" class="reply" id="reply" name="reply" placeholder="Your email (optional)" onfocus="placeholder=''" onblur="placeholder='Your email (optional)'" /><br><br>
<input type="text" class="subject" id="subject" name="subject" placeholder="Subject" onfocus="placeholder=''" onblur="placeholder='Subject'" /><br><br>
<textarea class="message" id="message" name="message" rows="10" cols="50" placeholder="Enter your message" onfocus="placeholder=''" onblur="placeholder='Enter your message'"></textarea><br><br>
<input type="submit" class="send" id="send" name="send" value="Send Message" />
</form>
First step would be to check the return value of the mail function to see if the email was successfully (as far as PHP/mail function can ascertain) sent.
$mail_result = mail($_MAILTO, $_SUBJECT, $_FORMCONTENT, $_MAILHEADER);
if ($mail_result) {
echo <<<HTML
<div>Mail was successfully sent!</div>
HTML;
} else {
echo <<<HTML
<div>Sending mail failed!</div>
HTML;
}
Secondly, you should check that all appropriate settings are correctly set in your php.ini file, specifically the sendmail_path setting. You should definitely take a look at official documentation in the PHP Manual.
As a last ditch effort, you may want to look into an alternate method of sending the mail.
Simple Example:
<?php
// Has the form been submitted?
if (isset($_POST['send'])) {
// Set some variables
$required_fields = array('name', 'email'); // add fields as needed
$errors = array();
$success_message = "Congrats! Your message has been sent successfully!";
$sendmail_error_message = "Oops! Something has gone wrong, please try later.";
// Cool the form has been submitted! Let's loop
// through the required fields and check
// if they meet our condition(s)
foreach ($required_fields as $fieldName) {
// If the current field in the loop is NOT part of the form submission -OR-
// if the current field in the loop is empty
if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {
// add a reference to the errors array, indicating that these conditions have failed
$errors[$fieldName] = "The {$fieldName} is required!";
}
}
// Proceed if there aren't any errors
if (empty($errors)) {
// add fields as needed
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
$email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );
// Email receivers
$to_emails = "anonymous1#example.com, anonymous2#example.com";
$subject = 'Contact form sent from ' . $name;
$message = "From: {$name}";
$message .= "Email: {$email}";
$headers = "From: {$name}\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if (mail($to_emails, $subject, $message, $headers)) {
echo $success_message;
} else {
echo $sendmail_error_message;
}
} else {
foreach($errors as $invalid_field_msg) {
echo "<p>{$invalid_field_msg}</p>";
}
}
}

Categories