Solved

PHP mail() will not send emails to certain domain

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

735 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