PHP mail form problems - php

I'm creating a simple mail form with checkboxes, a couple of input tabs, and a text input section. It uses PHP to retrieve the information then email it to a specific email. Right now I have it emailing to my own yahoo email just for testing. When I test the site on my hosting account jacobbuller.com/testsites/peacock/contact.php the form works perfectly and forwards the email from my generic "theski" server email. But when I upload the site to the actually live hosting account for peacockautoservice.com the contact form works - it executes and sends a ?msg=1 variable in the url - but I never receive the email in my Yahoo account...
Here's the PHP I am using to send the email.
<?php ob_start();
<?php
$required_field = array('name', 'email', 'message');
foreach($required_field as $fieldname) {
if (!isset($_POST[$fieldname])) {
$errors[] = $fieldname;
}}
if (empty($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$phone = $_POST['phone'];
$services = IsChecked('checkboxes', 'factory');
$services .= IsChecked('checkboxes', 'timing belt');
$services .= IsChecked('checkboxes', 'brakes');
$services .= IsChecked('checkboxes', 'computerized');
$services .= IsChecked('checkboxes', 'steering and suspension');
$services .= IsChecked('checkboxes', 'heating and air');
$services .= IsChecked('checkboxes', 'electrical');
$services .= IsChecked('checkboxes', 'other');
$body = "Customer:" . $name;
$body.= "Phone Number:" . $phone;
$body.= "Contact:" . $contact;
$body.= "Services:" . $services;
$body.= "Message:" . $message;
$to = "jcbbuller#yahoo.com";
$subject = "Peacock Auto Services Inquirey";
$from = $email;
$mailed = mail($to, $subject, $body, $from) or die("Error!");
}
if($mailed) {
redirect_to("../contact.php?msg=1");
}
?>
<?php
// IsChecked FUNCTION - Detemines what checkbox are/aren't checked on form.
function IsChecked($postname, $value){
if(!empty($_POST[$postname])) {
foreach($_POST[$postname] as $job) {
if ($job == $value) {
$project = " ". $value . " ";
return $project;
}
}
}
} //END IsChecked FUNCTION
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
?>
<?php ob_end_flush(); ?>
Please let me know if you see something wrong with the PHP above or if you know why their GoDaddy hosting account is not executing the email. I tried using their customer service network but they said that they can't help me with my code...

Your issue is mainly at server end. Mail function is working because of your check on it, if it had failed, it would have given you notification. So, mails are going definitely. If mail server is working properly at your production server, then check for SPAM folder at yahoo mail server. I would suggest you to ask your hosting provider to enable SPF and DKIM records because most of email providers requires sender authentication (if it is not a spam) and these records are helpful in it.
I can also see that your not using any headers, so I would suggest you to use extended headers to avoid providers identifying you as a spammer. I use below mentioned headers and my emails never go in spam on anyprovider, but again it depends on IP reputation of the server as well.
$headers .= "Reply-To: Awaraleo <email#domain.org>\r\n";
$headers .= "Return-Path: Awaraleo <email#domain.org>\r\n";
$headers .= "From: Awaraleo <email#domain.org>\r\n";
$headers .= "Organization: Awaraleo\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
and then use it like
mail($to,$subject,$message,$headers);
Here email#domain.org should be a valid email address created on the domain where this form is implemented.
Another authentic way is to use SMTP Authentication in coding.

Related

Not receiving mails php mail outlook

I have a problem with the dynamic sending of emails from my site
Customers who have hotmail, live emails do not receive emails (which are in bcc receive emails) as well as some customers with gmail for example receive them but in spam and others in main. The code I use
function emailDemande($email, $nameL){
if (!preg_match("#^[a-z0-9._-]+#(hotmail|live|msn).[a-z]{2,4}$#", $email))
{
$passage_ligne = "\r\n";
}
else
{
$passage_ligne = "\n";
}
$subject = 'Votre demande d\'expérience mystère';
$headers = "From: site <contact#site.com>".$passage_ligne;
$headers .= "Bcc: compte#gmail.com".$passage_ligne;
$headers .= "MIME-Version: 1.0".$passage_ligne;
$headers .= "Content-Type: text/html; charset=UTF-8".$passage_ligne;
$message = '<html><body><p>Cher(e) '.$nameL.',</p></body></html>';mail($email, $subject, $message, $headers);};
I assume you are using Cpanel hosting.
What you want to do is.
Go to Cpanel > MX entry > Select Domain name in question > Select Remote Mail Exchanger.
Thats it.

PHP Mail to SMTP

I currently have a piece of PHP script that sends an email from a user inputted email. This was being identified by GMail as a spam email because it spoofed the email. I would like to convert my script so that it does exactly the same things but through an SMTP email.
<?php
ob_start();
include('mplookup.php');
ob_end_clean();
$email = $_POST['emailfrom'];
$human = $_POST['human'];
$text = $_POST['text'];
$address = $_POST['address'];
$city = $_POST['city'];
$footer = '<br><em><strong>Disclaimer: "Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of EG4DEMUK. EG4DEMUK will not accept any liability in respect of defamatory or threatening communication. If you would like to raise a complaint about an email sent using our tool, please contact us at ".</strong></em><p>-----------------------------------------------------------</p>
The ERC is an organisation that brings together Egyptian citizens and movements abroad, irrespective of their political or ideological affiliations. We share in common a belief in the principles of the January 25th Revolution and oppose all aspects of corruption and dictatorship in Egypt. We believe in constitutional legitimacy and work for the establishment of a civil state that reflects the will of the Egyptian people and their freedom in choosing their government.</p>';
$postcode = $_POST['postcode'];
$name = $_POST['flname'];
$message = $text.$name."<br />".$address."<br />".$city."<br />".$postcode."<br />".$footer;
$to = "";
$subject = 'Sisi\'s visit to the UK: Sent using the ERC\'s Tool ';
$headers = "From: ".$email."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\r\n";
$headers .= "BCC: \r\n";
$headers .= "CC: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
?>
<?php
if ($_POST['submit'] && $human == '4')
{
if (mail ($to, $subject, $message, $headers)) {
echo '<p>Your message has been sent! Thank you for participating in EG4DEMUK\'s campaign.</p>';
}
else
{
echo '<p>Something went wrong, please go back and try again!</p>';
}
}
?>
I have no clue how to proceed in converting this to SMTP. Any help would be much appreciated.
You can use a pre-written PHP library for this.
https://github.com/PHPMailer/PHPMailer
The GitHub page includes a very good example script and you can simply copy your already written parameters to the corresponding variables of the library.
For example:
$to = "xxx#outlook.com"; becomes $mail->addAddress('xxx#outlook.com');
$mail->Body = $message;
etc.

Php mail. Can't get email on one address

I have a problem with my mail in php. I code form to send email. I receive email on gmail but I have other mail address and I can't get email on it.
I checked in spam and there is no email also.
Below is my code.
<?php
$emailErr = "";
$endMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Proszę uzupełnić pole e-mail";
}
else if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$namesurname = $_REQUEST['name_surname'] ;
$email = $_REQUEST['email'] ;
$number = $_REQUEST['number'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$message = $subject . ": " . $message . " " . $number . " " . $namesurname . " " . $email;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
mail("szafor#szafor.pl", "Zamówienie pomiaru",
$message, "From: formularz#szafortest.pl \r\n"."Content-Type: text/plain; charset=UTF-8\r\n");
$endMessage = "Dziękuję za przesłanie wiadomości.";
}
}
?>
One important thing to consider with sending mail is that you should at least have the return path of the message be an email address that is actually hosted on the server that you are sending from.
You can set the From and the Reply-To address as any address, but the return-path should be set to a valid email address hosted on your server. Let's say that you want the "reply" button to send back to "this_email#wherever.com" but the server you are using hosts email for "mydomain.com". Create an email account on your server, "info#mydomain.com" for example.
$recipient = "sendto#email.com";
$subject = "Test email";
$message = "This is the message.";
$headers .= "From: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Reply-To: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Return-Path: Your Name Here <info#mydomain.com>\n\r";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .="X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .="MIME-Version: 1.0\r\n";
mail($recipient, $subject, $message, $headers);
I have found that the more valid header information that I provide, the more likely the email will be delivered. Right now these headers always work for me, and I have a scheduling program that is sending email to a hundred different email addresses every day. See if that works better for you.

