Trying to send Email

Started by Hardcoal, January 04, 2025, 22:51:27

Previous topic - Next topic

Hardcoal

can anyone figure, why this dont work..
Im trying to send email..

and it writes email sent successfully, but it does not really send it
Thanks
SuperStrict
Import brl.standardio
Import bah.libcurl

Function SendEmail(smtpServer:String, userName:String, password:String, recipient:String, subject:String, body:String)

    Local curl:TCurlEasy = TCurlEasy.Create()
   
    If curl = Null
        Print "Failed to initialize cURL"
        Return
    EndIf

    ' Configure SMTP settings
    curl.setOptString(CURLOPT_URL, smtpServer) ' SMTP server address (e.g., "smtp://smtp.example.com:587")
    curl.setOptString(CURLOPT_USERNAME, userName)
    curl.setOptString(CURLOPT_PASSWORD, password)

    ' Set the sender email address
    curl.setOptString(CURLOPT_MAIL_FROM, "<" + userName + ">")

    ' Set the recipient email address
    Local rcptList:Byte Ptr = Null
    'rcptList = curlListAppend(rcptList, "<" + recipient + ">")
    curl.setOptBytePtr(CURLOPT_MAIL_RCPT, rcptList)

    ' Email headers and body
    Local payload:String = "From: " + userName + "\r\n"
    payload :+ "To: " + recipient + "\r\n"
    payload :+ "Subject: " + subject + "\r\n"
    payload :+ "\r\n" ' Blank line between headers and body
    payload :+ body

    ' Set the payload as the email data
    curl.setOptString(CURLOPT_POSTFIELDS, payload)

    ' Perform the request
    Local result:Int = curl.perform()
    If result = CURLE_OK
        Print "Email sent successfully!"
    Else
        Print "Failed to send email: " + CurlError(result)
    EndIf

    ' Cleanup
    'If rcptList <> Null Then curlListFree(rcptList)
    curl.cleanup()
EndFunction

' Usage
SendEmail("smtp://smtp.gmail.com:587", "hardcoal@gmail.com", "xxxxxx", "hardcoal@walla.com", "Hello", "This is a test email LaLa.")
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Qube

Quick scan over your code and it looks like you're missing the requirement for SSL and setting the TLS version.

Hopefully this will be of use.
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD.
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32GB DDR5 RAM 1T PCIe 4.0 SSD.
Microsoft Surface Pro 11 ( Snapdragon® X Elite ), 16GB RAM, 512GB SDD.
ASUS ROG Swift OLED PG27AQDM OLED 240Hz.

Until the next time.

Hardcoal

#2
QuoteASUS ROG Swift OLED PG27AQDM OLED 240Hz.
You got oled.. Very nice

When I try to send Email from blitzmax..
Is the blitzmax like an email server?
anyways.. i keep trying to understand this,
but i admit i just want it to work


I tried this pop thing as well


Strict

Import brl.standardio
Import brl.socket

Type Email
    Field number:Int
End Type

Global POP3_Fail:Int

Const FAIL_None:Int = 0
Const FAIL_NoServer:Int = 1
Const FAIL_User:Int = 2
Const FAIL_Pass:Int = 3

Function OpenPOP:TStream(pop3:String, user:String, pass:String)
    Local pop:TStream = OpenStream(pop3, 110)
    If pop
        Local welcome$ = ReadLine(pop)
        If OK(welcome$)
            WriteLine(pop, "USER " + user)
            Local userResponse$ = ReadLine(pop)
            If OK(userResponse$)
                WriteLine(pop, "PASS " + pass)
                Local passResponse$ = ReadLine(pop)
                If OK(passResponse$)
                    POP3_Fail = FAIL_None
                    Return pop
                Else
                    ClosePOP(pop)
                    POP3_Fail = FAIL_Pass
                    Return Null
                EndIf
            Else
                ClosePOP(pop)
                POP3_Fail = FAIL_User
                Return Null
            EndIf
        EndIf
    Else
        POP3_Fail = FAIL_NoServer
        Return Null
    EndIf
End Function

Function ClosePOP(stream:TStream, remove:Int = 0)
    If stream
        If Not remove
            WriteLine(stream, "RSET")
            ReadLine(stream) ' "+OK Markers cleared"
        EndIf
        WriteLine(stream, "QUIT")
        Local quit$ = ReadLine(stream)
        CloseStream(stream)
        If Not OK(quit$)
            Return False
        EndIf
        Return True
    EndIf
End Function

Function GetEmailList:TList(stream:TStream)
    If stream
        Local emailList:TList = New TList
        WriteLine(stream, "LIST")
        Local list$ = ReadLine(stream)
        If OK(list$)
            Repeat
                Local entry$ = ReadLine(stream)
                If entry$ = "." Then Exit
                Local e:Email = New Email
                e.number = Int(Left(entry$, Instr(entry$, " ") - 1))
                emailList.AddLast(e)
            Forever
        EndIf
        Return emailList
    EndIf
End Function

Function ReadMessage(stream:TStream, number:Int, remove:Int = 0)
    If stream
        WriteLine(stream, "RETR " + number)
        Local msg$ = ReadLine(stream)
        If OK(msg$)
            Print "----------------------------------------------------------------------------"
            ' Read header (read until blank line)...
            Repeat
                Local header$ = ReadLine(stream)
                If header$.Trim() = "" Then Exit
                Local pos:Int = Instr(header$, ": ")
                If pos
                    Select Left(header$, pos - 1)
                        Case "From", "To", "Subject", "X-Mailer", "Date", "Message-ID"
                            Print header$
                    End Select
                EndIf
            Forever
            Print "----------------------------------------------------------------------------"
            Print ""
            ' Message body (read until single period)...
            Repeat
                msg$ = ReadLine(stream)
                If msg$ = "." Then Exit
                If msg$ = ".." Then msg$ = "."
                Print msg$
                If KeyHit(KEY_ESCAPE) Then Exit
            Forever
            Print ""
        EndIf
        If remove
            WriteLine(stream, "DELE " + number)
        EndIf
    EndIf
