Page 1 of 1

Extended error info for email library needed

Posted: Thu Dec 19, 2024 11:48 am
by Fangbeast
I've got this little procedure to send email from one of my forms. It gets it's information for the server from a json preferences file. Works fine, no crashes etc. The problem is that no packet is sent and I cannot get any extended information from the routine apart from the obvious #PB_Mail_Error code that doesn't actually tell me what the error is from the server I use.

It's getting the progress ID at:
ProgressId.i = MailProgress(OutgoingMailId.i)
Debug GetLastErrorString(ProgressId.i)
and passing it to

Code: Select all

Procedure.s GetLastErrorString(Error.i = #False)
but I'm not getting any error string returned so I don't know where I'm at.

Anyone feel bloody minded enough to tell me where the mistake might be? I will sacrifice a politician on a spit for you.

Code: Select all

Procedure Address_EmailAddressSend()
  
  Protected Message.s
  
  MailToUser.s    = GetGadgetText(#Gadget_Addressemail_sentto)
  MailToSubject.s = GetGadgetText(#Gadget_Addressemail_subject)
  MailToBody.s    = GetGadgetText(#Gadget_Addressemail_message)
  
  ; Get email details from json file
  
  If  PGM::Get("smtp human name")      And 
      PGM::Get("smtp server name")     And 
  Val(PGM::Get("smtp server port"))    And 
      PGM::Get("smtp user name")       And 
      PGM::Get("smtp server password") And 
      PGM::Get("smtp email address")
    
    If MailToUser.s And MailToSubject.s And MailToBody.s
      
      OutgoingMailId.i = CreateMail(#PB_Any, PGM::Get("smtp email address"), MailToSubject.s)
      
      If OutgoingMailId.i <> #False
        SetMailAttribute(OutgoingMailId.i, #PB_Mail_XMailer, "Stormy")
        SetMailBody(OutgoingMailId.i, MailToBody.s)
        AddMailRecipient(OutgoingMailId.i, MailToUser.s, #PB_Mail_To)
        ; PB 6.20 beta 1
        
        If SendMail(OutgoingMailId.i, PGM::Get("smtp server name"), Val(PGM::Get("smtp server port")), #PB_Mail_UseSSL | #PB_Mail_UseSMTPS, PGM::Get("smtp user name"), PGM::Get("smtp server password"))  <> #False
          
       ; PB 6.10, 6.12
          
       ; If SendMail(OutgoingMailId.i, PGM::Get("smtp server name"), Val(PGM::Get("smtp server port")), #PB_Mail_Asynchronous | #PB_Mail_UseSSL, PGM::Get("smtp user name"), PGM::Get("smtp server password"))  <> #False
          
          Repeat
            ProgressId.i = MailProgress(OutgoingMailId.i)
            Debug GetLastErrorString(ProgressId.i)
            If ProgressId.i = #PB_Mail_Connected
              MailMessage("Connected to the " + PGM::Get("smtp server name")  + " email server", "Info")
            ElseIf ProgressId.i = #PB_Mail_Error
              MailMessage("Error connecting and sending message to  " +  PGM::Get("smtp server name"), "Error")
              MailMessage("The email message packet has created some sort of error", "Error")
            ElseIf ProgressId.i = #PB_Mail_Finished
              MailMessage("The email message packet has been correctly sent", "Info")
            Else
              MailMessage("Sent " + Str(ProgressId.i) + " Bytes", "Info")
            EndIf
            Delay(300)
          Until ProgressId.i = #PB_Mail_Finished Or ProgressId.i = #PB_Mail_Error
        Else
          MailMessage("The mail packet could not be sent", "Error")
          Debug GetLastErrorString(ProgressId.i)
        EndIf            
      Else
        MailMessage("Could not create the outgoing email message", "Error")
      EndIf
    Else
      MailMessage("One or more of the recipient's details are empty", "Warn")
    EndIf
  Else
    MailMessage("One or more of the email server details are empty!", "Warn")
    MessageRequester("Server Details Error", Message.s  + #CRLF$ + #CRLF$ + "Correct this before trying to send an email.", #MB_ICONERROR)
  EndIf
EndProcedure

Code: Select all

Procedure.s GetLastErrorString(Error.i = #False)
  If Not Error.i
    Error.i = GetLastError_()
  EndIf
  Protected Buffer.i, ResultString.s
  If FormatMessage_(#FORMAT_MESSAGE_ALLOCATE_BUFFER | #FORMAT_MESSAGE_FROM_SYSTEM, 0, Error.i, 0, @Buffer, 0, 0)
    ResultString.s = PeekS(Buffer)
    LocalFree_(Buffer)
    ProcedureReturn ResultString.s
  EndIf
EndProcedure 

Re: Extended error info for email library needed

Posted: Thu Dec 19, 2024 9:48 pm
by PBJim
I tried my best, given your offer. I was able to send mail successfully, only if I remove #PB_Mail_UseSSL.

I found that ProgressId never has a value which matches your 'if' conditions, so something isn't right. For instance, all the lines such as below where you output MailMessage() are empty.

Code: Select all

MailMessage("Sent " + Str(ProgressId.i) + " Bytes", "Info")
I am using PB 6.00, so perhaps that is a factor, but at least it works for me without SSL.

Re: Extended error info for email library needed

Posted: Fri Dec 20, 2024 7:31 am
by Fangbeast
My code worked fine in the pb 6.xx series where there was no ssl requirement as I had to put my application on Google's legacy apps list.

I forgot to mention that I am testing 6.20 beta 1 with the new options and I did have something in the body field so I will test more.

Bisonte mentioned to me that the mail lib was not a wrapper for the API (Like many pb commands) so GetLastError_ will not work in this case.

Back to the drawing board!!!

Re: Extended error info for email library needed

Posted: Fri Dec 20, 2024 1:34 pm
by PBJim
Ah, that's why it was returning nothing then.

Anyway, it was a quick little test for me that I couldn't resist, even when needing to finish for the night and I had some fun guessing what your module did. I hurriedly wrote the below as a quick and dirty replacement for your Get() to make the code runnable.

Code: Select all

Procedure.s Get(type.s)
  
  Define value.s
  
  Select type.s
    Case "smtp human name"
      value.s = "Jim"
    Case "smtp server name"
      value.s = "mail.  etc."
    Case "smtp server port"
      value.s = "587"
; **
; ** etc. etc.
; **
  EndSelect
  
  Debug type.s + " ---> " + value.s
  ProcedureReturn value.s
  
EndProcedure