PHP mail() sends emails using "anonymous#..." - php

I have tried the following with little success:
$fromEmail = "something.com <noreply#something.com>\r\n";
$headers = 'From: '.$fromEmail;
$headers .= 'Reply-To: '.$fromEmail;
$headers .= 'Return-Path: '.$fromEmail;
$headers = 'MIME-Version: 1.0' . '\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
if(mail($to, $subject, $message, $headers)) { echo "1"; exit; }
I have tried commenting out the Reply-To: and Return-Path: lines as well as replacing the if(... line with:
if(mail($to, $subject, $message, $headers,'-fnoreply#something.com')) { ...
In all cases the email arrives but is from anonymous#...

There is a syntax error in your code.
You are missing a dot in MIME header line.
should be:
$headers = 'From: '.$fromEmail;
$headers .= 'Reply-To: '.$fromEmail;
$headers .= 'Return-Path: '.$fromEmail;
$headers .= 'MIME-Version: 1.0' . '\n';
<...>

It looks like anonymous#... is your envelope "from" address. The envelope "from" address is different from the address that appears in your "From:" header of the email. It is what sendmail uses in its "MAIL FROM/RCPT TO" exchange with the receiving mail server.The main reason it is called an "envelope" address is that appears outside of the message header and body, in the raw SMTP exchange between mail servers.
To change the envelope "from" address on unix, you specify an "-f" option to your sendmail binary. You can do this globally in php.ini by adding the "-r" option to the "sendmail_path" command line. You can also do it programmatically from within PHP by passing -f mail#something.com as the additional parameter argument to the mail() function (the 5th argument).
In the php.ini you can add default from address like this
sendmail_from = me#something.com

To change the envelope mail from, you can use the fifth argument. This is used for options that should be passed directly to sendmail. Here, you should add -f info#mywebaddress.com. A simple example is shown below.
mail('recipient#domain.com', 'Subject', 'Message',
'From: info#myaddress.info','-f info#myaddress.info');
And also, all of this is mentioned in the official PHP manual on mail().

Related

How can use BCC in sending multiple mail in PHP mail function

I am trying to send mail to multiple recipient without them seeing each others email address
this is what I have done so far
//my variable $mailto got all the emails from the database
$mailto = preg_replace('#\s+#',',',trim($mailto));
$headers = 'FROM: COMPANY INC <support#admin.com>\r\n';
$headers .= 'BBC'.$mailto."\r\n";
$headers .= 'Content-Type:text/html; charset=ISO- 8859-1\r\n';
mail($mailto, "MY TITle", $mailbody, $headers);
You are setting the wrong header. There is no BBC, and you're missing a colon.
try
$headers .= 'BCC: '.$ml."\r\n";

PHP Sending Mail with Headers to Gmail

The mail() function in PHP works fine when sending to Gmail if I don't specify any headers. However, as soon as I try to add in headers, the function still returns true but I never get the email. The Gmail server seems to reject the delivery.
These are the headers I'm using:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $username . ' <' . $email . '>' . "\r\n";
$headers .= 'From: Blahblah <email#example.com>' . "\r\n";
mail($email, 'Subject', '<html><body>Body of message</body></html>', $headers);
I was hoping that someone could point out to me the flaw? I grabbed most of this code straight from the PHP manual, so one could understand my confusion and frustration. Thanks!
The bigger email services will not deliver mail sent from your own personal server in this manner.
Due to SPAM issues. All the big mail providers require SPF records, DKIM, and reverse DNS before they will accept your mail.
Some live/hotmail user's are not receiving html mail
Personally I prefer to be rid of that hassle and use a 3rd party mail server for all of my outgoing mails to my users.
$Headers .= "From: $Yourname <$YourEmail>\r\n";
or
$Headers .= 'From: '.$Yourname.'<'.$Youremail.'>'."\r\n";
or
$Headers .= 'From: '.$Yourname.' <".$Youremail.">' . "\r\n";
Give a try if any of this work?

Configuring exim's outgoing e-mail address

I forgot how I set it. I have two websites and I send the e-mail through my second site. Here's my /etc/exim4/passwd.client file:
*.google.com:noreplyn#firstsite.com:password
*.google.com:noreply#secondsite.com:password
I'm sending mail through PHP's mail() function with the following headers:
$headers = 'From: no-reply <noreply#secondsite.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Reply-To: noreply#secondsite.com' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
But I am getting this header on my client's email:
from: no-reply firstsite.com
reply-to: secondsite.com
to: apathetic012#gmail.com
How do I properly set a custom outgoing e-mail address for exim?
PS: I'm using a smarthost (Google apps)
Set your envelope From.
mail($recipient, $subject, $body, $headers, '-f noreply#secondsite.com');

How to add custom header to PHP mail?

I have a mail function with the standard parameters:
mail($to, $subject, $message, $headers);
Where:
$headers = "From: admin#mysite.org";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
Now, I'm trying to get the sent e-mails to show 'My Site' as the sender instead of 'admin#mysite.org' but if someone responds to the mail it still needs to go to admin#mysite.org.
I've tried putting "My Site" in the "From" field before and after the e-mail address but it just appends to the address. I've tried appending it to the $headers variable but that messes the whole e-mail up. I've also tried adding it this way:
$headers .= "\r\nMy Site";
but that doesn't seem to do anything.
I know this can't be complicated as I've seen it done a hundred times, but I can't seem to find a straight-forward answer for this - how do I 'mask' the admin e-mail with the site name but still keep it as the address any response goes to?
Change your From: header to "From: My Site <admin#mysite.org>"
$headers = "From: My Site <admin#mysite.org>";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
From the PHP Docs.
There is the "From: something <something#something.com>"; header, and then there is also the optional fifth parameter of mail() which allows you to override the from setting that may or may not be set in php.ini.
<?php
$to = 'name#domain.com.au';
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From: Name <noreply#domain.info>" . "\r\n";
$fifthp = '-f noreply#domain.info';
mail($to, $subject, $message, $header,$fifthp);
?>

PHP mail function is sending emails in Gmail's SPAM folder, How to get rid of it?

i am using this code :
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
it sends email to my gmail's SPAM folder .
Suggest any solution .
i dont want to use any PEAR MAIL type way to send email or dont want to require and include any file.
This functions works without including/requiring any additional php file.
You should use proper header like this from PHP manual
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
The Gmail spam filter has lots of parts to it which determine the spamminess of mail. You could start by seeing the SPF record for your domain. Avoid using sales language, HTML and colours where possible in your email.
There's also an old but fun video from Google about how their spam filtering works.
It's all in the headers. I don't know how GMail works internally, but I've found on my projects that setting Reply-To can Content-Type headers seems to fix this.
See PHP mail() docs for an example of doing this.
(Note: I've got a PHP questionnaire/response system that sets these headers and the e-mails come through correctly to GMail, Hotmail and Yahoo! Mail).

Categories