Is it possible to send mail through an exchange server from Linux via php?

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
I need to be able to send mail from a php application at work, but we have an exchange server, and not a normal mail server and there's no way they would agree to open up an smtp relay and I guess I don't really blame them.

Is there a way to programatically talk to the exchange server directly to send mail if you have the username/password of an account that has a mailbox? Basically it would bypass Outlook completely.

Worse case scenario I can have the machine hosting the VM actually have Outlook open then have some kind of script to relay the info to, but I'm hoping to avoid anything dirty like that.

Oh, and I think we're running exchange server 2010. I'm just guessing though, because we have Office 2010 and windows 7, so I presume they may have done the exchange upgrade at the same time. A few years back they were upgrading a lot of stuff from XP/2003 platform. I have no access or say with the exchange server though so whatever method I go with needs to work without requiring any changes to the server.

Essentially it's for a departemental app I'm working on that is a sorta gray area, boss and everyone else is cool with it, but it's something that needs to stay within the bounds of the department, we can't be changing stuff like server settings etc. Otherwise I'd just ask to see if they can enable SMTP and then I could just use that.
 
Last edited:
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
If your company has an AWS account you could use SES, it supports SMTP.

I have no idea about the existence of Exchange libraries for PHP, I'm guessing your Google search came up empty?
 

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
Yeah not finding much on google. I found someone suggesting phpmailer, but pretty sure that's for SMTP only, ex: standard email. Exchange is kind of it's own separate beast that is integrated deeply into a windows/AD environment, so not sure how easy it is to talk to outside of that. Some suggested enabling SMTP relay but there's no way they'd want to do that, for good reason. I have no control over that. There is no way they would want to get AWS, if they wanted to use an external service I'd just ask to use my own SMTP server. While the info in any emails that will get sent is not really sensitive, I still rather not do anything that makes it leave the network unless it has to, anyway. (ex: if going to internal recipients it stays local)

I still need to check with IT anyway, maybe I'll be surprised and they'll actually want to work with me, but pretty sure they won't want to enable anything extra on the server, I need to work with the way it's setup. Basically I need to emulate an Outlook connection.

Actually, that is another option, I think there are flags to mailto: where I can prepopulate an email. That may actually work, is there anything else similar that is more powerful that I can look into? I presume with mailto I'm kinda limited in how big the body can be or if I can even have returns. If I have a simple button that the user presses and it spawns a new email in their Outlook, that would actually work I think. As long as it does not do some weird silly business with delays. We had this Access app before but ever time it spawned an email you had to click a bunch of crap and wait before it sends it.
 
Last edited:

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Office on desktops does have a COM automation interface, but I've never had a reason to dig into it.
 

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
I might have to give that a try. Failing that, I might just go the mailto route, anyone know if there's a limit to how big of a message can be prefilled? Since you can do stuff like this:

this (Edit: ok, so that totally failed. Could have sworn you could do this?)

Not sure how flexible this is though, but it may actually be acceptable, the I don't have to worry about the server having to send mail, it will just rely on the client. Of course if i get server side working then I can also implement stuff like automated emails (ex: reminders or other features I may choose to add to the program)
 

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,103
126
Haven't programmed for a long time.

There are add-attachment.php, create.php & send.php example files in that folder that you can look into, however.

Good luck!
 

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
Haven't programmed for a long time.

There are add-attachment.php, create.php & send.php example files in that folder that you can look into, however.

Good luck!

Pretty sure those functions are just for standard email and won't work with exchange. (ex: relaying to an SMTP server)

Actually, that got me thinking, is there anything I can do on the server side (the Linux one in the office that I manage), ex: setup a local SMTP server that can then talk to exchange? That might be an easier route. Essentially I can relay to the local SMTP server using normal php email functionality, and the SMTP server can talk to exchange to send the mail in exchange's protocol. Basically act like an Outlook instance. I'm thinking that might be possible actually, I will google that further.
 

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,103
126
Pretty sure those functions are just for standard email and won't work with exchange. (ex: relaying to an SMTP server)

But it says:

The PHP Exchange Web Services library (php-ews) is intended to make communication with Microsoft Exchange servers using Exchange Web Services easier. It handles the NTLM authentication required to use the SOAP services and provides an object-oriented interface to the complex types required to form a request.

Actually, that got me thinking, is there anything I can do on the server side (the Linux one in the office that I manage), ex: setup a local SMTP server that can then talk to exchange? That might be an easier route. Essentially I can relay to the local SMTP server using normal php email functionality, and the SMTP server can talk to exchange to send the mail in exchange's protocol. Basically act like an Outlook instance. I'm thinking that might be possible actually, I will google that further.

Maybe. But earlier you said your IT dept would not open up SMTP.
 
Last edited:

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
That's a bulletin board limitation. Try this

Oh ok good to know that works! Will be my fallback plan.

But it says:

The PHP Exchange Web Services library (php-ews) is intended to make communication with Microsoft Exchange servers using Exchange Web Services easier. It handles the NTLM authentication required to use the SOAP services and provides an object-oriented interface to the complex types required to form a request.



Maybe. But earlier you said your IT dept would not open up SMTP.


Oh I missed that part. As for SMTP I was talking about setting up SMTP on my own server, I can do what I want there as it's my own VM running on a workstation (not really an ideal setup, but do what you can with what you have basically). So I wonder if there's like a postfix extension where it can relay to exchange using Exchange's protocol. So essentially as far as php is concerned it would be regular SMTP using the local server on the same VM, but then that server would use Exchange/Outlook protocol to send the mail through exchange so it can go off the network or to others on the network. Will need to research on that further I'm sure there's something. Though the php one that does exchange directly may very well work too.

I will be working tonight so I'll read on this stuff further and see what I can do. I still have to code the actual system that will use this so the email part is actually like the final step so I have lot of time to think/read on it. Worse case scenario I will use the mailto method and it will just open a new email in outlook.
 

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
Maybe I'm putting the cart before the horse here. I'm at work and I just telneted to port 25 on the exchange server, looks like SMTP actually IS open! This may end up making my life easier. I have not tried any commands to send mail though but if the port is open and displays a SMTP banner that is a good sign. I can then use standard SMTP protocol to send the mail. I presume it would require authentication and would use the AD user/pass combination.

I did not figure they would have that actually open, so I had not thought of checking before.

Though I may still talk to IT before I play with that just in case seeing mail going through that way might raise any flags.
 

razel

Platinum Member
May 14, 2002
2,337
93
101
You just need to configure the mail object. Then use the same object to send. Often SMTP to point to your exchange server. Chances are you can connect but they do not allow your server to relay messages to exchange.
 

Red Squirrel

No Lifer
May 24, 2003
70,084
13,533
126
www.anyf.ca
Yeah I knew about those settings, just did not figure that they had SMTP turned on the exchange server, as exchange itself is rather proprietary, at least internally. I did not really play with this yet as I have not gotten to that point in the application.

I may give it a try to see if I can authenticate with a regular user account on the SMTP port or if it even allows relay, but failing that I might just do the mailto: link method.
 

razel

Platinum Member
May 14, 2002
2,337
93
101
Even if your use an existing Exchange email account, the Exchange server will not accept your server or dev machine's mail until it is allowed.

You can actually debug this yourself by telnetting to that Exchange server's mail port. You can then compose the email via telnet and see how far it goes. They all tend to accept EHLO (misspelled hello) from your internal network.