This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have the following php code on my HTML page:
<body>
<?php
if ($_POST) {
$message = 'Name :' . $_POST['name'] . "\n" .
'Email :' . $_POST['email'] . "\n" .
'Phone :' . $_POST['phone'] . "\n" .
'Date :' . $_POST['book'] . "\n" .
'Message :' . $_POST['message'];
mail('ruan#gmail.com', 'Contact-us Form', $message);
}
?>
And HTML as the following:
<form method="post" action="contact.php" onsubmit="return valid()" enctype="multipart/form-data">
<ul>
<li>
<p class="left">
<p>Name</p>
<input type="text" name="name" placeholder="John Doe" />
</p>
.....
</form>
Now i have changed the email address to check if that could be the problem but still, I don't receive any email.
I think you need to include headers for php mail function
<?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);
?>
check out this link http://php.net/manual/en/function.mail.php.
If still not working check your smtp setting for your server. If also not working then use phpmailer. Check out this link for phpmailer http://phpmailer.worxware.com/?pg=examples
Related
I want to have a page with some form inputs and with date picker i want to send these all fields to mail how to i do this with PHP?
You can do that by first getting what users typed in a form.
Then parse the input into a text file or html file that you can send using the mail() function in PHP.
for example for a "datepickerfield" field:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = $_GET['datepickerfield'];
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
You can access the form input fields in PHP over $_GET or $_POST. This depends on your form.
<form action="/mail.php" method="get">
or
<form action="/mail.php" method="post">
Here's an example:
HTML Form:
<form action="/mail.php" method="post">
<label for="fullname">Full name:</label>
<input type="text" id="fullname" name="fullname">
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
<input type="submit" value="Submit">
</form>
and in your php file :
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = 'The Birthday of '.$_POST['fullname'].' is: '.$_POST['birthday'];
mail($to, $subject, $message, $headers);
?>
Take a look at https://www.php.net/manual/en/function.mail.php
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.
I've written an simple PHP mail form but it refuses to run on one server but will on another. Both servers are running PHP 5.4 and other PHP progs run on the problematic server but mail will not send.
I've pared the mailer back to the simplest code but am stumped as why one server wont send the mail. Is there a configuration I need to activate or a test I can perform to diagnose the problem?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Simple PHP Mailer</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="sender_email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="subdata" value="Submit">
</form>
<?php
//Taken from: http://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script
if(isset($_POST['subdata'])){
$to = 'myname#gmail.com'; // this is your Email address
$from = $_POST['sender_email']; // this is the sender's Email address
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$subject = "PHP Email Form Test";
$message = $fname . ' ' . $lname . ' (' . $from . ') wrote the following:' . '\n\n' . $_POST['message'];
$headers = 'From: user#gmail.com' . "\r\n" .
'Reply-To: user#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Mail Sent. Thank you ' . $fname . ', we will contact you shortly.';
}else{
echo 'Mail Error';
}
}
?>
</body>
</html>
Any help would be appreciated, many thanks.
Kw
I'm trying to make a form where the user introduces data and it gets submitted to e-mail.
However, it gets me to a blank page (file.php page), and the e-mail is not sent.
HTML code:
<form action="./sendmail.php" method="post">
<div class="log">
<input type="text" id="name" value="name" name="studname" class="form">
<input type="email" id="mail" value="mail#gmail.com" name="studmail" class="form">
<input type="tel" id="age" value="age" class="form" name="studage">
<input type="submit" value="submit!" id="submit">
</div>
</form>
And now here is my "sendmail.php" code:
<?php
/* STUDENT PARAMETERS */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
mail('code#gmail.com', 'Message',
'NAME: ' . $studentName . '\nMAIL: ' . $studentMail . '\nAge: ' . $studentAge);
?>
How can I solve this and why is this not working? Thank you for your time.
I think you can use phpmailer class
#Telmo Vaz try this
<?php
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$recipient='code#gmail.com';
$subject="Message";
$content = "New Message Form Contact us\n From: ".$studentName .", \n Email: ".$studentMail .",\n Age: ".$studentAge;
$headers = 'From: code#gmail.com' . "\r\n" .
'Reply-To: code#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval=mail($recipient, $subject, $content, $headers);
if( $retval == true ){do something;}
else{echo "Error Sending Message";}
?>
You need an email address to send the email from. The email won't be send from this address, but the selected address will be shown in the "From" section. Try this:
/* STUDENT PARAMETERS */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$to = "code#gmail.com";
$subject = "Message";
$message = "NAME: " . $studentName . "\nMAIL: " . $studentMail . "\nAge: " . $studentAge;
$from = $to;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
I have a contact form made with php on my website. The problem is that it perfectly sends english letters but doesn't support russian letters. So, I need to change the encoding, how do i do that?
Here is a code:
<div id="center">
<p class="please">Please contact us using this form.</p>
<div id="formbox">
<?
if (isset ($_POST['message'])) {
$name = # trim ($_POST['name']);
$contact = # trim ($_POST['contact']);
$message = # trim ($_POST['message']);
if (! $name or ! $contact or ! $message) echo ('<p style="color: red">You should fill in all the blanks.</p>');
else { mail ("support#myemail.com",
"Message from Giftosite (Sender: $name)",
"$message \n\n Reply to: \n $contact");
echo ('<p style="color: green">Message has been sent, thank you!</p>');
$_POST['name'] = $_POST['contact'] = $_POST['message'] = '';
}
}
?>
<form method="POST" class="form">
<p class="formcontent">Your name:</p>
<input name="name" value="<?=#$_POST['name'];?>">
<p class="formcontent">Your e-mail address:</p>
<input name="contact" value="<?=#$_POST['contact'];?>">
<p class="formcontent">Message:</p>
<textarea name="message" rows="6" cols="36"><?=#$_POST['message'];?></textarea>
<p><input type="submit" value=" Send "></p>
</form>
</div>
</div>
Set headers in your email
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Content-type: text/html; charset=UTF-8;' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
So, in your code instead of this
mail ("support#myemail.com", $message from Giftosite (Sender: $name)", "$message \n\n Reply to: \n $contact");
You will have
$headers = 'From: webmaster#example.com' . "\r\n" .
'Content-type: text/html; charset=UTF-8;' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail ("support#myemail.com", $message from Giftosite (Sender: $name)", "$message \n\n Reply to: \n $contact", $headers);
You need to set header for your email before sending Unicode characters, try this
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
mail ("support#myemail.com",
"Message from Giftosite (Sender: $name)",
"$message \n\n Reply to: \n $contact", $header_);