So I've completed a contact form made in PHP. After testing it sends emails, but in the email header it is not capturing the sender's name or email address. It shows up as sh-908129268#eu.hosting-webspace.io
Is there a way to correct that? I need to be able to respond to emails.
The inputs are as follows:
<form id="contact" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<input class="input is-small" type="text" name="name" placeholder="Text input" value="<?= $name ?>" >
<p class="help is-danger"><?= $name_error ?></p>
<input class="input" type="email" name="email" placeholder="Email Address" value="<?= $email?>">
<p class="help is-danger"><?= $email_error?></p>
// back-end file
$sendTo = 'test#email.com';
$subject = 'Business Enquiry';
$headers = "";
$headers .= "Sent From: ".$name. "\r\n";
$headers .= "Reply To: ".$email. "\r\n";
$headers .= "Message: ".$message. "\r\n";
if (mail($sendTo, $subject, $message, $headers)) {
$success = "Thank you for your message, I will reply as soon as I can.";
$name = $email = $message = ''; // resets all fields
}
You cannot pick the names of the headers freely, you have to adhere to the standard (RFC 5322 Internet Message Format).
Your header "Sent From" should be just From, your header "Reply To" should be Reply-To and your header "Message" shouldn't be in the headers at all - you already pass the message as such into the mail function.
All fixed now, thanks. All I needed was to specify one header ,
$headers = "From: ".$email;
So it's just $subject and $headers
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have an HTML form that posts to a PHP mail function. For some reason it does not send the content anymore. The emails display "This email has no content." Am I missing something?
//HTML - Post method form.
<form class="form" id="form" action="submitform.php" method="post">
<h2 style="padding-top: 10px;">Contact Us</h2>
<input class="input" type="text" placeholder="Name" name="name"><br>
<input class="input" type="text" placeholder="E-Mail Address" name="email"><br>
<input class="input" type="text" placeholder="Phone #" name="phone"><br>
<textarea class="input" placeholder="Questions, Specifications, etc."
name="message</textarea>
<input class="inputButton" type="submit">
</form>
//PHP - Gets posted HTML input data and sends it to the email, formatted with \n
<?php
$to = 'example#example.com' ;
$subject = 'Inquiry' ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nPhone: $phone \nMessage: $message \n";
$sent = mail ($to, $subject, $message) ;
if($sent == true) {
echo "<h3>Thank you for your inquiry.</h3>";
}else{
echo "<h3>Sorry, your message wasn't sent.</h3>;
}
?>
I've found defining headers, even if I just want default values, helps me get my mail delivered. I can't say this is what you need to do because your code looks like it should run successfully as is.
An example of setting the headers to (I believe) default values:
$to = $to_email;
$subject = $your_subject;
$message = $your_message;
$headers = "From: S.O.<no-reply#dontusethis.email>\r\n" .
"Reply-To: [can be nice, not needed of course]\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
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.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Recently, I have purchased a domain and hosting. I've also created a webpage having contact form in it. I want when the user wrote something in textarea of contact form and clicks submit then that message is sended to my gmail account and so that i can reply also to those messages. I've also used this script to do so but its not working.
This is sendemail.php file
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'raunakhajela#gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
This is my contact form code in index.php
<div class="contact" id="contact">
<div class="container-3x">
<div class="block-header"> <!-- Blocks Header -->
<h2 class="title">Contact Us</h2>
</div>
<div class="block-content"> <!-- Blocks Content -->
<form action="sendemailphp" method="post" class="trigger-form-animate">
<input type="text" value="" id="name" name="name" placeholder="Name *" />
<input type="text" value="" id="email" name="email" placeholder="Email *" />
<input type="text" value="" id="website" name="website" placeholder="Website *" />
<textarea type="text" value="" id="message" name="message" rows="3" placeholder="Your Message *" ></textarea>
<div class="clr"></div>
<button>Submit Message</button>
</form>
</div>
</div>
</div>
I've also tried "http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html" this tutorial but its not working. Unable to find "Send through Gmail (easier to set up)" this option in gmail on adding another accounts windoww.
How can i do that?? Plzz hlp
You don't have a named subject form element which may explain why mail is failing and may very well be sent to Spam instead.
For example: <input type="text" name="subject">. Either add one of replace.
You will however need to make sure that this is filled out using a conditional statement
(see further below for an example).
$subject = #trim(stripslashes($_POST['subject']));
with
$subject = "Form submission";
You could also add this instead to your form:
<input type="hidden" name="subject" value="Form submission">
Either this or what I've outlined just above will work.
Many Email clients will either send mail to Spam or reject it altogether if a "subject" is missing, which is clearly missing from your code.
Checking if subject has been filled example:
if(isset($_POST['subject']) && !empty($_POST['subject'])){
// do someting
}
try this,both will work
<?php
$to = "usermailid#gmail.com";
$subject = "Welcome to";
$message = " Hi $username,<br /><br />
Thank you for signing up with us.<br />
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
if($mail)
{
$to = "admin#gmail.com";
$subject = "Following Customer Signed Up";
$message = " $username,Customer is signed up with us,<br /><br />
Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
Phone:$phone<br/>Zip Code:$zip<br/>Message:$message_cust<br/><br/><br/>
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
}
?>
i've been trying to fix this one with solutions i've found here and in other foruns but nothing seems to work. I'm really noob with PHP. I don't know if this info is relevant but i'm hosting my project at byethost.com which they say it supports PHP.
I have made a contact form in my HTML contact page, made a PHP file to process the data and send it to my gmail adress. It says message successfully sent, but the data never arrives at my inbox on gmail. I've tried changing the PHP code (with copy paste solutions) in alternative ways but with no luck. I've also tried to sent it to my hotmail account but it doesn't work either. Can anyone help me?
Here is the HTML contact form:
<div id="form_wrap">
<form method="post" action="form_process.php" id="contact_form" class="contact">
<label>
<span>NOME*</span>
<input name="nome" type="text" tabindex="1" required>
</label>
<label>
<span>E-MAIL*</span>
<input name="email" type="email" tabindex="2" required>
</label>
<label>
<span>TELEFONE*</span>
<input name="phone" type="tel" tabindex="3" required>
</label>
<label>
<span>WEBSITE</span>
<input name="website" placeholder="http://" type="url" tabindex="4">
</label>
<label>
<span>MOTIVO DE CONTACTO*</span>
<select class="escolha" name="motivo" size="1" tabindex="5" required>
<option>Contratá-lo</option>
<option>Fazer uma pergunta</option>
<option>Dizer olá ou agradecer</option>
</select>
</label>
<div>
<label>
<span>MENSAGEM*</span>
<textarea name="message" tabindex="6" required></textarea>
</label>
</div>
<div>
<input name="submit" type="submit" value="Enviar" id="submit">
</div>
</form>
<a class="subtop" href="#subtop">– Voltar ao topo</a>
</div>
Here is my form_process.php:
<?php
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];
$to = "mygmailadress#gmail.com";
$subject = "Site contact form";
$header = "From: ".$fromText."\r\n";
$header .= "Cc: ".$ccText."\n";
$header .= "Reply-To : ".$fromText."\r\n";
$header .= "Return-Path : ".$fromText."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
mail($to, $subject, $header, $message, "From: " . $nome);
}
if(#mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
?>
Why twice mail function?
mail has 4 parameters, you give 5 params (from is a paert of fourth, it should be in $header)
variable $emailRecipient used in your mail function doesn't exists.
in 'from' header should be an e-mail address (and name), not only a non-mail string
There are many possible reasons that the email never arrives.
It could be caught in spam, blocked by your host or your mail function could be incorrectly setup in your php.ini file.
However, from your code it looks like you are using the mail() function incorrectly.
You are trying to send the email twice by calling the function twice. Just call it once like:
if(mail(...)) {
echo 'good times';
} else {
echo 'boo, no email was sent';
}
Secondly you are using the function incorrectly. According to the documentation here the mail function takes five arguments like so:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$additional_headers and $additional_parameters are optional as denoted by the [square brackets]. $to, $subject and $message are required in that order.
Thirdly I heavily suggest NOT using the built in mail() function.
I suggest using SwiftMailer. It's a fully comprehensive PHP library which will look after you.
U are trying to send mail twice you are using wrong variable names.
This code works for me.
<?php
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];
$to = "asdf#gmail.com";
$subject = "Site contact form";
$header = "From: ".$email."\r\n";
$header .= "Cc: ".$email."\n";
$header .= "Reply-To : ".$email."\r\n";
$header .= "Return-Path : ".$email."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
if(mail($to, $subject, $message, $header))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
}
?>
If you are usinf free hosting, they probably restrict your ability to send email.
Something like that is happening:
https://www.freehosting.com/client/knowledgebase.php?action=displayarticle&id=25
PHP mailing functionality is limited on free account to prevent abuse. To make it working, your mailing script should be configured to use SMTP server 'cpanel.freehosting.com' and to authenticate against it using credentials of email account set up in cpanel.
Paid accounts are provided with unrestricted access to PHP mailing functionality.
You can find more info on setting up email authentication in PHP scripts at this link:
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
Optionally, PHP mail() can be enabled by purchasing corresponding addon HERE.
i have the following code
$subject = "Subject Here";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Domain Name <domain#domain.com>' . "\r\n";
$to = $email;
$body = '
My Message here
';
mail($to, $subject, $body, $headers);
and it send mail correctly but when i see details in the email in gmail ...
it shows
from Domain Name
to myemail#myemail.com
date Tue, May 25, 2010 at 12:41 PM
subject my subject here
mailed-by mars.myhostingcompany.net
while i want to show my own address in mailed by section so that it should be mydomain.com instead of mars.myhostingcompany.net
There are two types of sender (From), the MIME header sender and the envelope sender.
You send the MIME sender with in the headers in the 4th parameter of the mail() function. You are doing this fine.
The enveloper sender (the one you can send when sending email through sendmail or a sendmail compatible wrapper) with the -f flag, is set in the 5th mail() parameter, additional_parameters, in the format you'd pass it on the command line: -femail#address.tld.
So your mail function would end up looking like this:
mail($to, $subject, $body, $headers, "-fdomain#domain.com");
I take it you're on shared hosting so the reason it shows your hosts email address is because when you configure PHP there is a setting called "sendmail_from" which is a default address to send mail through in the event that no address is provided in your code.
You appear to be specifying the proper headers in your code so I can only think of one possibility (that I can't test from this computer). Try removing the < > around your email address - it may be trying to read that as HTML and so you have nothing. This can occur on Windows machines because PHP itself parses the custom headers and not the MTA (message transfer agent) and PHP treats any < > as HTML.
I realize that it doesn't look as professional (since the email client won't show the name when it receives the email) but if you're running from a Windows machine there's very little else you can do unless you switch to an alternative mail package.
$subject = "Subject Here";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: domain#domain.com' . "\r\n";
$to = $email;
$body = '
My Message here
';
mail($to, $subject, $body, $headers);
//FORM PAGE:
<form method="POST" action="mailer.php">
<p>Please feel free to contact me on the form below or my direct email address: jkench#jasonkench.co.uk<br>
<br><br>
<br>
<br>
<br>
</p>
<table width="327" border="0">
<tr>
<td width="102">Name:</td>
<td width="215"><input type="text" name="name" size="19"></td>
</tr>
<tr>
<td>Company:
<label for="company"></label></td>
<td><input type="text" name="company"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" size="19"></td>
</tr>
<tr>
<td>Telephone No:
<label for="telephone"></label></td>
<td><input type="text" name="telephone"></td>
</tr>
</table>
<p><br>
Enquiry:<br>
<textarea rows="9" name="message" cols="65"></textarea>
<br>
<br>
<input type="submit" value="Submit" name="submit">
</p>
</form>
//PHP MAILER PAGE
<?php
if(isset($_POST['submit'])) {
//SEND TO
// Send the completed form to the below email address:
$to = "myemail#mydomain.co.uk";
//SUBJECT
// Subject of the email form:
$subject = "Jason Kench - Web Developer";
//NAME
//POST the details entered into the name box
$name = $_POST['name'];
//COMPANY NAME
//
$company = $_POST['company'];
//EMAIL
//
$email = $_POST['email'];
//TELEPHONE NUMBER
//
$telephone = $_POST['telephone'];
//MESSAGE/ENQUIRY
$message = $_POST['message'];
//Headers from a online site may help not sure
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//FROM EMAIL ADDRESS:
// Additional headers to change the FROM EMAIL ADDRESS
$headers .= 'From: Web-Contact-Form#mydomain.co.uk' . "\r\n";
// BODY
// This is the body of the message that will be sent to my email address with their details.
$body = "
You have received a message from the online contact form at http://www.jasonkench.co.uk\n
Details Below: \n \n
From: $name\n
Company: $company\n
$headers
Email Address: $email\n
Telephone No: $telephone\n
Message: $message\n";
// FORM SENT
// This will alert the customer the form has been successfully sent.
echo "Your details have been sent, I will contact you within 48 hours.";
// Use the mail function to email the following variables to my $to email address.
mail($to, $subject, $body, $headers);
} else {
// Display error message if there is a problem.
echo "Sorry there seems to be a problem. Please email me direct at: $to thank you.";
}
?>