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.
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.
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 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! :)
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>';
}
}
?>