Sending Email Through PHP - php

I'm trying to create a forum where a user completes it and places their email on the forum so I know who it came from. Once completed, I validate and send the contents of the forum to my email address but I'm not getting any emails from the site.
Here is my HTML
<article class="contact" id="contact">
<div class="contact-content">
<div class="span5 contact-info">
<!-- Contact title -->
<div class="contact-title">
<h2>Contact</h2>
</div>
<div class="contact-scroll">
<p>Address: 123 Baker Street</p>
<p>Phone Number: (555) 555-555</p>
<p>E-Mail: example#gmail.com</p>
<div class="class1" style="position: absolute; display: none;">
<div class="class2">
<div class="class3" style="position: absolute; top: 0px;" oncontextmenu="return false;">
<div class="class4" style="position:relative;"></div></div><div class="class5"></div></div></div>
<!-- contact form -->
<div class="contact-form" style="margin-top: 10px;">
<form action="" method="post">
<input style="width: 185px; float:left; font-size:14pt; color: white; b" type="text" name="your-name" id="your-name" value="" size="40" class="wpcf7-form-control-wrap your-name" aria-required="true" placeholder="Full Name">
<input style="width: 185px; float:left; font-size:14pt; color: white;" type="email" name="your-email" id="your-email" value="" size="40" class="wpcf7-form-control-wrap your-email" aria-required="true" placeholder="Email Address">
<input style="width: 374px; font-size:14pt; color: white;" type="text" name="your-subject" id="your-subject" value="" size="40" class="wpcf7-form-control-wrap your-subject" placeholder="Company Name">
<textarea style="color: white; width: 373px;" name="your-message" id="your-message" cols="40" rows="10" class="wpcf7-form-control-wrap your-message" aria-required="true" placeholder="Your Message"></textarea>
<input type="submit" id="sub" name="sub" value="Submit">
</form>
</div>
</div>
</div>
</div>
</article>
Here is my php
if(isset($_POST['sub']))
{
$name = $_POST["your-name"];
$email = $_POST["your-email"];
$companyName = $_POST["your-subject"];
$message = $_POST["your-message"];
$to = "sendto#gmail.com";
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$subject = $name." from ".$companyName." just emailed from online CV";
mail($to, $subject, $message, $headers);
echo "Mail Sent.";
}
The "if" statement is executing properly because the echo is outputted. I've also tried to pass the values to an alert box and all the input values are displayed in the alert box using this php code. The only problem is I can't get it to email the inputs to the email address I've specified in my php code. I'm running this site on 000Webhost free servers. Is the a script I have to include in my index.php to make the mail function work? Do I have to configure something inside the control panel on my 000Webhost account? Or is this simply just a syntax error?
Thanks

Alright I just managed to figure this out. To make this work on gmail, I had to add extra stuff to my headers.
$headers = "From: {$email}\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= "Return-Path: {$email}\r\n";
$headers .= "X-Mailer: PHP5\r\n";
$headers .= "Content-Transfer-encoding: 8bit\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "Importance: 0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= "Delivered-to: {$to}\r\n";
$subject = $name." from ".$companyName." just emailed you";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent.";
}
else
{
echo "Mail was not sent!";
}
These extra headers are needed to have mail sent to Gmail accounts. Everything else from my original code worked fine as is.

Related

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

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

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 not grabbing form information

