PHP code is sending emails to spam, how to prevent it? [duplicate] - php

This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 1 year ago.
I'm new to PHP and HTML working on a website both codes seem to be working.
I'm trying to edit an existing contact us form and send the form to a default email but it is getting to the spam folder.
Here is my HTML code part:
<div class="row-fluid">
<div class="span8" id="divMain">
<h1>Contact Us</h1>
<h3 style="color:#;"></h3>
<hr>
<!--Start Contact form -->
<form name="enq" action="contact-form-handler.php" method="POST" onsubmit="return validation();">
<fieldset>
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" maxlength="80" />
<input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" maxlength="80" />
<textarea rows="11" name="message" id="message" class="input-block-level" placeholder="Message" maxlength="1024"></textarea>
<div class="actions">
<input type="submit" value=" Send " name="submit" id="submitButton" class="btn btn-inverse pull-left" title="Click here to submit your message!" />
</div>
</fieldset>
</form>
<!--End Contact form -->
</div>
and here is my PHP code:
<?php
$name=$_POSt['name'];
$vistor_email=$_POST['email'];
$message=$_POST['message'];
$email_from='ex2#gmail.com';
$email_subject="New request to A-Akawi";
//$email_body="User Name:$name.\n"."User Email:$vistor_email.\n"."User message:$message.\n";
$body = <<<EMAIL
You have received a new request from $name with the following email address $vistor_email.
The following request is:
$message.
EMAIL;
$to ='ex1#gmail.com';
$headers .= "From: Johnson Smith <noreply#ksar.com> . \r\n" ;
$headers .='Reply-To: '. $to . "\r\n" ;
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($to,$email_subject,$body,$headers);
header("location: contact.html");
?>
I'm getting it to the spam even after adding the headers how to fix it?

As far as I know you also need to add the Return-Path: to the header of your e-mail.
But there could be many more reasons why your email is going to the spam.
The spf records in your DNS could be configurate wrong or the IP of your server is listed in spam filters

Related

PHP - Form Submit Doesn't Work [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I created a form styled with bootstrap and followed a PHP tutorial on how to make the form functional (when info is filled an email to be send to a specified email) but it doesn't work.
I have read a lot of threads on Stackoverflow but couldn't find a solution.
I configured my php.ini file (I think correctly) and still the mail() function doesn't work.
Below is my code. I am an absolute beginner in PHP and I really appreciate your help!
<?php
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
if (has_header_injection($name)||($email)) {
die;
}
if ( !$name || !$email || !$message ) {
echo '<h2 class="success">All fields are required</h2> Try again';
}
$to = "example#email.com"; (this has my true email)
$subject = "$name sent you a message via example's contact form";
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
mail($to, $subject, $message, $headers);
?>
<h2 class="success"> Thank you! Your project request is submitted. We will get back to you within 24 hours.</h2>
<p>« Go to home page</p>
<?php } else { ?>
<form action="" class="contact-form" id="contact-form" method="post" role="form">
<div class="form-group">
<label class="sr-only" for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" required="" placeholder="Name">
</div>
<div class="form-group">
<label class="sr-only" for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" required="" placeholder="Email">
</div>
<div class="form-group">
<textarea class="form-control" id="message" name="message" placeholder="Enter a brief description of your project" required=""></textarea>
</div>
<button type="submit" class="btn btn-custom" name="contact_submit">Send</button>
</form>
<?php } ?>
Are you working on localhost, if yes then email functionality is not going to work, put your code on server.

