Php email function not working properly - php

I know this seems like it is a duplicate but please read first:
I have the following php code:
<?php
$to = 'myemail#yahoo.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$headers = "From: ".$email." \r\n";
$headers .= "Reply-To: ".$email."\r\n";
mail($to, $subject, $message, $headers);
?>
I think it's the standard email sending script. However, I face an interesting bug. My website is florin-pop.com and the emails are only sending when in the email input field I put something like this: blahblah#florin-pop.com or mama#florin-pop.com or anything before #florin-pop.com.
If I try to put a something different like test#yahoo.com or even a real yahoo email address I don't get the email. Why? It's something wrong with my code? It may be from the hosting company? ( I use hostgator ).
EDIT:
If I change the Reply-To to the domains email address then it is working, but it is still not the perfect way to do it. If you press the reply button and forget about this trick, you will email yourself.
Code:
<?php
$to = 'myemail#yahoo.com';
$my_domain_email = 'myemail#mydomain.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$headers = "From: ".$email." \r\n";
$headers .= "Reply-To: ".$my_domain_email."\r\n";
mail($to, $subject, $message, $headers);
?>

In this case, delivery failure may be caused by Yahoo's adoption of the Domain-based Message Authentication, Reporting, and Conformance (DMARC) policy.
This means all DMARC compliant mail receivers (including Yahoo,
Hotmail, and Gmail) are now bouncing emails sent as "#yahoo.com"
addresses that aren't sent through Yahoo servers. [Yahoo]
Twitter, Facebook, Linked In, Paypal, AOL, Comcast and others have also adopted this policy. [Venture Beat]
A solution: Change the "From" header to an address at the server from which you are sending the email. This (correctly) indicates that the mail was sent from your server, and not from Yahoo. You can still use a user-submitted address in the "Reply-To" header so that the recipient can reply to the sender.
As a best practice, you should ... be using a domain you control in ... the "From:" header... [For example,] the site visitor's name is shown in the descriptive part of the "From:" header, and the "Reply-To:" header is set to the website visitor's address, but the actual address used in the "From:" header clearly indicates that your website is the origin of the message. [DMARC]

Related

PHP mail() not sending

I have a WebFaction server and the following code:
$to = "outreach#bmun.org";
$reply_to = "From: " . $_POST['email'];
$name = $_POST['name'];
$subject = "Outreach Request Session for " . $_POST['school'] . " on " . $_POST['date'];
$em = $_POST['message'] . "\n-" . $name;
$sentmail = mail($to, $subject, $em, $reply_to);
$sentmail returns true, but the email is not sending for some reason.
$sentmail returns true, but the email is not sending for some reason.
The mail function in PHP simply sends the mail via an MTA (mail transfer agent) on the server. A true can just mean the local MTA accepted it. But that is not all you need.
First, does your hosting provider actually allow outgoing mail? Or are messages sent to a virtual “black hole?”
Now, let’s assume that your local MTA—most likely sendmail—works, and the mail jumped off of the server & made it into the real world. Okay, great!
But not so fast…
The thing is just because you send a mail, it doesn’t mean that the receiving end thinks the mail is valid. And chances are the receiving end has decided a random e-mail sent off of a random server is simply SPAM.
I have posted a more detailed answer here, but when it comes to SPAM it basically boils down to this: Do you have an SPF (Sender Policy Framework) record setup for your domain? Do you also have a PTR (reverse DNS) record set for that domain?
If you do not have an SPF or PTR record, the chance of your message simply being flagged as SPAM is quite high.
If you are serious about sending mails off of your server, you need to at least get your SPF record & PTR record set.
You don't have any headers in your email.
Although they don't seem to be required, your emails will be blocked by spamcheckers a whole lot sooner.
This is an example:
<?php
$headers = "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$status = mail($to, $subject, $message,$headers);
?>

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);

Is it possible to specify "Reply From" email address in form submission using PHP?

