I've tried out a few PHP contact form tutorials but none seem to work for me. I'm not sure what I'm doing wrong. I tested it in localhost and nothing, so I went ahead and hosted it to see if that would work but still nothing.
HTML
<form class="form" action="form_process.php" method="post" name="contact_form">
<p class="name">
<label for="name">Name</label><br>
<input type="text" name="name_first" id="name" placeholder="First" />
<input type="text" name="name_second" id="name" placeholder="Last" />
</p>
<p class="email">
<label for="email">Email</label><br>
<input type="text" name="email" id="email" placeholder="mail#example.com" />
</p>
<p class="text">
<label for="email">Comments</label><br>
<textarea name="text" placeholder="Write something to us" /></textarea>
</p>
<p class="submit">
<input type="submit" value="Send" />
</p>
</form>
form_process.php
<?php
$name_first = $_POST['name_first'];
$name_second = $_POST['name_second'];
$email = $_POST['email'];
$text = $_POST['text'];
$from = 'From: ';
$to = 'EMAIL HERE';
$subject = 'Hello';
$body = "From: $name_first\n $name_second\n E-Mail: $email\n Message:\n $text";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html");
echo '<p>Your message has been sent!</p>';
exit;
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Your error is from the line
if ($_POST['submit']) {
This is because you did not give your submit button a name of submit. If you fix this line in your HTML it should fix the issue:
<input type="submit" name="submit" value="Send" />
I recommend that you set an error log in your php.ini file. That way you can see the error for yourself which would have said something similar to:
PHP Notice: Undefined index: submit in
/var/www/pwd/blah/form_process.php on line 12
If you are working in localhost mode you will need phpmailer.
First you need download phpmailer from here https://github.com/PHPMailer/PHPMailer/archive/master.zip
Then paste in your folder. If my coding doesn't clear you, you can check from
https://github.com/PHPMailer/PHPMailer
<?php
require 'PHPMailerAutoload.php'; // Your Path
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // Your mail
$mail->Password = 'secret'; // Your mail password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//Check Condition
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Second way.
If you working testing in online mode(Have own domain and hosting), you can just randomly copy and paste.
Doesnt required phpmailer.
if(isset($_POST['email'])) $email = $_POST['email'];
else $email = "";
function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$myname."\" <".$myemail.">\r\n";
return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers));
}
if(isset($Submit) && $Submit=="Go") {
$emailContent ='';
$sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent);
if($sent) {
echo $emailContent;
header('Location: contact.php');
}else{
echo "Failed";
exit;
}
}
?>
Regards
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
HTML Form with Sendemail
(2 answers)
Closed 5 years ago.
I have only dabbled in PHP a little, I am trying to remember what I did last year. I am having trouble getting this to work - the user is redirected to index.php but then nothing happens. No email is recieved and no verification 'email sent/not sent' etc.
I am sure it is probably a silly mistake.
Any help would be appreciated.
contact.html
<form method="post" action="index.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
index.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email']; // GET EMAIL ADDRESS FROM FORM
$to = 'annie.palmer#outlook.com';
$subject = 'Website enquiry from' .$name;
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: Annie<$from>\r\nReturn-path: $from" . "\r\n";
$headers .= "Content-type:text/text;charset=ISO-8859-1";
?>
<?php
if ($_POST['submit'] && ($_POST['human'] == '4') {
/* Anything that goes in here is only performed if the form is submitted */
if (mail ($to, $subject, $body, $headers, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I'm posting this as a community wiki; I don't feel any rep should come of this, nor do I want rep.
mail() uses 4 arguments, not 5.
http://php.net/manual/en/function.mail.php
There is a 5th but it doesn't do what you're expecting your 5th to do.
So remove the , $from from this:
if (mail ($to, $subject, $body, $headers, $from))
^^^^^^^
The From: belongs in the header and expects an email address.
Example from the manual:
<?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);
?>
Try using the PHPMailer. Once uploaded to your site sending an email is easy.
require ('../mail/PHPMailerAutoload.php');
require ('../mail/class.phpmailer.php');
//set up your email
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tbs'; //set as required
$mail->Host = "enter your email host here";
$mail->Port = 25;
$mail->Username = "enter your mail account username here";
$mail->Password = "enter your mail account password here";
$mail->From = "the from email address";
$mail->FromName = "the from name";
$mail->Subject = "the subject goes here";
$mail->IsHTML(true); //your choice
$body = "the body of you email - html or plain text";
$mail->Body = $body;
$mail->AddAddress("add the email address(es) of recipients");
$mail->Send();
I have php website when try to submit contact form Post method not working. All of form elements inside of index.html i'll share the codes with you. I'll be happy if you could help me
In HTML File (index.php)
<form id="main-contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="İsim Soyisim" required>
</div>
<div class="form-group">
<input type="email" name="mail_adress" class="form-control" placeholder="Mail Adresi" required>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Konu" required>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="8" placeholder="Mesaj" required></textarea>
</div>
<button type="submit" name="send" class="btn btn-primary">Mesaj Gönder</button>
</form>
in PHP (sendemail.php)
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xx#xxxxx.com'; // SMTP username
$mail->Password = 'xxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587; //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('xxx#xxxxxx.com', 'xx xx xx'); //Set who the message is to be sent from
$mail->addAddress('xxx#xxxxx.com', 'xx xx xx xx'); // Add a recipient
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = 'Mesaj Konusu'.$_POST['name'];
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
echo $_POST['subject'];
exit;
}
echo 'Message has been sent';
?>
Thank You
Try to compare with the === operador, to compare not only the same value, but also the same data type.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
I found the answer ajax prevent to POST method when i fix that everthings are became good
var form = $('#main-contact1-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Mail Gönderiliyor...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Mesajınız başarı ile iletilmiştir. En kısa sürede tarafınıza dönüş yapılacaktır.</p>').delay(3000).fadeOut();
});
});
when i delete this code everythings are good. How to fix that without delete it..? That's a new question to ask...
<?php
function sendmail($to,$subject,$message,$from)
{
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers = "From: " . $from. "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
}
?>
you write ==POST(capital letters), and use method = post(small letters), so change it to small or capital in both places. or check sendmail.php
i have this form:
<form action="/sendemail.php" id="main-contact-form" method="post" name="contact-form" role="form">
<div class="form-group">
<input class="form-control" id="name" name="name" placeholder="put your name" required="" type="text" />
</div>
<div class="form-group">
<input class="form-control" id="email" name="email" placeholder="Email" required="" type="email" />
</div>
<div class="form-group">
<input class="form-control" id="subject" name="subject" placeholder="Subject..." required="" type="text" />
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Mensaje" required="" rows="8"></textarea>
</div>
<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Enviar" />
and this is sendemail.php:
<?php
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message\n $pos";
mail ($to, $subject, $body, $from)
?>
the email is arrive empty, i try a lot of stuffs but always comes in the same way, is like the form can´t pass the data from inputs to php variables. im new in php so, any help is welcome.
thanks.
The use of mail() function is so insecure and is a vector attack to send massive spam, don't use anymore, for this reason is better to use PHP Mailer libray.
Create a EMAIL account (example: no-reply#domain.com) in your hosting and after that install this library PHP Mailer, now you can do this:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
UPDATE
After some research I've found that:
You need to ask godaddy technical support to enable "sendmail".
PHP mail() not working on GoDaddy
----------
I've tested your code and it works. I've received the email with ALL the $_POST contents without any problems.
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
NOTE:
1 - $pos = $_POST; is an array and it will be displayed as Array.
2 - Emails sent via mail() will most likely end-up on the spam folder (read the comments below).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have created an HTML form, and some part of a PHP script. Look below.
I need, somehow, to send the form, using the mail function. How do i do this?
HTML:
<form id="kontaktForm" action="Scripts-ContactForm/recieving.php" method="POST">
<div id="kontaktFormVenstre">
<div id="inputFields_container">
<div id="inputField_container">
<input class="inputField" type="text" name="name" placeholder="Dit navn" />
</div>
<div id="inputField_container">
<input class="inputField" type="text" name="email" placeholder="Din E-Mail" />
</div>
</div>
<div id="inputFieldMessage_container">
<textarea class="inputFieldMessage" name="message" placeholder="Din besked her" rows="8" cols="20"></textarea>
</div>
</div>
<div id="kontaktFormHøjre">
<div id="button">
<input type="submit" name="submit" value="Send mail" />
</div>
</div>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Fra:'$email;
$to = 'MyEmail';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
You can send mail like this. Use PHP mail() function.
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Fra:'$email;
$to = 'MyEmail';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail($to,$subject,$body,"From:$from");
}
?>
Your better off using something like PHPMailer, you can download it from: https://github.com/PHPMailer/PHPMailer
There are many examples to help you get started, but using this you can generate HTML emails and HTML alternatives ( for clients that cant read html )
example from github:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
as an alternative you can use PHP's mail() function and as said above its as simple as using:
if(mail('toemail', 'subject', 'message', 'headers')) {
echo 'Mail has sent';
} else {
echo 'Mail not sent';
}
http://php.net/manual/en/function.mail.php
Try this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'MyEmail';
$subject = 'Hello';
$headers = 'From: '.$name.' <' . $email . ">\r\n" .
'Reply-To: '.$name.' <' . $email . ">\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail($to, $subject, $body, $headers);
I am seriously struggling and have tried so many different methods to get my form to attach a file to the email sent through PHP.
This is the HTML form:
<form id="form1" enctype="multipart/form-data" action="submit/applicationscript.php" method="post" name="form1">
<input checked type="radio" name="school" value="English Martyres"/>
<input type="radio" name="stop" value="stop1" />
<input type="radio" name="stop" value="stop2" />
<input type="radio" name="stop" value="stop3" />
<input type="radio" name="stop" value="stop4" />
<input type="checkbox" name="mon" value="Monday" />
<input type="checkbox" name="tue" value="Tuesday" />
<input type="checkbox" name="wed" value="Wednesday" />
<input type="checkbox" name="thu" value="Thursday" />
<input type="checkbox" name="fri" value="Friday" />
<input type="text" class="text" name="name" required placeholder="First Name" /></div>
<input type="text" class="text" name="surname" required placeholder="Surname" /></div>
<input type="text" class="text" name="dob" required maxlength="10" placeholder="Date of Birth" />
<input type="file" name='uploaded_file' required />
<input type="submit" id="form1" name="submit" value="Submit" onClick="document.form1.submit()">
</form>
This is the PHP:
<?php
// Read POST request params into global vars
$to = $_POST['my#email.com'];
$from = $_POST['from#email.co.uk'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
// Send the message
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
It either sends and does not attach an attachment or it is unable to send or the $message is empty and therefore does not send.
Cannot anyone please help me, really need this to work and don't have a great knowledge of PHP, have looked at so many articles but cannot get my head around it.
I use PHPMailer for this and I haven't had any issues.
Get it here: https://github.com/PHPMailer/PHPMailer
Here is an example:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}