Fred why does PB's SendMail() not work with SendGrid.

Everything else that doesn't fall into one of the other PB categories.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Fred why does PB's SendMail() not work with SendGrid.

Post by swhite »

Hi

Sendgrid.com is a commercial email service that supports sending eMail via SMTP or HTTP. When I try to use the PB's SendMail function it never works with SendGrid's smtp server. However, if I use the Chilkat library with PB it works.

Fred can you explain why the PB's smtp library does not work with SendGrid's smtp server (smtp.sendgrid.net)?

Thanks,
Simon
Simon White
dCipher Computing
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by Dude »

I doubt there's anything wrong with SendMail(): it's very straight-forward and follows the SMTP protocol. Mail failures are usually due to the wrong port, password (Ascii vs Unicode), SSL setting, mail header, etc. Have you triple-checked that all these are correct? If just one of these is wrong, then the mail won't be sent.

Also, don't forget about two-factor and/or app authentication. I couldn't use SendMail() with Gmail until I discovered that I couldn't use my normal Gmail password with it: I had to use an app-specific password that Gmail provided, because Gmail knew it was a third-party app connecting (my PureBasic exe), rather than a human in a web browser.

Also, your anti-virus app and/or firewall might be blocking network activity for your exe? Try disabling them as a test.

[Edit] I just noticed this:
swhite wrote:Sendgrid.com is a commercial email service ... (smtp.sendgrid.net)
Are you using ".com" or ".net" for the SMTP settings? You have differing domains quoted above.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by Kukulkan »

Hi,

as Fred is using libcurl, it might be that the SSL protocol preferred by the mailserver is not working/supported. I've had the problem that the mailserver wanted one of these two login protocols during handshake (in this order):

250-AUTH GSSAPI NTLM

NTLM is fine but libcurl does not completely support GSSAPI (https://github.com/curl/curl/blob/maste ... #L290-L307). It has chosen GSSAPI and finally failed. I needed to disable GSSAPI using CURLOPT_LOGIN_OPTIONS in order to make it work with this mailserver. Sadly, I don't know if PB gives you that much control to either find out or even fix this.

Also, it simply might be an issue of the libcurl version used by Fred. Maybe some more recent version would do the trick?

I don't know if you're running into the same issue, but email offers a lot of traps and specialties to give you some headaches. Try to get some logging of the handshake to find out what the reason is.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by swhite »

smtp.sendgrid.net is correct url for their SMTP server. I have checked all the settings numerous times. I can access it if I use the Chilkat library with Purebasic and I have been using it for months on other application I have written in another development language. So there is something specific with PB's email functions in this regard. I did attempt to use Wireshark to see what was going on but I never saw any user name, password message etc. in the data captured. I did see that there were header checksum errors but I do not know enough about the details to know what part of the process this involved. I could see that the DNS lookup occurred but the process failed almost immediately after that.

I did not know about gmail's app specific password because I was just about to say that PB does not work with gmail either all though my other applications have had no trouble with the same gmail account. I suspect it has more to do with SSL protocols because it fails very early in the process from what I can see in wireshark. It would also be helpful if there was a way to report any email errors in PB.

I would be happy to supply test credentials if Fred thought it was worth testing.

Thanks,
Simon

Dude wrote:I doubt there's anything wrong with SendMail(): it's very straight-forward and follows the SMTP protocol. Mail failures are usually due to the wrong port, password (Ascii vs Unicode), SSL setting, mail header, etc. Have you triple-checked that all these are correct? If just one of these is wrong, then the mail won't be sent.

Also, don't forget about two-factor and/or app authentication. I couldn't use SendMail() with Gmail until I discovered that I couldn't use my normal Gmail password with it: I had to use an app-specific password that Gmail provided, because Gmail knew it was a third-party app connecting (my PureBasic exe), rather than a human in a web browser.

Also, your anti-virus app and/or firewall might be blocking network activity for your exe? Try disabling them as a test.

[Edit] I just noticed this:
swhite wrote:Sendgrid.com is a commercial email service ... (smtp.sendgrid.net)
Are you using ".com" or ".net" for the SMTP settings? You have differing domains quoted above.
Simon White
dCipher Computing
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by NicTheQuick »

swhite wrote:I did attempt to use Wireshark to see what was going on but I never saw any user name, password message etc. in the data captured. I did see that there were header checksum errors but I do not know enough about the details to know what part of the process this involved. I could see that the DNS lookup occurred but the process failed almost immediately after that.
Of course you can not find anything there because the connection is encrypted while sending the credentials.

I just tested sending Mails using my own Mailserver (which is very restrictive and only accepts the latest encryption mechanisms) using passwords up to a length of 140 characters and it works fine. So this should not be the issue.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by Kukulkan »

Libcurl offers very good logging which also contains unencrypted protocol messages. Maybe there is a chance to activate this by using CURLOPT_DEBUGFUNCTION to set a debug callback?

https://curl.haxx.se/libcurl/c/CURLOPT_ ... CTION.html

As a start, here is what we do (removed our specific stuff). But we do not use PB built in libcurl:

Code: Select all

ProcedureC.i _curlCb_debug(curl_Handle.i, curl_infotype, *char, size_t, *void)
  ; remove all trailing and leading CR and LF characters
  Protected msg.s = Trim(Trim(PeekS(*char, size_t, #PB_UTF8), #LF$), #CR$)
  Debug "CURL: " + msg.s
  ProcedureReturn 0
EndProcedure

curl_easy_setopt(curl_Handle.i, #CURLOPT_VERBOSE, 1)
curl_easy_setopt(curl_Handle.i, #CURLOPT_DEBUGFUNCTION, @_curlCb_debug())
By this you get a detailed log about all the handshake etc. Sadly, I don't know how to do this with built in libcurl functions by Fred. Looks like you can not use ImportC "libcurl.lib" because even InitNetwork() triggers linker errors then :-(

To make this shorter, maybe you can use curl commandline utility to emulate? If you have the same issue, curl command debug options may help you further...
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by swhite »

Hi

Just for completeness PB does work with Sendgrid without SSL on port 587 and 2525.

Simon
Simon White
dCipher Computing
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Solved: Fred why does PB's SendMail() not work with SendGrid

Post by swhite »

So my original problem was that SendGrid suggested that I use port 465 with SSL which I discovered did not work. On further investigation I found that Purebasic works with SendGrid with or without SSL on port 587. So port 587 is the solution to my problem with SendGrid.

Simon
Simon White
dCipher Computing
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Fred why does PB's SendMail() not work with SendGrid.

Post by Dude »

swhite wrote:Just for completeness PB does work with Sendgrid without SSL on port 587 and 2525
Yep, just like I said 6 weeks ago:
Dude wrote:Mail failures are usually due to the wrong port
;)
Post Reply