PHP mail() function is not sending - php

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' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$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."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$headers = 'De: '.$field_email."\r\n";
$headers .= 'Responder para: '.$field_email."\r\n";
$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."\r\n";
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'];

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);
Recommendation:
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.

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."\r\n";
} ?>

Related

Page Redirection action tag

I am writing '#' in the action tag of a form and then including a php script to perform action on click of submit button.
HTML code:
<form id="newsletter" method="post" action="#">
<div class="input-group">
<label class="sr-only" for="news">Full Name</label>
<input type="email" id="news" name="news" class="form-control" placeholder="E-Mail Address" required>
<span class="input-group-btn">
<button class="btn btn-default" type="submit" name="submit">Subscribe</button>
</span> </div>
</form>
<?php include "newsletter.php"?>
I want the index page to be redirected to a different page once the work is done. header[Location] doesnt seem to work as as it is giving an error of header already sent.
php code:
<?php
if($_POST){
$email = $_POST["news"];
$subject = "New Subscribe for Newsletter";
$message = "<b>From : ".$email."</b><br/>";
$message = "<b>Subscriber from mailnews: $email</b>";
$from = "contact#email.com";
$to = "mailnews#email.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: MailNews<'.$to.'>' . "\r\n";
$headers .= 'From: Contacted from <'.$from.'>' . "\r\n";
mail($to, $subject, $message, $headers);
?>
<?php }
?>
At the very top of your php include the function
<?php
Ob_start();
?>
Now you shouldn't get header already sent error. Make sure it's the first thing after your opening php tag.

Cannot POST PHP file for contact form?

So I have published my website, but am making minor improvements to it as time progresses. Just a note: I am impatient and this is making me want to obliterate myself. Help!
So the question is the title. I have a contact form on my website which uses HTML and PHP.
Here is the form on HTML:
<form action="contact.php" method="post">
Name<br>
<input type="text" name="cf_name"><br>
E-Mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea></br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
And here is the PHP for it:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'amir#arshak.co.uk';
$subject = 'ALERT! Somebody has viewed your website!'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thanks. I have recieved your message. I may reply to the given e-mail.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message failed to send. Send an email to amir#arshak.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
I can't figure out what is wrong with the code!
If the problem is that the email won't send...
You are allowing the from and reply-to addresses to be set to whatever is entered into the form.
A lot of mail servers will only allow an email to be sent where the from address matches the email address used to authenticate with the server.
This could possibly be causing your issue although it's hard to say without much more information.

How to trigger mail function when submit button clicked

