Send DKIM signed mails from PHP? - 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.

Related

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.

One email to forward to multiple email addresses

I am looking to create one email (example#domain.com) to forward to all email addresses in a database.
People can email to example#domain.com, and then that email would get blasted to a list of predefined email addresses. It would need to include support for attachments.
I realize this is not secure at all and leaves this email address open to anybody to use, but this is what our organization wishes to do.
What would be the best way to do this on a PHP server?
Thanks.
This can be achieved in PHP Server as you have to go in depth of following things:
Email Piping
Email Headers
MIME Email attachments
Mailing List Management
While emailing to a lot of people in the way you mentioned is not a good idea...
You can use PHP's "mail" function:
$to = "user1#domain.com, user2#domain.com"; //(Comma separated list of emails)
$from = "noreply#domain.com";
$subject = "Hello";
$message = "Hello there!";
$headers = "From: ". $from . "\r\n".
"Reply-To: ".$from . "\r\n";
//send it
mail($to, $subject, $message, $headers);
while($row = mysql_fetch_array($emails))
{
$addresses[] = $row['address'];
}
$to = implode(", ", $addresses);
And then send mail using mail function ... mail($to, $subject, $message, $headers);
The way you have worded your query is a bit confusing but it seems you have a hosting server that is going to receive emails to a domain email. You then want to have those emails autoforwarded to a list of recipients. It looks like you want to investigate (given you mention sendmail) linux mail services and autoforwarding. You may be looking at postfix and possibly using procmailrc to trap and redirect incoming mail on your server.
I have done this with procmailrc before but it really depends on what service is handling the incoming mail.
There is no problem calling a CLI PHP script as suggested by #George to then read your recipients from the database and do the sending.
While Arthur's answer made sense and most likely would work, I ended up finding that my host had the feature I was looking for buried deep inside it. For those who were wondering, it is called discussion lists.

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

Hide server when sending email from mail php function

All,
I have the following code:
$to = $friend_email[$x];
$subject = "Subject";
$message = "This is a message";
$from = $your_email;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
When the email sends (I'm using Godaddy's hosting service) it says From correctly but then in gmail it says via pxnlhgxxx.prod.xhx3.secureserver.net. Is there anyway to hide the via part or make it say something like website.com? Thanks for the help.
As per the mail() docs, you use the optional 5th parameter for the function and pass in the name of the server you'd like to masquerade as:
mail($to, $subject, $message, $headers, "-f sender#website.com");
If your hosting off godaddy then something like that will happen. You can use your own SMTP server, or use Google free SMTP Server (logging in with your gmail account). Host Gator does the same thing.
You can prevent Google from showing the 'via' notice by DKIM signing your outgoing mail to prove that you genuinely control the domain you're sending e-mail on behalf of.
Its all up to the configuration of the smtp server.

PHP: cannot send sms, but can send regular old email

I am trying to send text messages to my phone from my server using php. I recently configured the server to send email, which it does (verified). It, however, goes into my spam box. When I try to send a message via sms I do not receive anything.
This is the script I am using:
$to = "myemailaddress#gmail.com";
$subject = "testing";
$body = "";
$headers = 'From: testemailaddress#gmail.com';
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
The address that I am using for sms is myphonenumber#txt.att.net in the 'To' field.
I am going to go out on a limb here and say this is a authentication issue, maybe.
Is there anything I need to configure further? (i.e. php.ini)
Most email providers/servers today rely heavily on spam filtering / dnsbl. Your webserver is not a know mail server, and you probably did not set up SPF or anything.
An approach to avoid all those issues would be to utilize a Google Mail address (or any other providers). And instead of the PHP mail function use something more complex like Swiftmailer, which generates Mail headers that are less commonly autoclassified as spam.
See also: Using php's swiftmailer with gmail

Categories