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

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.

Related

PHP mail() will not send a mail to an Outlook Distribution List [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
How do I prevent mails sent through PHP mail() from going to spam? [duplicate]
(6 answers)
Closed 3 years ago.
So a company I am helping out has outlook for their mail. Unfortunately, outlook seems to be very inflexible on how I can set up emails. I have added multiple domains to the outlook admin center for our multiple different sites. However, it will not let me create anything useful with the new domains.
For instance, we have "information#ourMainDomain.com" set up as an account.
I tried to create an alias on this account for "information#aSecondaryDomain.com" but then outlook gives me an error saying "information#ourMainDomain.com" already exists. Evidently you cannot use the same username even with different domains.
Fine. So i tried to create a shared mailbox - same issue. information#aSecondaryDomain.com is not allowed, because information#ourMainDomain.com exists... getting frustrating now!
So, after experimenting, trying everything, and spending 2 hours on the phone with outlook support that got me no where, i opted for my own route:
I created a Distribution Group called information#aSecondaryDomain.com, told this that senders outside the organization can send to it, added all the people that need to be involved, and gave them all send access.
This allows us to send emails as info#aSecondaryDomain.com
It allows us to recieve them here as well.
Also, i can send from outside the organization, ie from my gmail i can send to this account and it comes through, Great!
The problem is when i use php for a contact form, it WILL NOT send to the distribution group. It is not going to junk that i can see, and mail trace doesnt show it ever coming in at all. If i change the send to address of mail function to one of our main emails with the organization it comes through. If i change it to send to my gmail it comes through. As soon as i set it to the address of the distribution group, it will not come through. I see no errors on the php, it thinks it sent... but it never does.
Does anyone have an ideas? Or has anyone done this before?
The code in question is very simple...
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$subject = $_REQUEST["subject"];
$msg = $_REQUEST["msg"];
$to = "information#aSecondaryDomain.org";
if (isset($email) && isset($name) && isset($msg)) {
$email_subject = "$name sent you a message";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$to.">\r\n"."Reply-To: ".$to."\r\n" ;
$msg = "From: $name<br/> Email: $email <br/> Subject: $subject <br/> Message: $msg";
$mail = mail($to, $email_subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
EDIT: I have no tried PHPMailer... same reuslt - it will send to my regular work address, and my personal, but not to the distribution group.

Send DKIM signed mails from PHP?

I'm currently sending email messages such as password reset emails or confirmations and notifications from my website through PHP's mail() function.
I'm already specifying Return-path and Reply-to headers. I'm sending emails like this:
$to = "someone#gmail.com";
$subject = "Hello";
$body = "Blah blah blah";
$from = "rocketnuts#example.com";
$headers = "From: Rocketnuts <$from>\nReturn-path: $from\nReply-to: $from\n";
mail( $to, $subject, $body, $headers, "-f$from" );
This works fine, but to reduce the risk of these emails ending up in spam folders, I would like to send DKIM-signed emails.
Is this possible with PHP's mail() function, or should I use some 3rd party package? (which?)
When searching around, I see questions like "sending DKIM signed emails to Outlook.com" but I want a general solution, not something for specific recipient domains.
Just in case: I'm only sending emails from addresses # the actual domain where my PHP scripts are running, and I have access to its DNS records.

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 doesn't send from yahoo to gmail

I have created a contact form using mail function, everything just fine but there is a problem by sending mail from yahoo to gmail!
any email to any email is good(even from gmail to yahoo).
But when the email goes from yahoo to gmail, it dose not work!
Like this:
mail("myemail#gmail.com",$subject,$message,"From :myemail#yahoo.com");
This dose not work!
how to fix this?
whole code:
<?php
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: ".$email;
$subject=$form_name." - ".$subject;
$message=nl2br($message);
$message=$message."<br /><br /><br />".$name."<br />".$phone."<br />".$web;
$message = wordwrap($message, 70, "\r\n");
if(mail($send_to,$subject,$message,$headers)){
echo "<p class='success'><span></span> ".$success."</p>";
}
else{
echo "<p class='error'><span></span> there was a problem, please try again</p>";
};
?>
Most likely this is failing due to a security check - your server is not associated with Yahoo.com, thus when the origin IP address of the e-mail is checked against the domain of the sender e-mail address, it fails and the e-mail is blocked. Only some e-mail providers do this level of checking.
If your goal is to have your yahoo e-mail address show as the reply to address, try setting it using the reply-to: header instead of the from: header. See the PHP mail() docs for more specifics. Use the appropriate or default from address of your domain or server. This will give you the best results as far as making sure your e-mails actually get delivered.
When your mail server is sending an email, the DNS SPF record is checked to ensure that your server is allowed to send emails where from address is set to yahoo.com or gmail.com. If there is no SPF records which tells that your server is allowed to send emails for yahoo.com domain, this means that your server is maybe sending a SPAM. This way, some mail servers can reject emails received from your server.
I've just solved the same problem by moving the sender email address from the "from" header to the "reply-to" header.
Set From with something like "no-answer#yourmail.com"
Set Reply-to (add a line in your code) with the sender email address.
Gmail will no longer filtrate the mail, considering it's a transparent behavior and then not a spam attempt.
Though the problem occurs only with Yahoo/Gmail…

Send e-mail with PHP to Facebook group mail address

1, I tried to send e-mail in PHP to my Facebook group email, but these updates, doesn't appear. If I send the e-mail from my yahoo mail, it posted instantly. What kind of information should I add to the mail header?
2, Is is possible to add images as attachments?
My current mail sender code is:
<?php
$to = "mygroup#groups.facebook.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "me#yahoo.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Does the email need to come from "me#yahoo.com"? Because Yahoo mail uses SPF, which Facebook can use to verify that your email didn't actually come from there, and discard your email as a spoofing attempt.
If the email needs to come from me#yahoo.com, you'll need to connect to Yahoo mail through SMTP, and pass your username and password through, and legitimately send your email from there.
As for image attachments, it's possible.
EDIT: As per the lovely DaveRandom's comment, the following PHP packages should make your job MUCH easier: PHPMailer or SwiftMailer

Categories