Send mail() using php but Sender name always Apache - php

I really need a quick fix and haven't found any solution to my problem.
I want to send email to the user but the sender name always shows Apache.
Here is my coding.
<?php
$password = rand(1000,9999);
$firstname = "syamsul";
$surname = "rizal";
$email = "example#yahoo.com";
$max_id = 3;
//Generate Email
$to = $email;
$subject = "Welcome to ShopOnline!";
$message = "Dear " .$firstname. ", welcome to use ShopOnline! Your Customer id is " .$max_id. " and the password is ".$password.".";
$headers = "From registration#shoponline.com.au" ;
// send mail
mail($to,$subject,$message,$headers, "-r 4914031#yahoo.com");
echo "Thank you for sending us feedback";
?>
And why i can't send this email to gmail account but works to non-gmail account? Thanks in advance!

set your header like
$headers = "From: registration#shoponline.com.au" . "\r\n" .
"Reply-To: registration#shoponline.com.au" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
UPDATE 2 :
with sender name
$headers = "From: Sender_Name<registration#shoponline.com.au>" . "\r\n" .
"Reply-To: registration#shoponline.com.au" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

This issue stems from you missing a colon in your headers string:
$headers = "From: registration#shoponline.com.au" ;
^ here
Add that in, and it should work as expected.

With regards to the second part of your question (gmail not accepting emails), there's a very large amount of considerations to be made.
First of all you should check the mail related php.ini sections. It's possible to specify a -f parameter that on some servers shall change the originating mail name from the server's default one (like in example "postmaster" and others) to your desired name.
Example:
-finfo#domain.tld
shall make your emails show up as coming from info#domain.tld. Notice the lack of space after the -f parameter.
Depending on how picky the destination email servers are, this alone may make your emails to be accepted. In fact many of them, as anti-spam measure check consistency between email headers and reported sender.
But that's not enough.
Some destination servers will still reject your emails or automatically put them in the spam folder.
To avoid that, there are several measures. Some of the basic tasks to perform are DNS related:
make sure your server has the appropriate reverse DNS records (PTR) set. Many - expecially shared hosting services do not assign them and you have to explicitly ask them to set them up for you.
You may check your records with one of the many websites featuring general DNS utilities. Here's a random website doing that.
You have also to set other DNS entries, namely the SPF record with appropriate setup like:
v=spf1 +a +mx +ip4:<ip address> ?all
Some servers will only recognize a TXT DNS entry with the same setup.

Related

how do i add From in your php email [duplicate]

