Situation:
Our client (owner of domain.com) has set the A record for www.domain.com to the IP address of one of our servers where we run the website behind domain.com. We only provide hosting for this domain, they have their own email servers.
This means that domain.com has another IP than the mail server for domain.com.
Problem:
Sending mails from PHP to foo#bar.com works BUT sending mails to *#domain.com does not work.
Question:
Does this has something to do with SPF records?
How do I solve this?
thx
Bundy
This happened to me on a shared host as well as is probably because there is a local delivery mechanism in place on the web server, i.e. when your web server sees an email for #domain.com it assumes it will be the one to handle, and does not pass in on to the actual mail server.
Go into your web server's panel (Cpanel or whatever) and check your email settings for this domain. Make sure "local delivery" or something similar is disabled for domain.com
Does your main domain.com (without www.) have an CNAME record? This will automatically refer the mx record of the domain to the cname record.
The characteristic payload information of an MX record is the fully
qualified domain name of a mail host and a preference value. The host
name must map directly to one or more address record (A, or AAAA) in
the DNS, and must not point to any CNAME records.
http://en.wikipedia.org/wiki/MX_record#cite_note-0
The best way to send emails is using a smtp method:
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/
Example file:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "mail.example.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "user#example.net"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "example#example.net";
$mail->FromName = "Mailer";
$mail->AddAddress("destiny#example.net");
//$mail->AddReplyTo("info#example.com", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Related
This code is work, email is sent
$mail = new PHPMailer();
$mail->setFrom("name1#gmail.com", "Name");
$mail->addAddress($to); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("name1#yahoo.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send())
{
echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
}
But for some reason, if I change From email to name2#yahoo.com
$mail->setFrom("name2#yahoo.com", "Name");
emails does not sends anymore. Phpmailer does not report any error messages.
name2#yahoo.com is valid working email address related to this web server.
Thanks.
This is covered in the PHPMailer troubleshooting guide.
Most service providers now have strict SPF and DMARC configurations (Yahoo especially, since they invented DMARC) that mean that you cannot send from addresses in their domains except through their own mail servers, or any others included in their SPF records.
Your code is sending through your own local server, which is not a Yahoo server, and thus won't work.
The solution is to send through Yahoo's own servers using authentication for your email account, something like:
$mail->isSMTP();
$mail->Host = 'smtp.mail.yahoo.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'me#yahoo.com';
$mail->Password = 'password';
Yahoo's DMARC config won't let you spoof the from address, so you can only use a from address that matches your username - this is probably the cause of the symptom you're seeing.
I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
// $mail->SMTPAuth = true; // enable SMTP authentication
// $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
// $mail->Host = "smtp.gmail.com"; // sets as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.
Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet="utf-8";
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '172.17.224.12'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
//$mail->Username = '***'; // SMTP username
//$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('itslt#example.com', 'Company.com');
$mail->addReplyTo('itslt#example.com');
$mail->addAddress('dichitslt#example.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
$bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';
$mail->SMTPDebug = 2;
$mail->Subject = 'Email from Localhost by CodexWorld';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
When I run this code, it says message has been sent. However, when I look at my email, there's a notification saying that the email sent through my code was bounced back.
What could be the possible problem? was it the configuration in my code or was it the server?
screencap of the bounce back email
For those who encounter the same problem, what resolved to this problem is smozgur's answer.
I think the server you are trying to send email through php - your
local server - is not authorized to send email and considered as SPAM
(like someone else is trying to send email by using your credentials).
When I need to make my scripts sends email from the server, then I add
my server IP as the SPF record in the DNS records. That way, my email
server knows that IP is sending email in my knowledge and allows. In
your case, I think you can likely add your IP to temporarily solve the
problem. But you need to do that for the production server IP later as
well. If this is the case of course..
I contacted supervisor and told him the status of the email function, which is emails are being sent, but the mail server returns the emails to the sender. Also, the possible cause of the problem, which is the local server is not authorized to send email and considered as SPAM. Lastly, the possible solution, which is add the server IP as the SPF record in DNS record.
I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "myusername#gmail.com";
$mail->Password = "gmail_password";
$mail->From = 'donotreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->AddAddress('Toreceiver#test.com', 'Receiver'); // Add a recipient
$mail->IsHTML(true);
$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;
$mail->Body = "This is in <b>Blod Text</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
It is sending mail from myusername#gmail.com but I want it to send with 'donotreply#mydomain.com' as set in $mail->From. Any help will be highly appreciated.
Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.
My solution was to add
$mail->AddReplyTo($FromEmail, $FromName);
This way at least if you reply to the message it will be delivered as described
There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.
Use:
$mail->SetFrom('donotreply#mydomain.com', 'Admin');
Instead of $mail->From and $mail->FromName.
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
settings > Accounts and import > Send mail as:
set another email which you want to use as from:
if you are using zoho then you can follow:
settings > Mail tab > Send mail as > add from address
then verify that email.
For GSuite users...
What Jyohul said, is the correct way of doing it. You can add an alias in Google Admin Console, under "Users". Click on the user's name, and then click on "user information". In there you can add Aliases. Once the aliases are added, you can then do what Jyohul said, which I added a needed step...:
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
Go to your Gmail, then click the gear on the top right, then:
settings > Accounts > Send mail as: then click "Add another email address".
Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.
$mail->SMTPDebug = 2;
You will the identify the error. Also make sure your SMTP Server credentials are correct. Like host, username and password.
Mine worked correctly
I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
// $mail->SMTPAuth = true; // enable SMTP authentication
// $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
// $mail->Host = "smtp.gmail.com"; // sets as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.