Solved

PHP mail() will not send emails to certain domain

Posted on 2010-11-25
3
492 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

12 Experts available now in Live!

Get 1:1 Help Now