PHP mail() - Exchange 2003 doesn't receive email - php

I have a PHP mail() script that sends a HTML email that contains embedded images.
The code is basically this:
mail($to, $subject, $message, $headers);
The headers are like so:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="--a9cf4407bac7d49ebd2d94af284cb0d8"
From: Myname <name#domain.co.uk>
Reply-To: name#domain.co.uk
The email delivers fine to all outlook.com and gmail.com addresses. I've tested however when sent to our office which runs Microsoft Exchange 2003 the email doesn't deliver. It doesn't even reach the logs which baffles me. Exchange isn't blocking the email, it isn't even receiving it.
Is there a known PHP / Exchange 2003 conflict? Or is the issue elsewhere?
I'm happy to supply more code if needed.

You seem to be using php's native mail function. It is often cumbersome to configure and troubleshoot.
You may save yourself a lot of time by looking into PHPMailer or Swift Mailer. These libraries are constantly maintained and fixed to stay compatible with exchange and other systems.
Also, with exchange it may be easier for you to set up an email account on it and let your application use it.

I had the same symptoms a few years back and if your problem is the same as mine you'll be surprised to find out it has nothing to do with your code.
In my case I was sending mail from from an internally hosted web server to an also internally hosted mail server. The web server was in the DMZ interface(a firewall/networking term) and the mail server was on the internal interface. Each interface operates on a different IP subnet (again networking stuff). The DNS for the mail domain (mail.ourdomain.com) resolved to the external IP of our firewall--which is configured to route that traffic to the internal mail server. However, that configuration only worked for traffic coming from the outside interface (external traffic). Traffic originating from the DMZ interface (mail sent from our webserver) could not be routed to the internal mail server.
You can do a quick check to see if you have the same problem. SSH to your web server and attempt to ping the mail server using the dns name.
ping mail.domain.co.uk
If you get a response, then this may not be the same problem.
Otherwise check with whoever is in charge of the firewall / network security and work with them to figure out what you need to do to get traffic from the webserver to the mail server. In our case we added a hosts file entry for mail.domain.com resolving to the internal IP of the mail server instead of the external IP.

Related

Sending mail from webserver to external mail server with local domain name

