Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

PHP mail() will not send emails to certain domain

Hello experts,

I am currently trying to move my website over to another host. The site has several forms that users can fill in which use PHP's mail() function to send a confirmation email to the user and a notification email to me. This has always worked perfectly on the old server.

However, on the new server, PHP's mail() function will not send emails to our domain (let's call it mysite.com). It will send emails to any other domain except ours, and comes up with no errors or warnings - as far as the PHP mail() function is concerned, it has sent the email.

Some test code I have been using is included below. It will work if the domain for the 'to address' is anything other than 'mysite.com'. Does anyone have any idea why this is happening?

$to      = 'me@mysite.com';
$subject = 'Test email 2 from me';
$message = 'hello';
$headers = 'From: someone@mysite.com' . "\r\n" .
    'Reply-To: someone@mysite.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)){
	echo "mail sent<br/>";
}else{
	echo "mail not sent<br/>";
}

echo $to."<br/>".$subject."<br/>".$message."<br/>".$headers;

Open in new window

0
theskintman
Asked:
theskintman
1 Solution
 
Frosty555Commented:
This type of problem kindof sounds like a problem I had recently with local mail routing - the webserver's PHP is configured to use it's own local mailserver to route emails, and emails that match your own domain are routed locally instead of going out to the internet - fine if you are using the web host's provided email service but might not work if you are using your own servers for email.

Look into what mailserver your new web server is using, and how it is configured. You might be able to see this using the phpinfo() call in a php page. If there is a way to explicitly specify your own smtp server to use as a smarthost, that would be ideal since mail would be routed through an smtp server that you control.

Maybe you can use PHPMailer class instead? It supports specify your own smtp server instead of using php's built in settings.

Can you give a little more information on how your email is setup at your organization? Are you using the mailservers provided by your web host or something else? Do you have your own MX records set up for your domain?
0
 
Loganathan NatarajanLAMP DeveloperCommented:
did you try something like this,

instead of sending & receiving same domain, try diff. domain name???
$to      = 'me@mysite.com';
$subject = 'Test email 2 from me';
$message = 'hello';
$headers = 'From: someone@othersite.com' . "\r\n" .
    'Reply-To: someone@othersite.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)){
	echo "mail sent<br/>";
}else{
	echo "mail not sent<br/>";
}

echo $to."<br/>".$subject."<br/>".$message."<br/>".$headers;

Open in new window

0
 
theskintmanAuthor Commented:
Thanks for the replies!

I managed to fix it in the end - the problem was that the server was routing emails internally, rather than sending to our external mail server, so emails sent to our domain never reached the outside world. Luckily my host provides a CPanel where you can easily change this kind of stuff. If anyone else has this problem and has CPanel, you go to 'MX Entry Maintenance', select the domain you are trying to send emails to, and under 'email routing' select 'Remote Exchange Server'.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now