PHP contact form sending multiple emails when I add $headers - php

I have a contact form which sends emails but displays my email server as the "from" and hence it is difficult to reply to emails sent from the contact form
I have tried adding
$headers = 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
to my script but although this works perfectly in respect to sending an email you can reply to, it still also sends an email which you cannot reply to!
I am a PHP newbie so I think I have made an error making two messages send, if someone could please point out what it is that would be much appreciated as although I have looked at other similar questions on here I cannot find a solution
Here is my code:
include ("includes/header.php");
$pagetitle = "Contact Us";
$description = "";
$keywords = "";
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = 'info#mydomain.co.uk';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
$headers = 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
<div class="container-fluid contactform">
<div class="col-md-6 contactform-padding">
<h2>Contact Us</h2>
<?php
if (isset($_POST['submit']) && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
<form method="post" action="index.php" data-ajax="false" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label " for="name">
<p>Name</p>
</label>
<input class="form-control" id="name" name="name" type="text"/>
</div>
Please could someone point out to me why this is sending two emails? I would REALLY appreciate it - I know I have made a stupid mistake and I cannot see why/how
Thanks!

Please check code once again.
You are calling mail function two times.
One in above and another in if condition
from above code remove the mail function
mail can be sent from your if condition

Related

How to create contact form and mail to the owner of website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to get customers details through contact form and that would be sent to my mail id. so I am trying to code but its not working. I have tried by watching YouTube videos
This is my HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name: <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>
</form>
</body>
</html>
This is my PHP Code
<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk#gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
?>
mail function as described here http://php.net/manual/en/function.mail.php
Example:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
So the header "From" must be an email address while you are putting a custom name.
How to get mail function delivery mail to 'to' address please read here
It is mostly correct.
You can't pass Mobile as the additional parameter, that will do nothing.
I have changed the script to append the mobile to the message body.
To set a from address, you need to have a from email, just a name by itself won't work.
Also depending on your php configuration and mail server configuration, you may not be able to change the from address and it will result in an error, or be ignored.
<?php
if ($_POST) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$mes = $_POST['mes'];
// append mobile to message
$mes .= "\r\nMobile: $mobile\r\n";
$from_email = "imjeevajk#gmail.com';
mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");
// mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
exit;
}
Please set submit name field and update the php mail function as mentioned below.
<input type="submit" name="sendMail" value="Send Mail"></input>
<?php
if (isset($_POST['sendMail'])) {
$to = 'admin#dummyemail.com';
$subject = 'Contact From Site';
$from = "From: ".$_POST["name"]."\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message = "Name: ".$_POST["name"]."";
$message .= "Mobile: ".$_POST["mobile"]."";
$message .= "Message: ".nl2br($_POST["mes"])."";
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo 'Thanks for Contacting Us.';
} else{
echo 'Unable to send email. Please try again.';
}
}
?>
Better use PHP Mailer
some servers providers block mail function and that can be reason why it doesn't work.

