Solved

PHP mail() will not send emails to certain domain

Posted on 2010-11-25
3
491 Views
Last Modified: 2012-06-27
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
Comment
Question by:theskintman
3 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 34211809
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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34211819
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
 

Author Comment

by:theskintman
ID: 34211952
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now