How can I fix this PHP Mail Form issue? - php

I have a php contact form on my website. However when my customers input their e-mail address and submit their message I got a reply to my own e-mail. For example if they enter adam#yahoo.com I recieve the e-mail as myusername#myhostingprovider.com .... How can I fix this so the reply section shows the e-mail the customer input? Thank you.
Here is my code for my contact form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<fieldset>
<div class="formValidationError bg-red-dark color-white" id="contactNameFieldError">
<p class="center-text uppercase small-text">Name is required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError">
<p class="center-text uppercase small-text">Mail address required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError2">
<p class="center-text uppercase small-text">Mail address must be valid!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactMessageTextareaError">
<p class="center-text uppercase small-text">Message field is empty!</p>
</div>
<div class="formFieldWrap">
<label class="field-title contactNameField" for="contactNameField">Name:<span>(required)</span></label>
<input type="text" name="contactNameField" value="" class="contactField requiredField" id="contactNameField"/>
</div>
<div class="formFieldWrap">
<label class="field-title contactEmailField" for="contactEmailField">Email: <span>(required)</span></label>
<input type="text" name="contactEmailField" value="" class="contactField requiredField requiredEmailField" id="contactEmailField"/>
</div>
<div class="formTextareaWrap">
<label class="field-title contactMessageTextarea" for="contactMessageTextarea">Message: <span>(required)</span></label>
<textarea name="contactMessageTextarea" class="contactTextarea requiredField" id="contactMessageTextarea"></textarea>
</div>
<div class="formSubmitButtonErrorsWrap">
<input type="submit" class="buttonWrap button button-grey contactSubmitButton" id="contactSubmitButton" value="SUBMIT" data-formId="contactForm"/>
</div>
</fieldset>
</form>
Also here is the code for the PHP script
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'myemail#yahoo.com';
// email address ends
$subject = "Mywebsite.com | Mobile - Message From: $name";
$message = "<strong>From:</strong> $name <br/> <strong>E-Mail:</strong> $email </br><br/> <strong>Message:</strong> $message";
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers = 'Reply-To: ' . $email . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>

In your code, you haven't been adding to your header string, you've just been resetting it each time.
What you're finally sending to the mail function in the header variable is just;
'Content-type: text/html; charset=iso-8859-1' . "\r\n"
What you need to do is either where you set $headers = , change the equals sign so it looks like this $headers .=
As that means that it will add on to the String. Or, just format it as one massive string.
<?php
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);

Related

contact.php does not send email fields

My server receives emails but none of the fields are shown.
All fields are empty in the email? Do you have any suggestions? I have tried everything that I know but no results. Form fields does not pass to the php
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'mail#domain.com';
// email address ends
$subject = "Message From: $name";
$message = "<strong>From:</strong> $name <br/><br/> <strong>Message:</strong> $message";
$headers .= 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
for this form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<div class="form-field form-name">
<label class="contactNameField color-theme" >Name:<span>(required)</span></label>
<input type="text" name="contactNameField" id="contactNameField" />
</div>
<div class="form-field form-email">
<label class="contactEmailField color-theme" >Email:<span>(required)</span></label>
<input type="text" name="contactEmailField" id="contactEmailField" />
</div>
<div class="form-field form-text">
<label class="contactMessageTextarea color-theme" >Message:<span>(required)</span></label>
<textarea name="contactMessageTextarea" id="contactMessageTextarea"></textarea>
</div>
<div class="form-button">
<input type="submit" class="btn bg-highlight text-uppercase font-900 btn-m btn-full rounded-sm shadow-xl contactSubmitButton" value="Gönder" />
</div>
Thank you

Using PHP for email file in HTML

I've been having problems using php in my html form. While it will send, the $_POST variables are empty when I try to grab them in the php file. Any ideas on what I could be doing wrong?
My HTML code:
<form class="submitAMessage" name="Submit a Message" method="post" action="sendresults.php">
<div>
<h4>Submit a Message:</h4>
<label for="name">Name:<br><span class="required"></span></label>
<input type="text" id="name" name="name" placeholder="Your name" required="required" />
</div>
<div> <br>
<label for="email">Email Address:<br><span class="required"></span></label>
<input type="email" id="email" name="email" placeholder="your#email.com" required="required" />
</div>
<div> <br>
<label for="message">Message:<br><span class="required"></span></label>
<textarea id="message" name="message" placeholder="Write your message here." required></textarea>
</div>
<div>
<input type="submit" id="submit" name="submit" formmethod="POST" value="Submit" />
</div>
</form>
My php file:
<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Results from Contact Form';
$emailfrom = 'noreply#website.com';
// Your email address. This is where the form information will be sent.
$emailadd = 'website#gmail.com';
// Where to redirect after form is processed.
$url = 'http://www.website.com/main.html';
// Makes all fields required. If set to '1' no field can not be empty.
// If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "Results from Form:\n\n";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$line = '
';
mail($emailadd, $subject, $text.$name.$line.$email.$line.$message, 'From: '.$emailfrom.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
The only thing that sends in the email is:
Results from Form:
Any help is appreciated, thanks in advance!
You need to pass the headers into the mail function which is option.
Here is the functions all parameters
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$to Receiver, or receivers of the mail.
$subject Subject of the email to be sent.
$message Message to be sent.
$additional_headers this is the optional headers which is used for the mail options
you can to set the following values in headers.
// header configuration for to send the HTML mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
$additional_parameters The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail
You should use the header in mail function. Add following code in in your code too.
$header = "From:abc#somedomain.com \r\n";
$header .= "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
mail($emailadd, $subject, $text.$name.$line.$email.$line.$message,$header, 'From: '.$emailfrom.'');
Good luck.

PHP contact form sends message through senders address

Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.

POST input to mail()

echo '<div id="email">
<form action="#" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal .'';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
Sending the email is working fine, it just wont let catch the inserted email.
I can't get the value of <input .... name="mail1"> (me#me.nl) into the "FROM:" section.
What do i wrong OR what is the thing that i don't do in this case ?
Whenever using $headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n"; it works perfectly.
Try
echo '<div id="email">
<form action="a.php" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
if (isset($_POST['submitemail'])) {
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal . '';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
}
the a.php is the name of ur php file

newline issues in PHP email

I am trying to make messages show newline characters as the customer types it, but I am getting /r/n between each line. I also want the $body .= $_SESSION['username']; to appear on a separate line.
I have tried to use this example: PHP nl2br() basic function to solve, but have not been successful.
PHP:
$body .= $_SESSION['username'];
$body .= $message;
$to = $email;
$subject = "copy of your notification";
$headers = "From: noti#r.co.uk\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Bcc:noti#r.co.uk' . "\r\n";
mail($to,$subject,$body,$headers);
HTML form:
<form action="notification.php" method="Post" class="rl">
<div>
<label for="message" class="fixedwidth">Message</label>
<textarea name="message" rows="7" cols="40" id="message"></textarea>
</div>
<div class="buttonarea">
<p>
<input type="submit" name="notify" value="Notify"></p>
</div>
</p>
</form>
You could try using the PHP_EOL constant:
$body .= $_SESSION['username'].PHP_EOL;

Categories