This is an issue I've encountered several time and haven't yet found a decent solution for:
Sending an e-mail from a webserver on e.g. "domain.com", to info#domain.com which is hosted on an external mail server e.g. Google Mail
In my case I always send from PHP over Apache and often on shared hosting, but I can imagine this is the same case on other frameworks.
These e-mails always seem to be delivered to the local mail server, even if I set the MX records on that server to point to the right external mail server.
A solution for this is to use an external SMTP server, but this isn't always easy when you're working with clients that either need to set-up a new e-mail account on their server and provide the SMTP details or sign-up for a third-party SMTP server.
What is the solution for this? Is there no way around SMTP?
Most emai/MTA server "autoconfigure" themselves. They guess list of local email domains (doimans with locally hosted mailboxes).
In sendmail case you can turn it off adding the following line in sendmail.mc:
define(`confDONT_PROBE_INTERFACES', `True')
Documentation : cf/README - confDONT_PROBE_INTERFACES
I have had the same issue many many times (in my case using PHP on a LAMP stack).
Try/check the following.
If you are using cPanel or similar, set the MX records to the external mailserver (Google apps etc).
Set up an SPF record to allow your hosting website to send email (this way no need to configure SMTP).
This may not be applicable but if you are using something like phpmailer. Set the property $mail->isMail(); so it tries to use your SPF allowed local mail() function to send the email. Sorry for going off into very specific advice, but might help in your particular situation.
Worth checking there are not similarly named local mailboxes on your hosting box.
Hope this helps!

My SMTP service is not sending messages to external addresses

I have a local server which host PHP applications. My applications send emails to different addresses. However, I am noticing a problem that I am unable to figure out. It send emails to internal addresses but it won't send messages to external addresses.
I also have an exchange server on the same network. so for example I have these 2 "internal domain"
domain1.com
domain2.com
if I execute this php function from my application it gets delivered with no problem
mail("name#domain1.com","Test Subject","Test message");
However, if I try sening a message to gmail, yahoo or any external domain it does not send it
mail("name#gmail.com","Test Subject","Test message");
My server is running on Windows 2008 Server R2 with PHP5.3.19 installed on it via IIS6
How Can I correct this issue?
mail() on Windows needs extra config in php.ini because Windows does not have Sendmail or anything similar.
The first is the SMTP setting which should point to your ISP's outbound server which is not necessarily the same server that you retrieve your mail from. When I worked in support there were a lot of issues like you describe because an inbound server will still accept mail, but only for the domains that are local to it.
The second is the sendmail_from setting, which is what the gimpy little mailer PHP wrote for the Windows distribution uses in every SMTP transaction regardless of what's in the headers. On *nix Sendmail will read the headers and get the From: address itself, on Windows PHP will always use what's in php.ini. If this is a "garbage" address like example#example.com or something with a non-existant domain many mail servers will reject it outright. What I recommend is to change it to something decent in PHP.ini as well as adding the following line to your mailing scripts:
ini_set('sendmail_from', $from_address);
Alternatively, just use PHPmailer since it simply works much better than PHP's horrid little mail() function.

PHP mail() not working

I'm building a site on my home computer using MAMP. The code I'm using employs the PHP mail() function to send emails, but whenever I test it, the mails aren't getting sent.
My computer is connected to the net, but I'm wondering if there's something about local hosting that prevents mails from getting sent. I'm not getting any kind of error message.
Any ideas?
PHP can send mail in one of two ways.
The first, and the default on non-Windows systems, is to use the local mail transfer agent installed on the system. This would be "sendmail" or an application compatible with it, the most popular probably being postfix.
The other is to connect via SMTP to some mail server.
You will either need to install a mail transfer agent on your local system (and set it up correctly), or edit PHP's configuration to specify an SMTP server address and port.
Yes, there are things that could block locally hosted mail. For one, your ISP could block SMTP to servers other than the ISP. ask your ISP support if they block SMTP... Or try telexing so someone's MX port 25 and do you get a response?
If your ISP blocks smtp you can still send the mail, but first you must relay that email through a hosted email server like your ISP mail server. This process is called 'smart hosting' and you can search for more info.
Even if you are not blocked on port 25, many sites will refuse or lose smtp traffic that originates from a dynamic or residential IP address, so again the smart host suggestion.
Also I suggest not using the built in mail() function in PHP... Use something that replaces and improves it like http://pear.php.net/package/Mail or http://sourceforge.net/projects/phpmailer/. Again, use the SMTP method as it is way more reliable than direct sending or calling Sendmail.
It is important to confirm this problem, doing SMTP manually over telnet. That way you isolate the problem from PHP. I did ISP support for years and saw this question lots. Most people setup php and mail correctly but get stuck on a background network issue with SMTP.
If you have Wireshark installed, it can record network traffic and you might see the actual SMTP traffic, for example the remote server may be refusing your connection. Wireshark is helpful but not required to solve this though. Good luck.
You need to setup SMTP server in order to be able use mail function, or you can use PHPMailer class, with it you can avoid using mail function and setup problems, PHPMailler need socket extension to be loaded in order to function correctly.

Exchange rejecting php mail from same domain

I have an IIS server running PHP on an Apache Module. I am running a domain on it, and this domain has a seperate setup for email which uses an exchange server. When I try to send email from the website using php mail() the exchange server rejects it. I.e. the header from line is sender#this-domain.com and it is sending to receiver#this-domain.com, both are email addresses set up in the exchange, even though the sender has no direct relationship with exchange.
The emails are not getting through. We tried using a Yahoo adress in the from line but nothing. Has anyone ever come across a problem like this.
You have to either configure the exchange server to accept relay requests based on the origin ip or some transparent authorization mechanism (don't know much about IIS and Exchange and integrated windows authentication, but the good people over at http://serverfault.com do. ). Or use some mailing library that can handle smpt authentication like e.g. http://swiftmailer.org
I once ran into a problem like this which involved the Exchange server not wanting to accept anonymous connections or that it would only accept mail from certain SMTP servers. Have you checked the configuration on your Exchange server to eliminate those possibilities?

will php's mail() attempt to deliver to localhost?

We are having a problem with a web application delivering emails. Suppose the site lives on domain.com. Emails sent to manager#domain.com aren't being recieved by the client, but when we set it to developer#developersdomain.com or manager#aliasdomain.com they are received by the developer and the manager, respectively.
Is php's mail() command delivering email addresses to the location domain to localhost, instead of routing it through a mail server?
Sadly, this is the way that the mailserver itself works. It knows the machine it's on is domain.com and assumes the mailbox should be a local box.
This is why it's a good idea to have your server's domain name actually be a subdomain, even if it's just www.domain.com. That way, mail addressed to manager#domain.com is sent on to the mail server specified by domain.com's MX record.
That'll be a setting on your mail software at the serverside I guess! Check your software settings.

Categories