Mail Form PHP Not Working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I been spending hours and still can't figure it out. New to PHP mail form coding. Any help would truly be appreciated! Two images below: the html and php. Pretty simple setup. I get the echo 'thank you!!' message after clicking send button, but I still don't receive any emails at all.
HTML Form:
<form action="assetslphplmail.php" method="POST">
<input placeholder="NAME" type="text" name="name">
<input p1aceholder="EMAIL" type="email" name="email">
<input p1aceholder="TELEPHONE" type="te1" name="te1">
<input p1aceholder="SUBJECT" type="text" name="subjectline">
<textarea p1aceholder="COMMENT" name="message" rows="6" cols="25" </textarea>
<input c1ass="send_button" type="submit" value="SEND">
</form>
PHP Code:
$subject1ine = $_REQUEST['subject1ine'];
$name = $_REQUEST['name'];
$emai1 = $_REQUEST['emai1'];
$tel 1' $_REQUEST['teI'];
$message = $_REQUEST['message'];
$to = "jondergmai1.com";
mail ( $to, $subjectline, $name, $emai1, $tel, $message);
echo 'Thank you!!';
Replace your PHP code with following:
<?php
$subject = $_REQUEST['subjectline'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$message = $_REQUEST['message'];
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To:'.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$to = "jonder#gmail.com";
mail($to, $subject, $message, $headers);
echo 'Thank you!!';
?>
Edit From and Reply-To part.
Here are the plenty of similar answers: PHP Mail form doesn't send email

Sending email with php and html form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm learning php and I'm trying to send an email to myself using an html form but it doesn't works.
<form action="index.php" role="form" method="post" name="emailform">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="comment">Text:</label>
<textarea type="textarea" class="form-control" id="textarea" rows="5" name="textarea"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit" name="submit">Submit</button>
</form>
<?php
function email()
{
$to = 'my_mail';
$message = $_POST['textarea'];
$from = $_POST['email'];
$subject = 'Portfolio';
$mail_headers = "From: " . $from . " <" . $from . ">\r\n";
$mail_headers .= "Reply-To: " . $from . "\r\n";
$mail_headers .= "X-Mailer: PHP/" . phpversion();
echo $to . " " . $message . " " . $from;
if(#mail($to, $subject, $message, $mail_headers))
echo #mail($to, $subject, $message, $mail_headers);
else echo "ERROR";
}
if(isset($_POST['submit']))
email();
?>
my_mail is my mail (I replaced it here but in the code there is my real email).
The code seem to work, in fact it display the echo #mail but the mail doesn't appear in my inbox
The code looks fine, but I think because of # in the mail function you are not seeing any errors. If you don't want to display error you can use it like this:
<?php
if(#mail($to, $subject, $message, $mail_headers)){
echo "Mail Sent!";
}else{
print_r(error_get_last());
}
?>
In this way the error is not thrown but you can use error_get_last() to see the error and log it if you want.
PS you are using mail function 2 times, so mail would be sent two times when it is going to work.
here is the correct way.
<form action="contact.php" etc... etc..>
blah blah blah
</form>
Then create a new php file (contact.php) and use the following
<?php
$field_name = $_POST["cname"];/* change " " according to your form */
$field_email = $_POST["cmail"];
$field_sub = $_POST["csub"];
$field_message = $_POST["cmsg"];
$to = "mailid1#mail.com, mailid2#mail.com";
$subject = " give subject" ;
$message = "message";
if(mail($to,$subject,$message))
{
echo "<script>alert('Your Message was sent Successfully. Thank You For Your Time !');</script>";
}
else
{
echo "<script>alert('Something wrong happened. Please try again later. Sorry For The Trouble'); </script>";
}
?>
<meta http-equiv="refresh" content="2; url=contact.html">
<!-- for coming back to intial page -->

PHP message sends "undefined"

I have mail.php which sends out an email. This is the code I'm using.
<?php
if(isset($_POST['submit'])){
$to = $_POST['email'];
$subject = 'Your Results!';
$message = $_POST['message'];
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail($to, $subject, $message, $headers);
//header("Location: thankyou.php");
echo $message;
}
?>
The contents of the email is supposed to the .innerText of of a div I have on my index.php page. If I echo out the message, then the output appears the page (mail.php) but when I comment/uncomment the necessary parts to email myself the message I receive is just "undefined".
Is $message not defined?
Here is the form and javascript I'm using
<form action="mail.php" method="post" onsubmit="this.message.value = document.getElementById('box').innerText;">
<input type="email" name="email" required>
<input id="content" type="hidden" name="message" value="">
<input type="submit" name="submit">
</form>
Change .innerText to .textContent. .innerText is a nonstandard property that doesn't exist in Firefox.
I don't know why you didn't have the same problem when used echo $message in the PHP script, unless you tested that version of the script with a different browser (always try to minimize the number of differences when you're testing like this).

php sending mail doesn't work

I have some problem here, I don't see the mistake, it was sending mail before I started using the POST var, but after I set them, mail is not sending, but I still see the screenOK.
$full_name=$_POST["full_name"];
$to = 'admin#mail.com';
$subject = 'Call me';
$message = "call $full_name";
$headers = 'From: admin#mail.com' . "\r\n" .
'Reply-To: '. "\r\n" .
'X-Mailer: PHP/' . phpversion();
if( mail($to, $subject, $message, $headers)){
echo '<script>
$("submit").click(screenOk);
</script>';
}
else echo 'STH got wrong';
?>
the form is here:
<form action=notify.php method=POST>
<div class=field>
<label for=full_name class=full-name>
<svg ...>...</svg>
</label>
<input name=subscriber[full_name] id=full_name placeholder="Your name" class="js-page text-field" data-page=/NameField></div>
What I did wrong?
Your form input is named subscriber[full_name] but you're trying to access it using
$_POST['full_name']
Try changing these to match;
for instance;
<input name="full_name" >
Some reading material: http://www.php.net/manual/en/language.variables.external.php
Try it
you POST variable not matching
<input name="full_name" id="full_name" placeholder="Your name" class="js-page text-field" data-page=/NameField>

Categories