End Function

Function ReadAllEmails(stream:TStream, remove:Int = 0)
    If stream
        Local emailList:TList = GetEmailList(stream)
        For Local e:Email = EachIn emailList
            ReadMessage(stream, e.number, remove)
        Next
    EndIf
End Function

Function OK(result$)
    Return Left(result$, 3) = "+OK"
End Function

Function GetStat$(stream:TStream)
    If stream
        WriteLine(stream, "STAT")
        Return ReadLine(stream)
    EndIf
End Function

' ----------------------------------------------------------------------------
' Demo
' ----------------------------------------------------------------------------

Local pop3:String = "pop.gmail.com"
Local user:String = "hardcoal@gmail.com"
Local pass:String = "xxxxxxxxxxx"

Print ""
Print "Connecting to POP3 server..."
Print ""

Local pop:TStream = OpenPOP(pop3$, user$, pass$)

If pop
    ReadAllEmails(pop)
    ClosePOP(pop)
Else
    Select POP3_Fail
        Case FAIL_NoServer
            Print "Cannot find server!"
        Case FAIL_User
            Print "Username doesn't exist!"
        Case FAIL_Pass
            Print "Incorrect username or password!"
    End Select
EndIf

Print ""
Print "Press any key to exit..."
WaitKey()

------------------------------

I got Error

Connecting to POP3 server...
Cannot find server!

Press any key to exit...


Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Midimaster

It is no good idea to send an e-mail on a user's computer in the background. This is the stuff, where spam bots are made from. You need to know the user's pop-account/password or you need to hardcode your pop-account/password into the bmx file.

better way:

normally you would prepare the content of an email and offer it to the standard mail app, the user has installed. This means his standard mail app pops up, shows the prepared mail and the user can now decide it to send or to cancel it.

Another way is to send the data as html request to your (developer's) homepage, where a php script receives it and sends the e-mail. This enables to keep control over the real mail sending.

 
...back from North Pole.

Hardcoal

I think thats too much for me.. 
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Derron

Quote from: Midimaster on January 06, 2025, 16:27:34Another way is to send the data as html request to your (developer's) homepage, where a php script receives it and sends the e-mail. This enables to keep control over the real mail sending

That results in the same issue: someone can spam your website with requests and you will get a lot of mails.
Of course you can mitigate such stuff by limiting the amount of mails per hour from a given IP, mails at all, ... but ...yeah you understood - this is an attack vector and very prone to have issues in the website script which increases risks...

Exposing your Mail address also bears risks - as usual, but if you offer some gmail one then their servers will take the load and do the filtering.

Best is - as you described, to ask the user if you want to open up the default mail client and prefill some new-mail-content. Be aware that people without an setup'd mail client will be greeted by these "welcome, first steps"-stuff of the mail client ... can be annoying too.


This aside:
Maybe ask what hardcoal is _really_ trying to do. Maybe asking the user for credentials is absolutely ok. Regarding pop3 ... not all want their mails to be fetched to local, most today will prefer imap (keep it on the server) potentially only archiving "old mails" to local in case of limited plans or being afraid of google reading to many of your mails :D


bye
Ron

Hardcoal

My goal is that lets say i have an app.. and i want the app to open new options only if you send this app to two other friends..
so my goal is to be able to have it sent from the app.

Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Midimaster

So the app (you) only need two e-mail-addresses. I would do this with an HTTPS-post to a php-script on your server, where the php script collects the e-mail-addresses and sends the two invitations and anythings else you want to do (Sending a copy to you, Checking if adress is already known, etc...

Send three strings in one POST-call: 
1. The new e-mail-adress
2. The user who invites his friend
3. A secret string to ensure, that it comes from your app

As it is PHP nobody can see how you process the data on your server*
As it is POST nobody can see what the data are
As it is HTTPS you need no passwords or account data

As a result of his HTTPS-post the user can immediately receive the OK for the options. This could be a kind of checksum of the sent data. So the OK is individual.


* If anybody call your php script from within a browser, he would not see the code, but an empty page or a predefined html text.

...back from North Pole.

Hardcoal

Im so unfamiliar with this field 
Thanks MidiMaster. I guess Ill have to read about it more 
and experiment 
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Derron

DO NOT send to 2 addresses via your webmail.

This is ... simply said: SPAM. These receivers did not opt in to receive emails from you. Most likely your mail address (and your mail server) will be banned / blacklisted for spamming.


Simply check how others approach such stuff:
- identify a computer (so you can check if you know this computer already)
- have people sign up for accounts (so computer ids are stored to your account etc)
- give them referal-IDs
- people who sign up can write who refered them (the first user's referal-ID) - it is up to the advertising user how they reach out to "potential new users" - so nothing you will be blamed for directly (no spam ...)
- they sign up (see above ... identify a computer, have people sign up ...) and store in the referal-IDs-corresponding original user, that they brought you an additional user.

Yes, this is not that easy to do ... but better than burning your mail address or mail server (and attention: some blacklists do not just block your host... they block your IP (you might share it with someone) or complete IP ranges - which most likely will cause a serious letter from your provider ...or worse)

bye
Ron

Hardcoal

#10
Good point.. Ill think of Another way
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]