I'm relatively new to PHP and I'm having trouble getting my PHP to take my form data and submit it in an email. The email sends to me with the plain text I have in it but all the form data isn't there. Does anybody have any ideas of what's wrong?
Here's the HTML:
<div id="box3" class="clearfix">
<form action="contact.php" method="post" enctype="text/plain">
<div id="greyContainer" class="clearfix">
<p id="text5">
Contact Us
</p>
<p id="text6">
Feel free to contact us and leave a message if you have any general questions!
</p>
<label id="formgroup">
<p id="text7">
Name:
</p>
<input id="name" type="text" value="Full Name" name="name"></input>
</label>
<label id="formgroup1">
<p id="text8">
Number:
</p>
<input id="phone_number" type="text" value="Phone Number" name="phone_number"></input>
</label>
<label id="formgroup2">
<p id="text9">
Email:
</p>
<input id="email" type="text" value="Email Address" name="email"></input>
</label>
<label id="formgroup3">
<p id="text10">
Message:
</p>
<textarea id="message_block" name="message_block" >Message or question...</textarea>
</label>
<p id="text11">
<span id="textspan">AVA Roofing & Siding<br />12 Arcade Ave.<br />Amherst, NY 14226<br />716.602.3947</span><br />
</p>
<img id="image1" src="img/envelope.png" class="image" />
<a href="thank_you.html">
<input id="input" type="submit" value="Submit" name="submit" ></input>
</a>
<div id="whiteLine" class="clearfix">
</div>
</form>
</div>
</div>
Here's the PHP:
<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$to = "example#gmail.com" ;
$from = $_POST['email'] ;
$name = $_POST['name'] ;
$headers = "From: $from";
$subject = "AVA Roofing Contact Form";
$message2 = $_POST['message_block'];
$number = $_POST['phone_number'];
$message = "A visitor has submitted the following requirements. \n\nName: $name\n\nEmail: $email\n\nNumber: $number\n\nMessage: $message";
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
$send = mail($to, $subject, $message, $headers);
if($send)
{header( "Location: http://www.example.com/folder/thank_you.html" );}
else
{print "We encountered an error sending your mail, please notify webmaster#YourCompany.com"; }
?>
You've got the wrong encoding on your form:
<form action="contact.php" method="post" enctype="text/plain">
^^^^^^^^^^^^^^^^^^^^^
Since you're telling the browser to send it as text/plain, it will not be processed by PHP. For this simple form, you don't need to specify ANY encoding. Just accept the defaults.
If you insist on specifying an encoding, it should be application/x-www-form-urlencoded for standard forms, or multipart/form-data if you're doing file uploads.
You can send to other file like file.php and in this file the action you would have something like this:
$recipiente = "your#email.com ";
$asunto = "Formulario";
$message ="name: ".$name."<br>";
$message .="email: ".$email."<br>";
$message .="coment: ".$coment."<br>";
$message = stripslashes($message);
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "From: $email\r\n";
$headers .= "Reply-to: $email\r\n";
$headers .= "Cc: $email\r\n";
mail($recipiente,$asunto,$message,$headers);

Mail is not coming properly using php?

I am using below simple code to send mail using php. when i echo the line $headers = "From: . $email\r\n"; //echo $headers; exit; which results like From: . test#email.com.
I found dot is printing before mail address , so mail is coming but not properly.If i remove dot , mail is not even coming.Whats wrong with this code ?
<?php
if (isset($_REQUEST['user_email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['user_email'];
$user_name = $_REQUEST['user_name'];
$user_message = $_REQUEST['user_message'];
include 'contact_mail_form.php';
$to = "testtest#gmail.com";
$message = $contactText;
$subject = "Test Mail";
$headers = "From: . $email\r\n"; //echo $headers; exit;
$headers .= "Content-type: text/html\r\n";
mail($to,$subject,$message,$headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
?>
<form id="contact_form" name="ContactForm" action="" method="post" onsubmit="return checkContact(this);">
<div class="wrapper">
<div class="fright">
<div class="name">Message:</div>
<textarea cols="1" rows="1" name="user_message" id="user_message" ></textarea>
<span class="clear"></span> </div>
<div class="fleft">
<div>
<div class="name">Your Name:</div>
<input type="text" class="input" name="user_name" id="user_name" />
<span class="clear"></span> </div>
<div>
<div class="name">E-mail:</div>
<input type="text" class="input" name="user_email" id="user_email" />
<span class="clear"></span> </div>
</div>
</div>
<div class="buttons"><a class="more" href="javaScript:void(0);" onClick="document.getElementById('contact_form').reset()">clear</a> <input class="more1" type="SUBMIT" class="button2" value="Send" name="" /> </div>
</form>
<?php } ?>
Try it like this your dot is acting as string not as concatenation
$headers = "From:" . $email."\r\n";
Maybe because \r\n should be surrounded with " not '
And Also
$headers .= 'Content-type: text/html'. "\r\n";

Categories