PHP mail() from email and replyto adds server address [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)
Send email using the GMail SMTP server from a PHP page
(16 answers)
Closed 4 years ago.
I currently have a php script to send emails from Ubuntu server with sendmail.
$to = $sendTo;
$subject = $subjectPrefix . $subject;
$txt = $message;
$headers = array(
"From: ". $email,
"Reply-To: ".$email,
"Content-type:text/html;charset=UTF-8",
"MIME-Version: 1.0",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
if(mail($to,$subject,$txt,$headers)){
echo "sent";
}
else {
echo "failed";
}
The emails send fine but always go into spam and have the server address attached to the email, for exmaple: "input#email.com"#ip-###-##-##-###.eu-west-2.compute.internal
How would i go about setting this to only show the input email and not go into spam?

It's because the mailserver you're using to send your email does not belong to the domain of the sender, and is therefore not a 'trusted' source.

You can use SMTP server of your email's domain. For example, if you want to use email from Google's Gmail Service as From address , you have to use the Gmail SMTP server.
So, you can add SMTP email rely for SendMail. How to do it you may read here
Also, you can install SMTP client msmtp and configure PHP for using it. Instructions here
At last, if you don`t want to configure your server, you can use PHPMailer library.

Related

How can I send email from this code, I am trying to send emails from localhost xampp server [duplicate]

This question already has answers here:
Send email using the GMail SMTP server from a PHP page
(16 answers)
Closed 4 years ago.
<?php
if (isset($_POST["send"])) {
$to = $_POST["to"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From: your#email-address.com\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "SUCCESS";
} else {
echo "ERROR";
}
}
?>
This is my php code, I have a HTML form and $to, $subject, $message is the input fields of the form, the $_POST["send"] is the button through which I want to send emails after clicking that button
You can't send emails from your localhost server. Get a free PHP server and upload your files using ftp. Following are some free php server providers where you can test your project.
000webhost
Infinity Free
That's true, you can't send emails from localhost, unless you were using a VPS configured with DNS + public IP + email server;
If isn't your case, I would recommend sending emails through a SMTP server:
PHP Mail (php.ini configuration)
PHPMailer (library)
For the SMTP server you could use any provider that allows SMTP.

php mail does not work with no-gmail address [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have part of my project when my clients will reseive mail after so many actions, when I testing my mail function, I have problem when the adresse is to "gmail" , I reseive the mail, but when the mail is no-gmail, the mail not reseived !!! :( this is my mail function:
function emailDemande($email, $name, $nameL){
$subject = 'Votre demande ';
$headers = "From: XXX <contact#emoovio.com>\r\n";
$headers .= "Bcc: abdelkhalek.oumaya#gmail.com, TEST#DOMAIN.com\r\n";
// $headers = 'From: XXX <'. $from. '>\r\n';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message='htmlmessage';
mail($email, $subject, $message, $headers);
};
Mail is very complex in his flow.
Your Mail Headers and Content
Does the sender mail address exist as mailbox or as forwarder? Sending mail with a non-existent mail address goes wrong.
What about the mail headers, subject and body? More info: https://sendgrid.com/blog/10-tips-to-keep-email-out-of-the-spam-folder/
The Sending Mail Server
Be sure the sending server (the smtp server) knows and allows the sender mail address to mail from.
Blacklist
Check if your smtp server is blacklisted. More info: http://mxtoolbox.com/blacklists.aspx
External Mail Service
I recommend to use external mail services, like Mailgun (http://mailgun.com). Mailgun provides an API for sending, receiving and tracking mail. A great advantage is that your own mail server (or global ip) cannot be blacklisted.
For testing your mail functionality, you can use Mailtrap (https://mailtrap.io).

PHP 'mail()' Function Not Sending Email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm using a basic script on a 1&1 hosted server:
$recipient = "email#domain.com";
$sender_name = $_POST['name'];
$sender_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html; charset=UTF-8"."\r\n";
$headers .= "From: {$sender_name} <{$sender_email}>"."\r\n";
$headers .= "Reply-to: {$sender_name} <{$sender_email}>"."\r\n";
mail($recipient, $subject, $message, $headers);
..but for some reason am not receiving any emails, nor any errors as per PHP mail() function not sending email instructs.
I thought this may be a server issue but 1&1 states that it is fully supported. I've also sent emails from this server/hosting before using just a recipient, subject and body and so I'm rather unsure as to why it is not working now!
UPDATE
Sending without headers, i.e.:
mail($recipient, $subject, $message);
..does work, so it would appear to be an issue with using the headers?
There may be many reasons, for example you should study what SPF is.
The $sender_email can't be any address, for example you can't put a gmail address and send emails claiming to be that sender, without any authentication, you aren't allowed to send email on behalf on that address, because I could for example send emails putting your address in the from, pretenting to be you when I'm not (I tried to use simple words)
You should use in the From something ending in #yourdomain.com, and set up SPF to allow your server's IP to send emails for that domain. OR otherwise send emails through SMTP (with PHPmailer, for example, it's very easy)
Errors in header does affect the mail delivery. if you send email with wrong headers, for example, let's say the email of the user is user#email.com and his name is "user user", and you send the email to user#email.com it might cause the email to go in spam or not accepted at all if it doesn't match on server.
Gmail, for one, has a name associated with every email id. if you put a wrong name with the email, there's a chance the email might not show up in inbox or sometimes even spams.
"But in the link I attached the example given uses a 'from' header set by a form input?"
if you want your users to be able to send email to you, best idea would be to use your own email address as the sender email. E.g. if your website is at website.com, configure an email like "contact#website.com" and configure your script to use this as From header, you can send it to your own email at the same domain(website.com) or any other email which you authorize. You can add the users's actual details in the mail. that'll ensure the successful delivery of the email always.

Contact form with Gmail account [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Hi I am trying to create my contact form and link it up with my Gmail account which I have. I have been trying to use the normal simple way by creating a HTML form and PHP file so that I can use the mail() function to send the email but this does not seem to work at all.
I want to create a contact form which links to my Gmail account and works when I host it on my server which is godaddy. This is driving me insane for the past 4 hours!!
Gmail is not the only mail service that will ignore sendmail emails.
If you're using 3rd-party hosting, you should find out the host name (e.g. "myhost.com") and use that in your From: header. Then use the correct email address in the "Reply-To" header instead.
Just for example:
$mailheader .= "Reply-To: Some One <someone#mydomain.com>\r\n";
$mailheader .= "Return-Path: Some One <someone#mydomain.com>\r\n";
$mailheader .= "From: Some One <mydomain#myhost.com>\r\n";
$mailheader .= "Organization: My Organization\r\n";
$mailheader .= "Content-Type: text/plain\r\n";
To increase deliver ability, you should use SMTP to send a mail via an actual email account.

PHP mail() - marked as spam [duplicate]

This question already has answers here:
Gmail and Hotmail marked as spam
(5 answers)
Closed 8 years ago.
My emails that are send by mail() are getting directly into the spam box, tested with GMAIL and HOTMAIL.
So:
I have dedicated server
My server ip is not listed on any blacklists
This is my code:
function send_email($recipient, $sender, $subject, $email_body){
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$sender;
mail($recipient,$subject,$email_body,$headers);
}
$from = 'info#domain.com';
$subject = 'subject';
$email_body = '<h1 style="text-align:center;">Title</h1><p>Message</p>';
send_email($userbday['email'], $from, $subject, $email_body);
Any thoughts?
Try to setup an SPF record for your domain.
Also
Email is serious business.
Try transactional email service providers
It may be that your email looks exactly as spam. It sometimes happens...
All my applications sends email - depending on how the client prepare the email templates - some are treated as spam almost instantly, some not.
Setting SPF Record can help, but also check (and use) some blacklist emails for your own good - like people who dont want to receive your emila should never be bothered again as they can trigger your server to be in the black lists (for example spamcop).

Categories