PHP Contact Form, not sending email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have been trying to make my first contact form and after hours of googling I can't seem to find the error in my code why it's not working.
My php code:
<?php
$to = 'llavert#gmail.com';
$from = strip_tags($_POST['email']);
$name = strip_tags($_POST['name']);
$adress = strip_tags($_POST['address']);
$city = strip_tags($_POST['city']);
$subject = strip_tags($_POST["subject"]);
$message = strip_tags($_POST['message']);
$header = "From: noreply#example.com\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header.= "X-Priority: 1\r\n";
mail($to, $subject, $message, $header);
print_r(error_get_last());
?>
My html form:
<form method="post" action="mailer.php">
<input type="text" class="col-md-13 col-xs-12 name" name='name' placeholder='Naam *'/>
<input type="text" class="col-md-13 col-xs-12 Email" name='email' placeholder='Email *'/>
<input type="text" class="col-md-13 col-xs-12 name" name='address' placeholder='Straat en huisnummer *'/>
<input type="text" class="col-md-13 col-xs-12 Email" name='city' placeholder='Postcode en gemeente *'/>
<input type="text" class="col-md-12 col-xs-12 Subject" name='subject' placeholder='Onderwerp *'/>
<textarea type="text" class="col-md-12 col-xs-12 Message" name='message' placeholder='Bericht aan BDS bvba *'></textarea>
<div class="cBtn col-xs-12">
<ul>
<li class="clear"><i class="fa fa-times"></i>Formulier legen</li>
<li class="send" name="submit" type="submit" value="Send"><i class="fa fa-share"></i>Bericht sturen</a></li>
</ul>
<br><br><br><br><br><br><br><br><br>
</div>
</form>
From the manual
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
It is most likely that your local mail server is not set up correctly.

I am not receiving mail php

I have a problem with receiving mail. I have tried different mail providers (microsoft, yahoo, gmail) and still not receiving mail. I am working on web site for my friend and I bought a bootstrap template with php contact.
I am using xampp and I have tried sending the mail that way and I have upload it on some other friends web site in sub folder and still not receiving it.
Here is the code:
HTML index.php
<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
<div id="msgInfo"></div>
<input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name">
<input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email">
<textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message"></textarea>
<button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>
Here is the contact.php code:
<?php
// PHP script for sending email
//
// Configuration
//
$toEmail = ""; // replace with your email where you would like to send email
$subject = 'Contact form from my website'; // replace with subject you want to receive
$body = 'You have received email from website:'; // replace with text that you want to receive in email
$from = ''; // replace with email that will look like sender
//
// ----- do not edit after this line if you don't understand what you are doing -----
//
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Invalid input";
return false;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body .= "\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message\n";
$headers = "From: $email\n";
$headers .= "Reply-To: $toEmail";
$res = mail($toEmail, $subject, $body, $headers);
echo "OK";
return true;
?>
This isn't my first time that I am working with php, in the past I had a working php(2-3 years ago) and I have tested that also but it doesn't work -.-
Can someone help me please.
You have to add name attribute for all input/textarea tags in your form so you can find them in your $_POST array
<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
<div id="msgInfo"></div>
<input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name" name="name">
<input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email" name="email">
<textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message" name="message"></textarea>
<button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>
and change this line in your php code ($ToEmail variable is always empty)
$res = mail($toEmail, $subject, $body, $headers);
with this
$res = mail($email, $subject, $body, $headers);
Try to replace with this
$body .= "\n";
$body .= "Name: ".$name."\n";
$body .= "Email: ".$email."\n";
$body .= "Messagee: ".$message."\n";
and change the headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = 'From: '.$name."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/' . phpversion();
Update
if($res){
echo "OK";
}
else{
echo "failed."
}
By this you can know whether your mail function in working or not.
Enable errors in php if not enabled using:
ini_set('display_errors',1);

Html contact form and php

