Emails sent by mail function is not being sent [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am trying to make a PHP script which sends mail from a html contact form, the script doesn't throw any error, but it doesn't send mails.
The code is below.
mail.php
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'stronka#obabie.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" .
"Here is the message:\n $message" .
$to = "bruno.kedzierski#wp.pl";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $email_subject, $email_body, $headers);
header('Location: index.html');
?>
My HTML file:
<div class="container" style="width: 200px; float: left; margin-left: 300px; margin-top: 25px">
<form action="mail.php" method="post">
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>Imie i nazwisko</label>
<input type="text" class="form-control" placeholder="Imie i nazwisko " name="name">
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>E-mail</label>
<input type="email" class="form-control" placeholder="E-mail" name="email" required>
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>Numer telefonu</label>
<input class="form-control" placeholder="Twoj numer" type="tel">
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto; width: 400px;">
<label>Wiadomosc</label>
<textarea class="form-control" placeholder="Twoja wiadomosc" style="height: 100px" name="message"> </textarea>
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>Plec
<select style="form-control">
<option value="chlop">Chlop</option>
<option value="chlop">Baba</option>
<option value="inny">Inna</option>
</select>
</label>
</div>
<div class="radio">
<label style="display: block;">
<input type="radio" name="optradio"> kradne</label>
<label style="display: block;">
<input type="radio" name="optradio"> nie kradne</label>
</div>
<button type="submit" class="btn btn-default" value="submit">Wyslij</button>
</form>
So I added attribute action="mail.php" and method="post", so that the PHP start when I press submit. Can anybody tell why it doesn't work?

First of all add this attribute in form tag
enctype="multipart/form-data"
then in php code try to send mail without header like this one
mail($to,$email_subject,$email_body);
if still you get error then try to print error like this one after mail function
print_r(error_get_last())

Sending mail isn't rocket science. A simple example that Just Works -
<?php
$to_address = "someone#example.com";
$from_name = "From Name";
$from_address = "no.reply#example.com";
$reply_to_name="Reply to name";
$reply_to_address="reply-to#example.com";
$subject = "Hello!";
$headers = "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $from_name <$from_address>\r\n";
$headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
$headers .= "Reply-To: $reply_to_name <$reply_to_address>r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
$headers .= "X-Mailer: Some PHP Script\r\n";
$message_body = "Message goes here. Be polite and wrap it every 70 lines
or so, otherwise some mail clients will display very long annoying
lines of text. There are functions that can do this automagically
for you ... or write you own.";
mail($to_address, $subject, $message_body, $headers);
?>
HOWEVER... the good old days of anyone being able to send mail anywhere from any machine running a script out there on the interwebz are over. Between not wanting to be seen as a source of spam, and not wanting to receive spam, dealing with SPF records and DKIM and ... it becomes a lot to deal with.
In order to really send mail and know it is your code that isn't working vs. the email system you need to have properly configured PHP and properly configured whatever mail server PHP is working with.
I see mail questions come up often enough, with no answers that have no concern for server configuration. I think I may set up a VM that has working mail to itself, post it somewhere, and post a self-answered question on setting up things for dev work when needing to work with mail()...

Related

Why won't my submit button send info to my desired email? (PHP)

I'm trying to have my submit button send name, email and message to my desired address. When I hit send, i get a page that says "This page isn't working - HTTP error 405."
Some notes: I'm using visual studio code and using their live server extension to test it. I have script tags in HTML linking it to my php file (not sure if this matters as it's already linked under form element?) The "contact.php" is my php file. I'm a noobie so I'm hoping I'm making a simple mistake.
// MY PHP
<?php
$userName = $_POST['name'];
$userEmail= $_POST['email'];
$message= $_POST['message'];
$to = "stackoverflowexample#gmail.com";
$body = "";
$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
mail($to,$body);
?>
// MY HTML
<form action="contact.php" method="POST">
<h2>Send Message</h2>
<div class="inputbox">
<input type="text" name="name" required="required">
<span>Full Name</span>
</div>
<div class="inputbox">
<input type="text" name="email" required="required">
<span>Email</span>
</div>
<div class="inputbox">
<textarea required="required" name="message"></textarea>
<span>Type your Message...</span>
</div>
<div class="inputbox">
<input type="submit" name="" value="Send">
</div>
</form>
If you write both HTML and PHP code in the same file then you need to save that single file as contact.php.
Also in PHP code Please set the condition to avoid warning like Undefined index
Also mail function must have at least 3 parameters 1. to 2. Subject 3. Body
See below code
<?php
// set condition
if(!empty($_POST)) {
$userName = $_POST['name'];
$userEmail= $_POST['email'];
$message= $_POST['message'];
$to = "stackoverflowexample#gmail.com";
$body = "";
$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
// set subject name as second param
mail($to,"test",$body);
}
?>