I'm building a website that sends and email to a user when he registers.
My code (the gist of it):
<?php
$to = "helloworld#gmail.com";
$subject = "Test mail";
$message = "Hello! \nThis is a simple email message.";
$headers = "From: munged#gmail.com";
$headers .= "\r\nReply-To: munged#gmail.com";
$headers .= "\r\nX-Mailer: PHP/".phpversion();
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
the problem is that when the mail is delivered, the from header remains munged#box123.bluehost.com, while reply-to gets changed to the specified value.
box123.bluehost.com is the hostname of the server on which the website is hosted.
So what am I doing wrong? What can I do to get the "From" address the same as the reply-to address?
Is it something I'm doing wrong, or is the web host playing foul?
Edit: I just noted that you are trying to use a gmail address as the from value. This is not going to work, and the ISP is right in overwriting it. If you want to redirect the replies to your outgoing messages, use reply-to.
A workaround for valid addresses that works with many ISPs:
try adding a fifth parameter to your mail() command:
mail($to,$subject,$message,$headers,"-f your#email.here");
It turns out the original poster's server (blueHost) has a FAQ concerning this very question.
Article 206.
This is because our servers require you (or your script) to use a properly formatted, valid From: field in the email's header. If the From: field is not formatted correctly, empty or the email address does not exist in the cPanel, the From: address will be changed to username#box###.bluehost.com.
You must change the script you are using to correctly use a valid From: header.
Examples of headers that should work would be:
From: user#domain.com
From: "user" <user#domain.com>
Examples of headers that will NOT work:
From: "user#domain.com"
From: user # domain.com
From: user#domain.com <user#domain.com>
Our servers will not accept the name for the email address and the email address to be the same. It will not accept a double declaration of the email address.
For scripts such as Joomla and Wordpress, you will need to follow their documentation for formatting the from fields properly. Wordpress will require the Mail From plugin.
Note: The email address you use must be a valid created account in the
cPanel.
I had the same Issue, I checked the php.net site. And found the right format.
This is my updated code.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
'Reply-To: '. $fromEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
The \r\n should be in double quotes(") itself, the single quotes(') will not work.
In order to prevent phishing, some mail servers prevent the From from being rewritten.
I realize this is an old thread, but i had the same problem since i moved to bluehost yesterday. It may not have been the selected answer but i support the bluehost article 206 reply.
I created a valid email in control panel and used it as my From address and it worked.
I solved this by adding email accounts in Cpanel and also adding that same email to the header from field like this
$header = 'From: XXXXXXXX <test#test.org>' . "\r\n";
The web host is not really playing foul. It's not strictly according to the rules - but compared with some some of the amazing inventions intended to prevent spam, its not a particularly bad one.
If you really do want to send mail from '#gmail.com' why not just use the gmail SMTP service? If you can't reconfigure the server where PHP is running, then there are lots of email wrapper tools out there which allow you to specify a custom SMTP relay phpmailer springs to mind.
C.
headers were not working for me on my shared hosting, reason was i was using my hotmail email address in header.
i created a email on my cpanel and i set that same email in the header yeah it worked like a charm!
$header = 'From: ShopFive <site#mysite.org>' . "\r\n";

PHP Mail is Being Sent to 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 7 years ago.
I know that there are many similar questions on this site, but none of them helped me.
I have the following PHP code:
<?php
$to = "mymail#inbox.com";
$from = "no-reply#heygee.com";
$subject = "Confirm your registration!";
$message = "Please follow this link to confirm your registration: www.bit.ly/32106";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: HeyGee! <no-reply#heygee.com>" . "\r\n" .
"Reply-To: mymail#inbox.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$ok = #mail($to, $subject, $message, $headers, "-f " . $from);
?>
Let's say mymail#inbox.com is my test e-mail. Whenever I send the mail to my address, I always receive the mail in my Spam folder. Why is that? Are there any corrections or tricks to be made to prevent the mail from arriving in Spam?
Thanks.
The reason why your mail is being sent to Spam folder is either because of the content of your email or that the receiving side is not able to verify if the email actually came from the stated domain in the from address, i.e., if the sender (you) are authorized to send email on behalf of heygee.com.
Content part is easy to correct. You need to avoid bad grammar, ambiguous links (e.g links which say google.com but point to example.com), etc. Your message should be well worded (exclude those words frequently found in spam mails), and preferably include an unsubscribe link as well (if sent to a mailing list).
Now comes the second and difficult part. The domain that you are writing in your from address should be the same domain from which you are executing this mail script or should be authorized by this domain's TXT records to send mail on its behalf. The simplest way to go about this would be (provided you have DNS access to the sending domain name) to add a TXT SPF record permitting the IP of the server your script resides on to send mail on its behalf.
Example SPF record:
"v=spf1 ip6:1080::8:800:200C:417A/96 -all"
The above record means Allow any IPv6 address between 1080::8:800:0000:0000 and 1080::8:800:FFFF:FFFF.
Checkout:
SPF (Wikipedia)
Also, you may have a look here http://www.openspf.org/
Now if you don't have DNS access, then simply change the domain name of the from address to the domain name of the server and it should fix it.
Hope it helps.
This is not related to your programming by itself. Filtering e-mail as spam is done on the client side and there is nothing you can change about that (if it were, millions of spammers would be doing it)
The problem you have is that your e-mail looks like spam. Try to write a better e-mail, with a body longer than 1 line. I would also remove the exclamation mark from the subject; that's also something that makes it look like spam.
If your spam detection system gives you detailed information on why it was labeled as spam (SpamAssassin can do that depending on how it is configured), then use that information to fix the e-mail that you send.

PHP mail: correct formulation of email headers on a VPS

I've been scouring forums like this one, and others, to try and get a better understanding of how to generate emails - in this case using the PHP mail() function - that will work without violating accepted protocols.
The situation is that my 'agency', for want of a better word, has a website advertising services from various companies. A customer who we can call John Doe fills in an online form and the information is sent to, say, Company A. Company A can then respond to it.
There is a further twist in that my site is hosted on a Virtual Private Server. So my 'real' address might be "Agency" <info#myagency.com> but the email that's generated may actually come from <someaddress#myvpserver.com>
I should add that there is an SPF record in place.
I'm really trying to understand how the email headers can be configured such that:
The email arriving at Company A looks like it comes from
John Doe john#doe.com
rather than
Agency info#myagency.com
or worse,
from someaddress#myvpserver.com
Company A is able to easily reply to John Doe - I don't need a copy of that
I'd really like to capture any bounces if the email sent to Company A is undeliverable for whatever reason
I don't know if that's feasible? If it is then I'd really like to know how to configure the headers - To, From, Reply-To and so on .. thanks!
1 & 2:
Add this header:
$headers = 'From: it#comes.fromhere' . "\r\n" .
'Reply-To: it#comes.fromhere' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
And 3:
$mail = mail($to, $subject, $message, $headers);
if($mail)
{
echo "Done!";
}
else
{
echo "Problem while sending!";
}
Try that!
I don't know if I'm allowed to answer my own question, but I've made some progress since posting which may be helpful to others. What seems to work - though more testing is required - is the following:
$to = 'info#companya.com';
$from = 'john#doe.com';
$sender = 'info#myagency.com';
$replyto = 'john#doe.com';
$subject = "message subject";
$msg = "message body";
$headers = "From: John Doe <$from>"."\r\n";
$headers = $headers."Sender: My Agency <$sender>"."\r\n";
$headers = $headers."Reply-To: John Doe <$replyto>";
mail($to, $subject, $msg, $headers, '-f'.$sender);
It seems that the optional extra parameter '-f'.$sender ensures that any bounce message is sent to My Agency, rather than John Doe; exactly what I wanted...;-)
Of course, this may not be the only solution and I'm still not sure that this is the correct usage of the From; and Reply-To headers, so any comments from those who know about these things - I'm just a hack - would be welcome.
UPDATE:
Since I posted the original question, a new DMARC protocol has been adopted (in early 2014) by a number of email providers. I found, for example, that some emails generated on the server were being bounced because the "From:" header didn't match the actual source, ie my domain. So, for example, if the "From:" header specified a yahoo.com address and it was detected that the email hadn't actually come from a yahoo.com server then it would be bounced. This has been done to make it more difficult for spammers to spoof email addresses, but it has had a knock-on effect.
A partial solution has been to set the "From:" header to the actual source - my email address - and to rely on the "Reply-to:" header to ensure that the reply does, in fact, go to the customer rather than to us. This has has some success but, at the time of writing, it seems that we're getting some replies that should have gone to the customer but didn't. It is as if some email providers are not respecting the "Reply-to:" header but I just can't be sure at this stage.
I make no apologies for trying to answer my own question, but I'm sure I'm not the only one experiencing these issues so I hope that a definitive solution may emerge. This, however, is the best I can do at the moment and we now have:-
$to = 'info#companya.com';
$from = 'john#doe.com';
$sender = 'info#myagency.com';
$replyto = 'john#doe.com';
$subject = "message subject";
$msg = "message body";
$headers = "From: My Agency <$sender>"."\r\n";
$headers = $headers."Sender: My Agency <$sender>"."\r\n";
$headers = $headers."Reply-To: John Doe <$replyto>";
mail($to, $subject, $msg, $headers, '-f'.$sender);