I'm going to post my html form and my php and hopefully someone can help me make them actually work. My agenda is to make what the user inputs into the html be sent to my email, and also for them to receive an email back saying that we got their email.
I also wanted for the user to be send to a "thank you we got your email" page after hitting submit. The html is kind of long so forgive me for that.
<!-- contact --->
<div class="templatemo_caption">
<div class="clear"></div>
<p>Create any task you would like.</p>
<div class="clear">
</div>
<div align="center">
<div class="container">
<div class="row">
<div class="col-md-3">
<form action="sendmail.php" method="POST">
<form role="form">
<div class="form-group">
<input name="name" type="text" class="form-control" id="name" placeholder="Your Name" maxlength="30">
</div>
<div class="form-group">
<input name="email" type="text" class="form-control" id="email" placeholder="Your Email" maxlength="30">
</div>
<div class="form-group">
<input name="subject" type="text" class="form-control" id="subject" placeholder="Your Task" maxlength="40">
</div>
<br /> <br />
<input type="submit" value="Send Feedback" />
</div>
<div class="col-md-9">
<div class="txtarea">
<textarea name="subject" rows="10" class="form-control" id="subject"> Any specific details we need to know?</textarea>
</div>
</form>
</form>
</div>
</div>
</div>
<!--Contact End-->
AND THE PHP LOOKS LIKE THIS
<?php
/* Subject and email variables */
$emailsSubject = 'This is where you type what you subject will show up as';
$webMaster = 'chelsieoverbay#yahoo.com';
/* Gathering Data Variables - Whats in the form */
$name = $_POST ['name'];
$email = $_POST ['email'];
$subject = $_POST ['subject'];
$msg = $_POST['msg'];
/*Security*/
/* What You Want To See In The Email Place Inbetween $body = <<<EOD and EOD; */
$body = <<<EOD
<strong>Client:</strong> $name
<br />
<br />
<strong>Email:</strong> $email
<br />
<br />
<strong>Subject:</strong> $subject
<br />
<br />
______________________________________________
<br />
<br />
$msg
EOD;
/* Headers is a tag containing the users email and how you want it to display in your email */
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
/* This is what sends the email */
$success = mail($webMaster, $emailsSubject, $body, $headers);
/* Results Rendered as Html */
echo file_get_contents("studenthome.nku.edu/~overbayc1/onlinebutler/submit.html");
?>
Here's an example of how you could send two messages, one to your recipent and one to yourself.
//MESSAGE TO RECIEPENT ************************************************/
// multiple recipients
$to = $_POST["email"];
// subject
$subject = 'Your mail has been recieved';
// message
$message = '
<html>
<head>
<title>Your mail has been recieved</title>
</head>
<body style="margin: 0px;">
<p style="margin-bottom: 35px;">Dear '.$_POST["name"].',</p>
<p style="margin-bottom: 5px;">
Your email with the subject: '.$con_subject.' has been recived, you will be answered as soon as possible, possibly within the last 5 days.
</p>
<p>
- AthaxDesigns
</p>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: '.$_POST["email"] . "\r\n";
$headers .= 'From: AthaxDesigns <noreply#athaxdesigns.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
//MESSAGE TO MYSELF ************************************************/
// multiple recipients
$to = "support#athaxdesigns.com";
// subject
$subject = 'You have recieved an mail from '.$_POST["name"].' Subject: '.$con_subject.' ';
// message
$message = '
<html>
<head>
<title>You have recieved an mail from '.$_POST["name"].'</title>
</head>
<body style="margin: 0px;">
<p style="margin-bottom: 35px;">Name: '.$_POST["name"].' Email: '.$_POST["email"].',</p>
<p style="margin-bottom: 5px;">
'.$_POST["subject"].'
</p>
<p style="margin-bottom: 5px;">
'.$_POST["content"].'
</p>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: '.$_POST["name"].' <'.$to.'> '. "\r\n";
$headers .= 'Reply-To: ' . "\r\n" .
// Mail it
mail($to, $subject, $message, $headers);

Php mail not sending

I'm trying to send an email using php but it's not working..
This is my code:
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label> <input name="nom" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
</form>
<?php
$name= ($_POST["nom"]);
$mail= ($_POST["email"]);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:<nelson-book#outlook.fr>' . "\r\n";
$suject="Book";
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["text"];
if (isset($_POST['submit'])) {
mail("nelson-book#outlook.fr", $suject, $message, $headers);
echo" teste";
}
?>
I already used a code SUPER similar to this ant it totally worked.. Can the problem be from the server?
thanks in advance.
check out your SMTP server restriction when you send email, with another hosting domain like, "outlook.fr", somethime this is't allowed.

Categories