my website contact form being used to send spoof emails

My hosting provider has contacted me and said one of the sites I have designed is sending spoof emails. Done a little bit of research but I still don't really understand how/what are they are doing to send these spoof emails. However more importantly how should I approach this, would it help if I try and put one of these 'captcha' things in place on the contact form or should I change the code I have on my site. Which is shown below:
<?php
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "***";
$Subject = "Message to A R C Products";
$Name = Trim(stripslashes($_POST['Name']));
$Address = Trim(stripslashes($_POST['Address']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Message = Trim(stripslashes($_POST['Message']));
// prepare email body text
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Message = "
Name:$Name
Address: $Address
Telephone: $Telephone
$Message";
// send email
$success = mail($EmailTo, $Subject, $Message, $headers);
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
<h2><strong>Contact Us</strong></h2>
<form method="POST" action="contact.php">
<br/>
<p style="margin-top: 0;">Fields marked (*) are required</p>
<p style="margin-top: 0;">Your Email:* <br/>
<input type="text" name="EmailFrom">
<p style="margin-top: 0;">Name:* <br/>
<input type="text" name="Name">
<p style="margin-top: 0;">Address:<br/>
<input type="text" name="Address">
<p style="margin-top: 0;">Telephone:<br/>
<input type="text" name="Telephone">
<p style="margin-top: 0;">Message:*<br/>
<TEXTAREA NAME="Message" ROWS=6 COLS=40>
</TEXTAREA>
<p style="margin-top: 0;"><input type="submit" name="submit" value="Submit">
</form>
Take a look on filter_input to clean your input data. Also i would not use the email from the form as a from address.
$EmailFrom = filter_input(INPUT_POST,'EmailFrom', FILTER_SANITIZE_EMAIL);

php script not working when sending from an external domain? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to run a php script on a website contact form. It's probably worth mentioning it's from a website template I bought and I have designed the website using this. My html/css/php knowledge is 'absolute beginner level' hence why I am on here...
Below is the php script (this came with the template). However it's not sending email through to the recipient email address. I've been told it's because the script is trying to send email from an external domain (ie the email address of the website visitor) through the domainname.co.uk mail server, and it’s going to reject it - how can I edit this script so that it works?
This is the PHP script:
<?php
session_start();
$email_to = 'enquiries#bonnelhomes.co.uk'; // change with your email
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo "success";
}
else{
echo "failed";
}
This is the html for the contact form:
<form id="contact" class="row" name="form1" method="post" action="send.php" >
<div class="span4">
<label>Name</label>
<input type="text" class="full" name="name" id="name" />
</div>
<div class="span4">
<label>Email <span class="req">*</span></label>
<input type="text" class="full" name="email" id="email" />
<div id="error_email" class="error">Please check your email</div>
</div>
<div class="span8">
<label>Message <span class="req">*</span></label>
<textarea cols="10" rows="10" name="message" id="message" class="full"></textarea>
<div id="error_message" class="error">Please check your message</div>
<div id="mail_success" class="success">Thank you. Your message has been sent.</div>
<div id="mail_failed" class="error">Error, email not sent</div>
<p id="btnsubmit">
<input type="submit" id="send" value="Send" class="btn btn-large" />
</p>
</div>
</form>
Any help would be much appreciated. Many thanks in advance :o)
replace
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
with
$headers = "From: mail#yourdomain.com\r\n";
UPDATE
since it is a contact form don't forget to add your user's details to $message
$message = $name. "<br>" .$email. "<br>" .$message;
security tip, on this line:
$email_to = 'enquiries#bonnelhomes.co.uk';
has security problem. An attacker can modify the headers, message and use your server to send unlimited spam messages to victims.
so add this line for more security:
if (strlen($email_to) > 30 || $email_to !== 'enquiries#bonnelhomes.co.uk') {
exit("Bye Hacker!");
}
If you still have problems sending "from your server" you might let some real email-server do the work. There is some Framework you can use which is called PHP-Mailer.
To use it, you have to download the framework and to place it into your server. Using this, you might wanna use SMTP (login-information from some real email-account).
it would look like:
require './PHPMailer/PHPMailerAutoload.php';
$mailer = new PHPMailer;
here you configure your email account to send the emails from. look on your hosters help-files to find out what you need to use to log in successfully:
$mailer->isSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = 'smtp.strato.de';
$mailer->Username = 'your#sendingaccount.de';
$mailer->Password = 'xxxxxxx';
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;
$mailer->From = 'your#sendingaccount.de';
$mailer->FromName = 'Mr Tester';
here you configure your actual email, you want to send:
$mailer->addAddress('enquiries#bonnelhomes.co.uk','Mr Admin');
$mailer->Subject = 'this is a contactform email';
$mailer->AltBody = 'your text with bla and request');
if($mailer->send()){
echo 'yeah man!';
}else{
echo 'some error occured';
}
Normally this is not absolutly necessary but it helps on servers with sendingproblems or if your emails get blockt by spam blockers.