how to Send Email details notification using php?

How can I send email notification to my users who gave registration details in my form using php?
I have code which runs perfectly to get emails to me, but now I also want to same details to my users.
I am trying to use "$from" into my array at "$to" but getting no email.
my mail.php
<?php
$subject = "Email Notification";
$message = "Thank you for your email";
$message = "Your Registering details are as follows:";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name</td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email</td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = $_POST['email'];
$to = array('my_address#example.com', 'my_address2#example.com', $from);
$lp = "notification#example.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$lp .'' . "\r\n" .
'Reply-To: '.$lp.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
It's pretty common that on shared hosting you need to send an email from an "existing email", at least match the domain. Maybe that's why the emails are not being sent?
So for example, if your domain is "www.my-awesome-domain.com", you can't send email headers
"from: example#example.com"
Instead, make sure to send emails from your domain, and ideally an existing email box, for example:
"from: office#my-awesome-domain.com"
Hope it helps! :)

PHP mail() form sending to GMAIL spam [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 2 years ago.
I know this problem has been addressed a few times on here. I tried following the directions for setting proper headers, I still run into problems with my emails going into the spam filter in Gmail.
If anyone can please take a look at what I've tried, I'd really appreciate it. The code below is without the headers added as explained here: http://www.velvetblues.com/web-development-blog/avoid-spam-filters-with-php-mail-emails/
Thanks in advance.
define("WEBMASTER_EMAIL", 'myName#mydomain.com');
if($post)
{
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
$error .= 'Name required! ';
// Check email
if(!$email)
$error .= 'E-mail required! ';
if($email && !ValidateEmail($email))
$error .= 'E-mail address is not valid! ';
// Check message
if(!$message)
$error .= "Please enter your message!";
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
echo 'OK';
}
else
echo '<div class="errormsg">'.$error.'</div>';
}
Use this code :
$to = Email;
$subject = subject ;
$body = "<div> hi hi .. </div>";
$headers = 'From: YourLogoName info#domain.com' . "\r\n" ;
$headers .='Reply-To: '. $to . "\r\n" ;
$headers .='X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
if(mail($to, $subject, $body,$headers)) {
echo('<br>'."Email Sent ;D ".'</br>');
}
else
{
echo("<p>Email Message delivery failed...</p>");
}
I think this is your issue:
"From: ".$name." <".$email.">\r\n"
since you are not gmail, hotmail or your users email provider, you cannot have "From: otherdomain.com" and then deliver the mail via "mail.yourdomain.com" - this will most likely move your mail to the spam folder.
Try
"From: YourWebsiteName <noreply#yourwebsite.com>\r\n"
."Reply-To: ".$name." <".$email.">\r\n"
instead.
ALso: your code is very unsave and a prime spam target - google "email header injection php"!
Google tends to discipline not only the websites but also service providers who used to have many users spamming across the network.
If you are signed up to one of these service providers that google recognizes as spammers this might be the reason why your php mail() messages drops in to spam box in gmail. Try to chat about this issue with your server provider.
In that case you will get a warning from google in your "spam" message:
"Why is this message in Spam? We've found that lots of messages from
home.pl are spam. Learn more"
I know this question has been asked ages ago, but I thought I'd drop a 2020 answer here so that it can potentially help new visitors.
Please note:
This answer serves as a generic answer and will require you to edit some of the details according to form inputs that you are using.
You also need to update the email address in the headers etc. to the one connected to your domain.
This solution assumes you're using Google Recaptcha. If not, then just delete the part about "Google recapthca".
This script has added security and validation that shouldn't be removed.
If you're going to use Sweet Alert then you should install it into your website/app, either via CDN or NPM.
Some Javascript to create custom Sweet Alert alerts that trigger on mail send:
// Custom SweetAlert alert that gets triggered on email send
function enquirySent() {
swal({
title: "Email sent!",
text: "Thank you for your email. We'll be in contact ASAP.",
icon: "success",
button: "Okay",
});
}
function enquiryNotSent() {
swal({
title: "Oops!",
text: "Your email was NOT sent due to an error.",
icon: "error",
button: "Okay",
});
};
The PHP script to send the mail:
<?php
if (isset($_POST['submit'])) {
// For the Google recaptcha
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data = [
'secret' => 'YOUR CAPTCHA SECRET KEY',
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
// Google recaptcha end
// Form details (sanitized)
$name = htmlspecialchars($_POST['name']);
$surname = htmlspecialchars($_POST['surname']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Mail headers and details
$email_from = 'youremail#yourdomain.com';
$email_body = "You have received a new message from the user $name $surname.\nHere is the message:\n\n".$message;
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\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" ;
$error = false;
// Some more input validation/sanitizing
if (!preg_match("/^[a-zA-Z ]*$/",$first_name) && $first_name!="") {
$error = true;
}
if (!preg_match("/^[a-zA-Z ]*$/",$last_name) && $last_name!="") {
$error = true;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email!="") {
$error = true;
}
function IsInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject,$str)) {
return true;
} else {
return false;
}
}
if (IsInjected($visitor_email)) {
echo "Bad email value!";
exit;
}
// Sending the email
if ($error == false) {
$to = "youremail#yourdomain.com";
$subject = "Enquiry from website";
mail($to, $subject, $email_body, $headers);
// Calling the email sent / not sent alerts
echo '<script type="text/javascript">',
'enquirySent()',
'</script>';
} else {
echo '<script type="text/javascript">',
'enquiryNotSent()',
'</script>';
}
}
?>

Categories