how to know if php mail failed

I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = 'itsdfdsf#7sisters.in';
$email_from = "info#7sisters.in";
$full_name = 'XXXX';
$from_mail = $full_name.'<'.$email_from.'>';
$subject = "testing sender name";
$message = "";
$message .= '
<p><strong>This is only a test mail. Please do not reply.</strong><br />
';
$from = $from_mail;
//$headers = "" .
// "Reply-To:" . $from . "\r\n" .
// "X-Mailer: PHP/" . phpversion();
$headers = "From:" . $from_mail . "\r\n" .
"Reply-To:" . $from_mail . "\r\n" .
"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,$message,$headers))
{
echo 'failed !!';
}
But although $to mail does no exist,it is not showing failed !!
The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.
I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog
check the return from of mail
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one...
You should run the $to through a validator to check its a valid address and then throw an error if its not, don't rely on mail() to filter out things which you already know are wrong, or can check against easily.
--UPDATE
Then check out #SeRPRo , but what your trying to do is hard work to test programatically - its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it's real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don't be surprised to find it difficult going if you avoid the verification e-mail route.
But although $to mail does no exist,it is not showing failed !!
actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. There's no easy way in PHP to check whether it's delivered.
You could CC yourself as a way of testing that it is leaving the outbox.
In my case it helped to set the return-path via the commandline parameter "-f", which can be passed in the $additional_parameters parameter of mail(). so i call
mail($to, $subject, $message, $headers, "-f address.where.i.want.the.bounces#xy.com");
... according to some comments on http://www.php.net/manual/en/function.mail.php hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the "From:" in the heade ... and so on)
The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory http://www.df.eu
Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce#mail.com');
This will send bounce email at bounce#mail.com if recipient mail id does not exist or recipient does not receive email by any other case.