I have a form on my website which will send me an email using PHP. It will be sent to an email address from my hosting service, which I never check, so I set it up to forward emails sent to that address to my iCloud email address. This works great, since it preserves the "From" address so when I reply, it is sent to the person who filled out the form. The problem is, when I reply to the email sent from the form, the person who gets that reply will see it came from my iCloud account, and replies to that email will go directly to my iCloud account rather than to my hosting service email.
My question is, is there a way to specify in my PHP code what email should appear in the "From" field upon replying to the email (but of course, this is different from the "From" address in the first email sent to me (the person filling out the form)?
Essentially, I want it to work like this:
-Upon form submission, an email is sent to example#myhostingservice.com with the "From" address set to the email inputted in the form
-example#myhostingservice.com forwards the email to my iCloud account (but preserves the email address of person who filled out form in the "From" address)
-I reply to that email using my iCloud account, and the receiver sees it came from example#myhostingservice.com (not iCloud), and when they reply it goes to example#myhostingservice.com
Here is a snippet of my code to see what I'm working with:
$name = $_POST['name'];
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if( empty($errors))
{
$to = $myemail;
$email_subject = "$subject";
$email_body = "Name: $name \n \n $message";
$headers = "From: $email_address";
mail($to,$email_subject,$email_body,$headers);
}
Thanks!
From docs (http://php.net/manual/en/function.mail.php):
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Although I must warn you, doing such may mark your email as spam since the email address it is being sent from does not match the From header.
I don't think this is possible using PHP - the email client you use on your iCloud account sets the new "Reply-To". However, try to add the hosting email account to your iCloud email client -(Using POP3 or IMAP)- then you will be able to "Send as" your hosting account and you won't need to forward everything to iCloud.

not sending email in php with header from email id

I am using the following code along with HTML for header .
$email="example#example.com"; in the example and I want to implement variable in its place.
Code as posted below but its not showing error nor sending email.
I have tried the following links PHP email form not sending information , PHP Sending Emails with File Attachments - Email Not Sending At All .
I have tried
$headers .= "From: <".$email.">\n";
and
$headers .= $email;
This displays $email in the label from header in email.
But its working fine till this line:
$headers .= 'From: ' .$email. "\r\n";
This above line is not sending email if I remove this line it works but it does not add From email id to the header.
Please help me out it does not show any error and I have tried many variations to the above code but still stumped.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['contact'];
$subject = "feedback";
$question = $_REQUEST['question'];
$body = "<html>
<head>
</html>";
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$headers = "MIME-Version: 1.0"."\r\n" ."Content-Type:text/html;"."\r\n";
$headers .= 'From:'.$email. "\r\n";
$to ='example#example.com';
mail($to,$subject,$body,$headers);
echo "<script>alert(' message sent.');</script>";
?>
I had the same issue with one of the servers I were dealing with. Apparently in my server, not specifying "From" header sends email from the default mail address of your account (in case of a shared hosting). I chose this solution as an ad-hoc fix and specified my actual from address in "Reply-To" header. This way, I can still receive the replies sent to those email threads.
This method seems feasible for just functional email addresses (eg., support#example.com)only. If you are using this approach for a user's email address (eg., john#example.com), chances are that the recipient might think of your email as a spam.
if you use standard mail function, then try this(do not add "From" header to $headers):
mail($to,$subject,$message,$headers,"-f ".$email);

PHP email text to gmail account

Is there some way I can take text (retrieved from a form), and email it to my gmail account? I can also have the user enter their email address, and a subject. Or if not is there a better way to have users send me a message? Thanks.
Use mail function to send email to specific address:
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
mail("your#gmail.com", $subject, $message);
But please don't take $to parameter from form, otherwise your script will be used for spamming.
I recommend that you use PHP Mailer this program will take care of all of the message construction and works well with Gmail. There is also sample code included for Gmail.
Expanding on what Ivan wrote to add users email as sender:
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST['from'];
// the sender email must be cleaned of mime header injection
$from = preg_replace_all("/[\r\n]+/", '', $from);
mail("your#gmail.com", $subject, $message, "from:$from\n");
This makes it easier to reply. However, you could just append their email address in the message body.

Categories