About Store Forum Documentation Contact
Donations:
309$/mo



Post Reply 
send mail from windows server
Author Message
yvanvds Offline
Silver Supporter

Post: #1
send mail from windows server
I thought i'd share this little piece of code. On linux you can use sendmail to send an email, but this is not available on a windows server. But you can send an email like this:

PHP Code:
if(SendMailSupported())
{
   
SendMail(fromNamefromMailclient.recoveryIDmailsubjectmessage);
} else
{
   
log("no sendmail support. Saving message for " client.recoveryID);

   
Str filename FFirst("C:/inetpub/mailroot/Pickup/recover""txt");
   
Str fileOnly SkipStartPath(filename"C:/inetpub/mailroot/Pickup/");
         
   
bool fileReady false;
   {
      
FileText f;
      if(
f.write(fileOnlyANSI)) {
         
f.putLine("From: " fromName " <" fromMail ">");
         
f.putLine("To: " client.recoveryID " <" mail ">");
         
f.putLine("Subject: " subject);
         
f.putLine(S);
         
f.putText(message);
         
f.putLine(S);
         
f.putLine(".");
         if(!
f.flush())
         {
            
log("Failed to send mail. Failed to flush file to disk.");             
         } else 
fileReady true;
      } else
      {
         
log("Failed to send mail. Failed to open file for writing.");
      }
   }
         
   if(
fileReady && !FRename(fileOnlyfilename))
   {
      
log("Failed to send mail. Failed to move to pickup dir.");
      
FDelFile(fileOnly);
   }


This assumes you have an smtp server installed and the user running your application has write access to C:/inetpub/mailroot/Pickup/

A few notable points are:

- You can't write to the pickup directory in one go. The file will be picked up by the system before you've completed writing to it.
- For some reason my files did not get sent in UTF8 or UTF16.
- Mind the emtpy line and a dot on a separate line after the actual message.
- The file must be closed before moving (renaming). In this example i've made sure this is the case by using extra braces.
01-06-2016 08:11 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: send mail from windows server
Thank you for sharing.

It's weird though that the SendMail does not work for you on Windows Server, as last time I've tested this on Win Server 2008 it worked ok, it was few years ago though.
Perhaps it's a permission issue? Maybe related to firewall or some other setting.
Or maybe you need to install SMTP software for the OS
You could try installing https://www.microsoft.com/web/downloads/platform.aspx
SMTP could be available under nternet Information Services (IIS) but I don't remember as that was long time ago.
https://youtu.be/xUF8fRIMYGk?t=5m25s
01-07-2016 05:00 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Silver Supporter

Post: #3
RE: send mail from windows server
Ah, I quickly assumed it was not supposed work because the 'sendmail' command is typically a linux command.

What is going on is that SendMailSupported() checks with an ipv6 address on a windows 2012 server. And although ipv6 is not disabled on my machine i cannot connect to port 25 with it. Connecting with ipv4 works. I tried it with telnet and am able to contact the smtp server. But with ipv6 is just fails.

I'm not sure why this is just yet. It might have something to do with my router, which only supports ipv4. I'll have to investigate further tomorrow.
01-07-2016 10:19 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: send mail from windows server
Hi,

Thanks for letting me know it's an IPv6/IPv4 problem.
'Bool SendMailSupported()' calls sock.connect(addr)
which does a check for:
Code:
switch(PLATFORM(WSAGetLastError(), errno))

Could you let me know what error is being returned there?

For example you can modify the source like that:
Code:
Int error=PLATFORM(WSAGetLastError(), errno);
LogN(S+"error_code:"+error);
   switch(error)

There is a workaround for "case PLATFORM(WSAEAFNOSUPPORT, EAFNOSUPPORT):" which I am curious if gets triggered, but fails.
Or if there's a different error code, which should be handled the same as the WSAEAFNOSUPPORT case.
01-08-2016 03:45 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Silver Supporter

Post: #5
RE: send mail from windows server
Hey. the error code returned was 10061, so WSACONNREFUSED. According to Microsoft:

Quote:No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.

But this is not true. The mail server is running, only not with ipv6. I've copied the code of the SendMail function but replaced

PHP Code:
if(sock.createTcp(DualStackSocket)) 

with
PHP Code:
if(sock.createTcp(false)) 


In this test I do recieve the mail I've sent. So it is only a matter of when to use ipv6. Of course, perhaps ipv6 should be enabled on my server. I actually know very little about that topic. I've just installed/configured this server and never came across something related to ipv6. I read that it should be enabled automatically.
01-08-2016 02:01 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: send mail from windows server
I was doing some googling but can't find anything related to this.

Is it possible, it's an anti-virus / firewall problem?

I think IPv6 should be enabled by default in the OS, no need to do anything, unless SMTP is not compatible with IPv6.

If this is not anti-virus / firewall issue, then I think a simple workaround would by doing 2 attempts (one with IPv6 if supported, and another with IPv4).
01-08-2016 03:27 PM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Silver Supporter

Post: #7
RE: send mail from windows server
As far as I know, Windows server does not install an anti-virus on its own. And i've disabled the firewall to be sure.

I also did a lot of googling and find lots of info on how to disable ipv6, not too much about enabling it. Which is logical if it should be enabled by default. I've read through that info anyway, and nothing indicates that ipv6 is disabled on my server. (For example, ping to localhost uses the ipv6 address.)

I don't think SMTP can be incompatible with IPv6, since IPv6 is on layer 3 of the OSI model, and SMTP is on layer 7.

As long as I'm the only one having this problem, I think the best workaround is to copy the contents of SendMail to my application. (It is the server app for my multiplayer game and I don't think I will need to run it someplace else very soon.) Or I can just keep using the code in the original post. It would be interesting to know if this also happens on other Windows 2012 servers, but I think not too much people are using them with esenthel right now.
(This post was last modified: 01-08-2016 05:10 PM by yvanvds.)
01-08-2016 05:06 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
RE: send mail from windows server
Can you replace Socket::connect(..
line
Code:
case PLATFORM(WSAEAFNOSUPPORT, EAFNOSUPPORT):
with
Code:
case PLATFORM(WSAEAFNOSUPPORT, EAFNOSUPPORT):
case WSACONNREFUSED:
And let me know if this is enough to make it work?
01-09-2016 06:02 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Silver Supporter

Post: #9
RE: send mail from windows server
That does not work. The connection attempt in that case returns WSAEAFNOSUPPORT.

I've added an extra log entry like this:

PHP Code:
SockAddr temp; if(temp.convert(addr))
         {
            if(::
connect((SOCKET)_s, (C sockaddr*)&temp._datatemp.size())!=SOCKET_ERROR)return CONNECTED;
            
Int error PLATFORM(WSAGetLastError(), errno);
            
LogN("wsa error code 2: " error);

            if(
PLATFORM(WSAGetLastError()==WSAEWOULDBLOCKerrno==EINPROGRESS))goto in_progress;
         } 

Which is a bit odd, because using the content of the SendMail function directly, with ipv4, works.
01-09-2016 07:29 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #10
RE: send mail from windows server
Many thanks for making the tests smile

I've added a workaround, in the following commits, it should work ok now:
https://github.com/Esenthel/EsenthelEngi...1d1b4ecb55
https://github.com/Esenthel/EsenthelEngi...d12a160e0f
01-10-2016 04:25 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Silver Supporter

Post: #11
RE: send mail from windows server
Thanks! Perhaps this whole thread could best be moved now, because it is not actually a usable code snippet :-)
01-11-2016 06:35 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #12
RE: send mail from windows server
moved to support / network section smile
01-12-2016 05:05 AM
Find all posts by this user Quote this message in a reply
Post Reply