Hotmail rejecting php's email's

My website sends activation mails to new members using the php mail() function. I manage my email accounts using the google mail service (aspmx.googlemail.com). But unfortunately, Hotmail users receive the activation email in their unwanted folder.
I did some research and found out about SPF records (I can not use keys since I have limited control over my webserver). So how do I configure my SPF record?
I now have:
v=spf1 a mx a:mywebsite.net include:aspmx.googlemail.com ~all
Would this be what I need?
EDIT:
I now used PHPMailer to connect to the google SMTP server. Hotmail no longer marks them as unwanted this way. (http://sourceforge.net/projects/phpmailer/)
PHP's mail() is probably not using smtp to send your emails. I would consider adding the appropriate headers and using a spam scorer to help your chances, or you could try and use smtp through google's mailservers (although I bet that is unlikely).
Here is a spam scorer:
http://www.contactology.com/check_mqs.php
Here is an example of some email headers, be sure to set your From header as its default probably isn't good for spam filters:
$headers = "From: My site<noreply#my_site.com>\r\n";
$headers .= "Reply-To: info#my_site.com\r\n";
$headers .= "Return-Path: info#my_site.com\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($recipient, $subject, $message, $headers);
Inorder to utilize that SPF record you need to add it to your DNS Zone file.
Go to wherever you have your DNS registered and add a couple of TXT records.
They should look something like:
mywebsite.net. IN TXT "v=spf1 a mx a:mywebsite.net include:aspmx.googlemail.com ~all
"
mailserver.mywebsite.net. IN TXT "v=spf1 a mx a:mywebsite.net include:aspmx.googlemail.com ~all
"
There is a ton of great info over at http://www.openspf.org/
This will not guarantee that your message will get through the spam filters, but it will help.
The other thing todo is get "whitelisted" with the Hotmail team. It has been a while, but you essentially register your machine with a contact. This allows them a person to complain to directly when questionable material comes from your site. They tend to let more of your mail through after you get yourself "whitelisted".

Categories