post to form to same page

Basically I am trying to re work a plugin so what I need to do is post the form data on the Cart.php form on the same page. Below is the set up I have but the $_POST info is not returning anything when email is sent:
Cart.php
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
</div>
</form>
<?php
//Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
?>
It sends the mail with nothing in it (or missing information) if you load it right away, and it's because of a specific reason.
As it stands, your code will send you an Email as soon as you load the page, so it's best to wrap your (PHP) code inside an isset() to work in conjunction with a (named) submit button, which seems to be missing in your originally posted question/code.
Plus, you have two undefined variables:
$mine and $flygt, so you'll need to define those to fit your needs.
I.e.: if(isset($_POST['submit'])) and <input type="submit" name="submit" value="Send">
Sidenote: It's best to check for empty fields, but that's another topic; see my footnotes.
Tested and working, and receiving all info and I've replaced your present mail function with if(mail($to, $subject, $emailBody, $headers)){...}
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
<input type="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
//Send the email
if(isset($_POST['submit'])){
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
// $mine = " Mine variable"; // replace this
// $flygt = " Flygt variable"; // replace this
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
// $send = mail($to, $subject, $emailBody, $headers);
if(mail($to, $subject, $emailBody, $headers)){
echo "Mail sent.";
}
else{
echo "Sorry, something went wrong.";
}
} // brace for if(isset($_POST['submit']))
?>
If you're still having problems:
Add error reporting to the top of your file(s) right after your opening <?php tag, which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Footnotes:
You are open to XSS attacks (Cross-site scripting).
Use the following (PHP) filter function: FILTER_SANITIZE_FULL_SPECIAL_CHARS
$name = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting.
To check for empty fields, you can add this below if(isset($_POST['submit'])){
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone'])){
echo "Fill out all the fields";
exit;
}
|| means "OR", which could very well be replaced by OR if you want, however || has precedence over OR.
This is a very basic method; there are other ways of accomplishing this, but you get the gist of it.
The issue here is that the mail is sent before the submit since you didn't test of the existence of $_POST variables, try something like this:
<form id='SimpleEcommCartCartForm' action="" method="post">
<input type="hidden" name="action" value="mailing"/>
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
<input name="submit" type="submit"/>
</div>
</form>
<?php
if(isset($_POST['mailing'])){
// Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
}
?>
Once the email is sent, you haven't told PHP to output anything to the page.
You could add something like echo 'Mail sent'; to the end of the PHP so if the script is being executed correctly then you'll know about it.
If the problem lies within the email not being sent at all, this may be a problem with your server and you should look for a tutorial on how to set up mail correctly. If you're using shared hosting, there are some companies that do not allow the use of the PHP mail() function so I would check with them first.
Amongst this I would recommend that you use something like PHPMailer, a full email library for PHP. Then you could configure for your emails to be sent from anywhere that supports SMTP (not sure if others are supported but there most likely is). PHPMailer will work with shared hosting.
Updated - Code that you can try
Try this out in replacement of $emailBody:
$emailBody = <<<EOT
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<h1>Pump Inquiry</h1>
<h3>From: $name<h3>
<h3>Phone: $phone<h3>
<p>Minetuff Parts $mine</p>
<p>Flygt Parts $flygt</p>
</body>
</html>
EOT;
NB: Some email clients only allow the use of inline CSS, so if you did decide to add anything to your <style> tag in the <head>, don't expect it to work very well or at all in some cases.
As someone has mentioned before, you'll also need to protect against cross-site scripting.

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