I'm trying to make an application to send emails when a button is clicked. I tried to use isset function to do that. But my code doesn't work well. How do I fix it?
Demo below:
<!-- <button name="sendemail">send</button> -->
<input type="submit" value="send"/>
<?php
if(isset($_POST['Submit'])){
?>
<?php
// multiple recipients
// $to = 'aidan#example.com' . ', ';
// $to .= 'wez#example.com';
$to="example#outlook.com";
// subject
$subject = 'test01';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
?>
You can use the below code to fire an email on click of the Send Email button
<form action="" method="post">
<input type="submit" value="Send details to college" />
<input type="hidden" name="button_pressed" value="1" />
</form>
<?php
if(isset($_POST['button_pressed']))
{
$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);
echo 'Email Sent.';
}
?>
A couple of things to address here. First, your input button doesn't do anything, because it isn't a part of a form. So you either have to use some sort of JavaScript or just a HTML form to submit the button to, so we can process it in PHP. A simple form is done like this below. Note the metohd="POST", this allows us to gather the information from the $_POST-array in PHP later on (by default forms are set to use GET).
<form method="POST">
<input type="submit" name="submit" value="Send!" />
</form>
This form will send the data over the POST-array, to the same page it's placed on, so place your PHP here as well.
Secondly, you did not have a name attribute in your submit-button, and since PHP uses the names rather than the IDs (like JavaScript does), you need to add this. This can be seen in the code-snippet above.
Further, you never check if the mail was actually sent or not. You should never take anything for granted.
<?php
if (isset($_POST['submit'])) {
/* add headers, messages and such here */
// Check if the mail is being sent from the server
if (mail($to, $subject, $message, $headers)) [
// Mail was sent! Great!
} else {
// It failed sending the mail
}
}
?>
You'll need to set the name attribute for your input element, and the input element need to be in a form. Also, the $_POST['submit'] should be in lowercase to keep it consistent.
You should also check to see if mail() is sent successfully. mail() will return true when sent and false if not sent.
Just a note, the actual sending of mail will depend on your hosting/ server, even if it returns true, it does not necessarily mean that the mail is sent.
Find out more about mail() at http://php.net/manual/en/function.mail.php.
The following code should work:
<form method="post">
<input type="submit" name="submit" value="send" />
</form>
<?php
if(isset($_POST['submit'])){
// NO Change from your original code
if(mail($to, $subject, $message, $headers)) {
// You can change this to whatever you want PHP to do when mail is successfully sent
echo 'Mail successfully sent';
} else {
// You can change this to whatever you want PHP to do when mail failed to send
echo 'Mail failed to send';
}
?>
Hope this helps. Thanks!

Make PHP mail form

I've an HTML contact form and I want to use it in my footer, but I don't know how to do it!!
Here is my form:
<div class="footer-right">
<p>Contact Us</p>
<form action="#" method="post">
<input type="text" name="email" placeholder="Email" />
<textarea name="message" placeholder="Message"></textarea>
<button>Send</button>
</form>
</div>
The <button> tag will not submit the form. You should change it to: <button type="submit">Send</button>
For handling the form you could add something like this in the beginning of your file::
<?
//Mail sending function
$subject = "Web Form";
$from = $_POST['email'];
$to = "youremail#example.com";
//data
$msg = $_POST['message'];
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
mail($to,$subject,$msg,$headers);
echo "Mail Sent.";
?>
Since you tagged it as PHP, this answer will get you started.
Change:
<button>Send</button>
to:
<input type="submit" value="Send">
Change:
<form action="#" method="post">
to:
<form action="mailer.php" method="post">
Create a file named mailer.php and place it in the same folder as the filename of the document you presented, and in that file, add the following contents:
<?php
$recipient="receipient#example.com";
$subject="new message from ".$_POST['email']." - contact form";
$message=$_POST['message'];
mail($recipient, $subject, $message);
?>
<p>Your message has been sent.</p>
Note that you should add custom HTML between the last two lines of the above code or your HTML won't validate with W3C standards.

Form for sending mail not sending

I have a "tell a friend" pop up email form that allows users to share my page with an email address that they enter. It pops up fine, but I can't get the form to send the email.
html:
<div id="tellfriend">
Close
<form id='tellafriend_form' method="post" action="#sendMessage" name="tellafriend_form">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name" />
<label for="to">Friend's email:</label>
<input type="text" id="to" name="to" />
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" />
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div><!-- #tellfriend -->
javascript that handles the "pop up":
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script>
$(function() {
$('#tellfriend').hide();
$('#sendMessage').click(function(e) {
$("#tellfriend").fadeToggle('fast');
});
});
</script>
php that's supposed to send the mail:
<?
if (isset($_POST['Submit'])) {
// This will check to see if the form has been submitted
$senders_name = $_POST['name'];
// The person who is submitting the form
$recipient_friend = $_POST['to'];
// The forms recipient
$subject = $_POST['subject'];
// The subject line
$message = $_POST['message'];
// The message being sent
mail($recipient_friend, "From $senders_name", $subject, $message);
if (isset($_POST['your_email'])) {
echo "<br>Your friend has been contacted <br><br>Thank you $senders_name";
}}
?>
Disclaimer: PHP newbie, hoping to learn. Thanks!
The order of your parameters in mail function is not correct. see this
it should be
mail($recipient_friend, $subject, $message);
if you want to use headers then do this
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$recipient_friend.' <'.$recipient_friend.'>' . "\r\n";
$headers .= 'From: '.$sender.' <'.$senderEM.'>' . "\r\n";
Then call mail like this
mail($recipient_friend, $subject, $message, $headers);
You have one error in your PHP code:
if (isset($_POST['Submit'])) {
should be:
if (isset($_POST['submit'])) {
with a lowercase "s".
Indeed the name of you submit button is "submit" but the value is "Submit".
You could eventually do that:
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
And your mail parameters are not correct like boug said.
You have 2 errors
first:
if (isset($_POST['submit']))
// $_POST is case sensitive
second:
if (isset($_POST['your_email']))
// you dont have an inout named 'your_email'

Categories