I am trying to send an email using PHP I'm using this form:
<form class="pure-form pure-form-aligned" action="contact.php">
<fieldset>
<input type="Text" placeholder="Nome" name="Name">
<input type="email" placeholder="Email" name="email">
</fieldset>
<fieldset>
<button type="Reset" class="pure-button pure-button-primary">Limpar</button>
<button type="submit" class="pure-button pure-button-primary">Enviar</button>
</fieldset>
</form>
and this php code
<?php
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$mail_to = 'nelson.maia@geralucros.com';
$subject = 'GeraRelax: '.$field_name;
$body_message = 'De: '.$field_name."
";
$body_message .= 'E-mail: '.$field_email."
";
$headers = 'De: '.$field_email."
";
$headers .= 'Responder para: '.$field_email."
";
$mail_status = @mail($mail_to, $subject, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Obrigado pela contacto.');
window.location = 'index.php';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Envio falhado');
window.location = 'index.php';
</script>
<?php
}
?>
I have used it before but now it doesn't work. I'm guessing this have to do with server configurations? Can someone enlighten me on this?
You need to add the form METHOD!
<form class="pure-form pure-form-aligned" action="contact.php" METHOD="POST">
Also missing . to add to headers.
$headers .= 'De: '.$field_email."
";
Also type in NOME should be NAME
<input type="Text" placeholder="Nome" name="Name">
TYPO IN VARIABLE NAMES
$field_name = $_POST['name'];
$field_email = $_POST['Email'];
Add the form method here:
<form class="pure-form pure-form-aligned" action="contact.php">
Add the submit button name here:
<button type="submit" name="submit" class="pure-button pure-button-primary">Enviar</button>
Change the php code to this :
<?php if(isset($_POST['submit'])){
// your code here..
.
.
.
// change the mail headers..
$headers .= 'De: '.$field_email."
";
} ?>
Your problem is your have created mail
incorrectly, if you view the PHP manual: http://php.net/manual/pt_BR/function.mail.php // http://php.net/manual/en/function.mail.php
you will see that mail($to, $usbject, $message, $headers);
and your version does not do this, you have no $message
value in your mail item.
Also add
error_reporting(E_ALL);
ini_set('display_errors', 1);
To the top of your code to display error messages which will give you more feedback as to why your email sending is failing. And remove the @
from your mail function until you are sure it works.
You should have
error_reporting(E_ALL);
ini_set('display_errors', 1);
...
$mail_status = mail($mail_to, $subject, $body_message, $headers);
Use UTF-8 character set, or UTF-16 if possible.
Use a dedicated Mailer extension such as PHPMailer or Swift Mailer
Add error tracking (as exampled) across your code until you're sure it works.
Eat five fresh fruit and vegetables a day.
Also noted from PHP Mail:
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.