LibCURL and email

Started by degac, November 11, 2017, 08:43:03

Previous topic - Next topic

degac

Hi
I'm trying (again...) to use LibCURL to send email from a BlitzMax application.
As I found many difficulties, my solution was to upload to a server the 'messages & attached file' and then use some PHP script on the server-side to accomplish everything.
But I'm in the situation I can't - for security reasons - use this approach, so I re-tried to use LibCURL (that can do it!) to send directly email from a BlitzMax application (an error message and some log files in the most frequent case, a PDF file in one case).

I've tried (with a working server and user/password), but I found only errors...

case 1 - error - using (as URL) mail.servername.net:587 (note - NO SMTP://

Quote
220 m-r2.th.seeweb.it ESMTP Postfix (Debian/GNU)
502 5.5.2 Error: command not recognized
221 2.7.0 Error: I can break rules, too. Goodbye.
* About to connect() to mail.****.net port 587 (#0)
*   Trying x.xxx.xxx.xxx..
* connected
* Connected to mail.*****.net (x.xxx.xxx.xxx) port 587 (#0)
* Server auth using Basic with user 'webmaster@********'
PUT / HTTP/1.1
Authorization: Basic d2VibWFzdGVyQGJsaXR6bWF4Lm5ldDpzb2VjaWVwaA==
Host: mail.*******.net:587
Accept: */*
Transfer-Encoding: chunked
Expect: 100-continue

* Connection #0 to host mail.****.net left intact
* Closing connection #0



case 2 - specifying SMTP://mail.servername.net:587
Quote
* About to connect() to mail.*****.net port 587 (#0)
*   Trying x.xxx.xxx.xxx..
* connected
* Connected to mail.******.net (x.xxx.xxx.xxx) port 587 (#0)
< 220 m-r2.th.seeweb.it ESMTP Postfix (Debian/GNU)
EHLO Base
< 250-m-r2.th.seeweb.it
< 250-PIPELINING
< 250-SIZE 102400000
< 250-VRFY
< 250-ETRN
< 250-STARTTLS
< 250-AUTH DIGEST-MD5 PLAIN LOGIN CRAM-MD5
< 250-AUTH=DIGEST-MD5 PLAIN LOGIN CRAM-MD5
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 DSN
AUTH DIGEST-MD5
< 334 cmVhbG09IiIsbm9uY2U9IjJydXdPeWRHaUFOM3Y1YXFjc1JONXc9PSIscW9wPSJhdXRoIixjaGFyc2V0PSJ1dGYtOCIsYWxnb3JpdGhtPSJtZDUtc2VzcyI=
QUIT
< 535 5.7.8 Error: authentication failed: cmVhbG09IiIsbm9uY2U9IjJydXdPeWRHaUFOM3Y1YXFjc1JONXc9PSIscW9wPSJhdXRoIixjaGFyc2V0PSJ1dGYtOCIsYWxnb3JpdGhtPSJtZDUtc2VzcyI=
* Closing connection #0
* Login denied

I understand that there's a problem - in the second case - of authentication, but I'm blocked now!
Anyone has ever used libCURL to send email?

Thanks anyone!

Cheers

Here's the code...

Code: BASIC

SuperStrict
Rem
curl example
https://curl.haxx.se/libcurl/c/smtp-tls.html
LIBCURL - EMAIL TEST
End Rem
Framework BaH.libcurl
Import BRL.StandardIO
Import BRL.filesystem
Local _to$,_from$,_cc$,_URL$,_USER$,_password$
_URL="mail.your site.net:587"
_USER="valid email"
_password="password"
_to="email_to"
_from="email_from"
_Cc=""
Local mail_message$="Date: \r\n"+..
  "To: "+_To+"\r\n"+..
  "From: "+_FROM+" (Example User)\r\nCc: "+_CC+..
" (Another example User)\r\nMessage-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"+..
"rfcpedant.example.org>\r\nSubject: SMTP TLS example message\r\n\r\n"+..
"The body of the message starts here.\r\n\r\n"+..
"It could be a lot of lines, could be MIME encoded, whatever.\r\nCheck RFC5322.\r\n"

Local payload:Tpayload=Tpayload.Create(mail_message)

Local curl:TCurlEasy = TCurlEasy.Create()

curl.setOptInt(CURLOPT_VERBOSE, 1)
curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)
curl.setOptString(CURLOPT_USERNAME, _USER)
curl.setOptString(CURLOPT_PASSWORD, _PASSWORD)
'????curl.setl_easy_setopt(curl, CURLOPT_USE_SSL, (Long)CURLUSESSL_ALL)
curl.setOptString(CURLOPT_URL, _URL)'587 means TSL (Transport Security Layer activated...)

curl.setOptString(CURLOPT_MAIL_FROM,_from)
curl.setOptString(CURLOPT_MAIL_RCPT,_to)

'this should resolve the problem, taken from a working example
'to upload a file - it should work in the same way
'see Tpayload class belove (a stupid trick to 'save' and reload it...)
'not working with 'attached' file I suppose...

'curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
'curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl.setReadCallback(payload.Read, payload) 'set the callback????
curl.setOptInt(CURLOPT_UPLOAD, True)
Local res:Int = curl.perform()
curl.cleanup()
End
'not a so elegant solution... but maybe it works!
Type Tpayload

Field name:String
Field size:Int
Field currentPos:Int

Field stream:TStream

Function Create:Tpayload(file:String)
Local this:Tpayload= New Tpayload

Local f:TStream=WriteFile("message_email.txt")
If f
WriteLine f,file
CloseStream f
End If
file="message_email.txt"
this.name = file
this.size = FileSize(file)
Print "Message size: "+this.size

Return this
End Function

Method DoRead:Int(BUFFER:Byte Ptr, size:Int)
If Not stream Then
stream = ReadStream(name)
currentPos = 0
End If

Local count:Int = Stream.Read( BUFFER, size )
currentPos :+ count

Return count
End Method

Function Read:Int(BUFFER:Byte Ptr, size:Int, data:Object)
Return Tpayload(data).DoRead(BUFFER, size)
End Function

End Type
If there's a problem, there's at least one solution.
www.blitzmax.org

Derron

Minor question:
Is your mail server accepting TLS? In the past I had trouble with SSL/TLS and Let's Encrypt. With my previous SSL certificate SSL/TLS was working as expected, with LE I needed to set to STARTLS (:143).

Did you try passing the same commands via a terminal? Shouldn't that work - if the commands are right.

The "5.7.8 Error: authentication failed" is normally just telling you that the credentials are incorrect.


bye
Ron

degac

 :'(
(facepalm... or head on wall... you choose!)

Just noticed that the 'mail account' (created automatically by the hoster/server) is name_serberNUMERS... and - of course - different from the FTP interface... damn!
I spent 2 days (this week!...) many thanks! I will check when at home on PC the (surely!) other problems I will find!
If there's a problem, there's at least one solution.
www.blitzmax.org

degac

#3
No luck!
created a new account, checked and rechecked username/password.
Installed an email client (eM Client) and username/password/parameters are correct.
I think there's a problem (revealed in eM Client) about the 'certificate'... but I can ignore and - in the email client - everything works without problems.
Probably libCURL needs some specific parameters to bypass this.

Investigating...

edit:

I found (in the bah.libcurl source code) these two constants...

CURLOPT_TLSAUTH_USERNAME
CURLOPT_TLSAUTH_PASSWORD
(CURLOPT_TLSAUTH_TYPE,"SRP"

it *seems* to work, but then I get a MEMORY ACCESS VIOLATION - surely there's some problems with LibCURL & SSL layer (or old version of libCURL on my configuration! I'm using LibCURL 7.28.1, today we ara at 7.56.1 so...)

Quote
* About to connect() to mail.***.net port 587 (#0)
*   Trying x.xxx.xxx.xxx...
* connected
* Connected to mail.***.net (x.xxx.xxx.xxx) port 587 (#0)
< 220 m-r2.th.seeweb.it ESMTP Postfix (Debian/GNU)
EHLO Base
< 250-m-r2.th.seeweb.it
< 250-PIPELINING
< 250-SIZE 102400000
< 250-VRFY
< 250-ETRN
< 250-STARTTLS
< 250-AUTH DIGEST-MD5 PLAIN LOGIN CRAM-MD5
< 250-AUTH=DIGEST-MD5 PLAIN LOGIN CRAM-MD5
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 DSN
MAIL FROM:<w1@****.net>
< 250 2.1.0 Ok

edit2:
I've found (somewhere on a .zip file on my computer!) a more recent libCURL module (7.51) - installed, compiled and... same error (MAV), same output result.


If there's a problem, there's at least one solution.
www.blitzmax.org