How to send an email as html?

Just starting out? Need help? Post your questions and find answers here.
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

How to send an email as html?

Post by Johanson »

How to send an email as html (v.5.73 !!! )

it is impossible that there is a programming language that cannot send an email in html format.
:-(
:-(
:-(
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to send an email as html?

Post by infratec »

Hm ...

if I look at C, it can do nearly nothing.
It all depends on libraries.

If you can nothing find here in the forum, simply choose a library of your choice and write a wrapper. :wink:
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to send an email as html?

Post by infratec »

Last edited by infratec on Sun Nov 13, 2022 4:14 pm, edited 1 time in total.
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

infratec wrote:Hm ...

if I look at C, it can do nearly nothing.
It all depends on libraries.

If you can nothing find here in the forum, simply choose a library of your choice and write a wrapper. :wink:
what does it mean?:

Code: Select all

#PB_Mail_Custom : Add customs fields (can be multi-line).
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: How to send an email as html?

Post by Keya »

Send yourself a plaintext email saying Hello, then send yourself a HTML version of the same. Compare the differences and it becomes obvious
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

Keya wrote:Send yourself a plaintext email saying Hello, then send yourself a HTML version of the same. Compare the differences and it becomes obvious
such suggestions are ...
...
I know exactly how the structure should look like, but how do I send it in PB ???

Question:
Will the following notation work in PB 5.73:

Code: Select all

SetMailBody (0, "MIME-Version: 1.0" + # CRLF $ +
                       "Content-Type: multipart / alternative; boundary = '-------'" + # CRLF $ +
                       "Content-Language: en" + # CRLF $ +
                       "This is a multi-part message in MIME format." + # CRLF $ +
                       "---------" + # CRLF $ +
                       "Content-Type: text / plain; charset = utf-8; format = flowed" + # CRLF $ +
                       "Content-Transfer-Encoding: 7bit" + # CRLF $ +
                       "non html" + # CRLF $ +
                       "---------" + # CRLF $ +
                       "Content-Type: text / html; charset = utf-8" + # CRLF $ +
                       "Content-Transfer-Encoding: 8bit" + # CRLF $ +
                       "<html>" + # CRLF $ +
                       "<head>" + # CRLF $ +
                       "<meta http-equiv = 'content-type' content = 'text / html; charset = UTF-8'>" + # CRLF $ +
                       "<title> topic </title>" + # CRLF $ +
                       "</head>" + # CRLF $ +
                       "<body>" + # CRLF $ +
                       "<p> As <font size = '+ 2'> sss </font> <br>" + # CRLF $ +
                       "</p>" + # CRLF $ +
                       "</body>" + # CRLF $ +
                       "</html>" + # CRLF $ +
                       "-----------")
No !
Or maybe this:

Code: Select all

SetMailAttribute (#Mail, #PB_Mail_Custom, 
                       "MIME-Version: 1.0" + # CRLF $ +
                       "Content-Type: multipart / alternative; boundary = '-------'" + # CRLF $ +
                       "Content-Language: en" + # CRLF $ +
                       "This is a multi-part message in MIME format." + # CRLF $ +
                       "---------" + # CRLF $ +
                       "Content-Type: text / plain; charset = utf-8; format = flowed" + # CRLF $ +
                       "Content-Transfer-Encoding: 7bit" + # CRLF $ +
                       "non html" + # CRLF $ +
                       "---------" + # CRLF $ +
                       "Content-Type: text / html; charset = utf-8" + # CRLF $ +
                       "Content-Transfer-Encoding: 8bit")
Hmmm????
If it used to work (in previous versions), why did it suddenly stop?
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to send an email as html?

Post by infratec »

As written ...
it will not work with te e-mail functions of PB, since the original Content-Type is still present.
At the moment you can not replace it, you can only add an additional one.
I already added a feature request about this:
viewtopic.php?f=3&t=72187

You have to use the way I showed you with libcurl.
Last edited by infratec on Wed Mar 10, 2021 11:20 am, edited 1 time in total.
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

infratec wrote:As written ...
You have to use the way I showed you with libcurl.
POLINK: error: Unresollved external symbol
'_curl_easy_strerror'.
POLINK: fatal error: 1 unresolved external(s).
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

By the way:

Code: Select all

SendMail(#Mail, Smtp$ [, Port [, Flags [, User$, Password$]]])
... returns NONZERO if the mail was sent correctly and zero otherwise
but

Code: Select all

If CreateMail(0, "senderMail", "Subject: Hello !")
  SetMailBody(0, "Hello !")
  AddMailRecipient(0, "mailTO", #PB_Mail_To)
  Result = SendMail(0, "smtpserver", 465, #PB_Mail_Asynchronous, "user", "password")
  Debug "Result="+Str(Result)
  Repeat
    Progress = MailProgress(0):  Delay(300)
  Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
  
  If Progress = #PB_Mail_Finished
    MessageRequester("Information", "Mail correctly sent !")
  Else
    MessageRequester("Error", "Can't sent the mail !")
  EndIf
EndIf
Result=1 ==> NONZERO, but "Can't sent the mail !"

Why ???
dige
Addict
Addict
Posts: 1254
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: How to send an email as html?

Post by dige »

"Daddy, I'll run faster, then it is not so far..."
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to send an email as html?

Post by infratec »

Johanson wrote:
infratec wrote:As written ...
You have to use the way I showed you with libcurl.
POLINK: error: Unresollved external symbol
'_curl_easy_strerror'.
POLINK: fatal error: 1 unresolved external(s).
From where is your used libcurl.pbi?
Which OS are you using?
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

infratec wrote:
Johanson wrote:
infratec wrote:As written ...
You have to use the way I showed you with libcurl.
POLINK: error: Unresollved external symbol
'_curl_easy_strerror'.
POLINK: fatal error: 1 unresolved external(s).
From where is your used libcurl.pbi?
Which OS are you using?
Win10 x64
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

I tried
I remind you: I have version 5.73
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to send an email as html?

Post by infratec »

I added the mime stuff in my libcurl.pbi

viewtopic.php?p=591235

This code works:

Code: Select all

;
; LibCurl e-mail with html body
;

EnableExplicit

IncludeFile "libcurl.pbi"

#MailServer = "smtp://mail.example.com"

#FROM = "<sender@example.org>"
#To = "<addressee@example.net>"

#SMTP_User = ""
#SMTP_Password = ""

;#SKIP_PEER_VERIFICATION = #True
;#SKIP_HOSTNAME_VERIFICATION = #True

Define.i curl, i, res
Define.curl_slist *recipients, *headers, *slist
Define.curl_mime *mime, *alt
Define *part
Define inline_text$, inline_html$

Dim HeaderArray$(5)

HeaderArray$(0) = "Date: Wed, 10 Mar 2021 06:30:29 +0200"
HeaderArray$(1) = "To: "+ #TO
HeaderArray$(2) = "From: " + #FROM + " (Example User)"
HeaderArray$(3) = "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>"
HeaderArray$(4) = "Subject: example sending a MIME-formatted message"

inline_text$ = "This is the inline text message of the e-mail." + #CRLF$
inline_text$ + #CRLF$
inline_text$ + "It could be a lot of lines that would be displayed in an e-mail" + #CRLF$
inline_text$ + "viewer that is not able to handle HTML." + #CRLF$

inline_html$ = "<html>" + #CRLF$
inline_html$ + " <body>" + #CRLF$
inline_html$ + "  <p>This is the inline <b>HTML</b> message of the e-mail.</p>" + #CRLF$
inline_html$ + "  <br />" + #CRLF$
inline_html$ + "  <p>It could be a lot of HTML data that would be displayed by e-mail viewers able To handle HTML.</p>" + #CRLF$
inline_html$ + " </body>" + #CRLF$
inline_html$ + "</html>" + #CRLF$

curl = curl_easy_init()
If curl
  
  If #SMTP_User <> ""
    ; Set username And password
    curl_easy_setopt_str(curl, #CURLOPT_USERNAME, #SMTP_User)
    curl_easy_setopt_str(curl, #CURLOPT_PASSWORD, #SMTP_Password)
  EndIf
  
  
  If Left(#MailServer, 5) = "smtps"
    ; If you want To connect To a site who isn't using a certificate that is
    ; signed by one of the certs in the CA bundle you have, you can skip the
    ; verification of the server's certificate. This makes the connection
    ; A LOT LESS SECURE.
    ;
    ; If you have a CA cert For the server stored someplace Else than in the
    ; Default bundle, then the CURLOPT_CAPATH option might come handy For
    ; you.
    CompilerIf Defined(SKIP_PEER_VERIFICATION, #PB_Constant)
      CompilerIf #SKIP_PEER_VERIFICATION
        curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
      CompilerEndIf
    CompilerEndIf
    
    ; If the site you're connecting to uses a different host name that what
    ; they have mentioned in their server certificate's commonName (or
    ; subjectAltName) fields, libcurl will refuse To connect. You can skip
    ; this check, but this will make the connection less secure. */
    CompilerIf Defined(SKIP_HOSTNAME_VERIFICATION, #PB_Constant)
      CompilerIf #SKIP_HOSTNAME_VERIFICATION
        curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
      CompilerEndIf
    CompilerEndIf
  EndIf
  
  
  ; This is the URL for your mailserver
  curl_easy_setopt_str(curl, #CURLOPT_URL, #MailServer)
  
  
  curl_easy_setopt_str(curl, #CURLOPT_MAIL_FROM, #FROM)
  
  *recipients = curl_slist_append(*recipients, #To)
  curl_easy_setopt(curl, #CURLOPT_MAIL_RCPT, *recipients)
  
  ; Build and set the message header list.
  For i = 0 To ArraySize(HeaderArray$()) - 1
    *headers = curl_slist_append(*headers, HeaderArray$(i))
  Next i
  curl_easy_setopt(curl, #CURLOPT_HTTPHEADER, *headers)
  
  ; Build the mime message.
  *mime = curl_mime_init(curl)
  
  ; The inline part is an alternative proposing the html and the text versions of the e-mail.
  *alt = curl_mime_init(curl)
  
  ; HTML message.
  *part = curl_mime_addpart(*alt)
  curl_mime_data(*part, inline_html$, #CURL_ZERO_TERMINATED)
  curl_mime_type(*part, "text/html")
  
  ; Text message.
  *part = curl_mime_addpart(*alt)
  curl_mime_data(*part, inline_text$, #CURL_ZERO_TERMINATED)
  
  ; Create the inline part.
  *part = curl_mime_addpart(*mime)
  curl_mime_subparts(*part, *alt)
  curl_mime_type(*part, "multipart/alternative")
  *slist = curl_slist_append(#Null, "Content-Disposition: inline")
  curl_mime_headers(*part, *slist, 1)
  
  ; Add the current source program As an attachment.
;   *part = curl_mime_addpart(*mime)
;   curl_mime_filedata(*part, "EMailWithHTML.pb")
  
  curl_easy_setopt(curl, #CURLOPT_MIMEPOST, *mime)
  
  ; Send the message
  res = curl_easy_perform(curl)
  ; Check For errors
  If res <> #CURLE_OK
    Debug "curl_easy_perform() failed: " + curl_easy_strerror(res)
  EndIf
  
  ; Free multipart message.
  curl_mime_free(*mime)
  
  ; Free lists.
  curl_slist_free_all(*headers)
  curl_slist_free_all(*recipients)
  
  curl_easy_cleanup(curl)
EndIf
Of course you have to adust the server and user stuff.
Last edited by infratec on Sun Nov 13, 2022 4:15 pm, edited 1 time in total.
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: How to send an email as html?

Post by Johanson »

infratec wrote:I added the mime stuff in my libcurl.pbi

https://www.von-der-salierburg.de/downl ... urldll.zip

This code works:

Code: Select all

;
; LibCurl e-mail with html body
;

EnableExplicit

IncludeFile "libcurl.pbi"

#MailServer = "smtp://mail.example.com"

#FROM = "<sender@example.org>"
#To = "<addressee@example.net>"

#SMTP_User = ""
#SMTP_Password = ""

;#SKIP_PEER_VERIFICATION = #True
;#SKIP_HOSTNAME_VERIFICATION = #True

Define.i curl, i, res
Define.curl_slist *recipients, *headers, *slist
Define.curl_mime *mime, *alt
Define *part
Define inline_text$, inline_html$

Dim HeaderArray$(5)

HeaderArray$(0) = "Date: Wed, 10 Mar 2021 06:30:29 +0200"
HeaderArray$(1) = "To: "+ #TO
HeaderArray$(2) = "From: " + #FROM + " (Example User)"
HeaderArray$(3) = "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>"
HeaderArray$(4) = "Subject: example sending a MIME-formatted message"

inline_text$ = "This is the inline text message of the e-mail." + #CRLF$
inline_text$ + #CRLF$
inline_text$ + "It could be a lot of lines that would be displayed in an e-mail" + #CRLF$
inline_text$ + "viewer that is not able to handle HTML." + #CRLF$

inline_html$ = "<html>" + #CRLF$
inline_html$ + " <body>" + #CRLF$
inline_html$ + "  <p>This is the inline <b>HTML</b> message of the e-mail.</p>" + #CRLF$
inline_html$ + "  <br />" + #CRLF$
inline_html$ + "  <p>It could be a lot of HTML data that would be displayed by e-mail viewers able To handle HTML.</p>" + #CRLF$
inline_html$ + " </body>" + #CRLF$
inline_html$ + "</html>" + #CRLF$

curl = curl_easy_init()
If curl
  
  If #SMTP_User <> ""
    ; Set username And password
    curl_easy_setopt_str(curl, #CURLOPT_USERNAME, #SMTP_User)
    curl_easy_setopt_str(curl, #CURLOPT_PASSWORD, #SMTP_Password)
  EndIf
  
  
  If Left(#MailServer, 5) = "smtps"
    ; If you want To connect To a site who isn't using a certificate that is
    ; signed by one of the certs in the CA bundle you have, you can skip the
    ; verification of the server's certificate. This makes the connection
    ; A LOT LESS SECURE.
    ;
    ; If you have a CA cert For the server stored someplace Else than in the
    ; Default bundle, then the CURLOPT_CAPATH option might come handy For
    ; you.
    CompilerIf Defined(SKIP_PEER_VERIFICATION, #PB_Constant)
      CompilerIf #SKIP_PEER_VERIFICATION
        curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
      CompilerEndIf
    CompilerEndIf
    
    ; If the site you're connecting to uses a different host name that what
    ; they have mentioned in their server certificate's commonName (or
    ; subjectAltName) fields, libcurl will refuse To connect. You can skip
    ; this check, but this will make the connection less secure. */
    CompilerIf Defined(SKIP_HOSTNAME_VERIFICATION, #PB_Constant)
      CompilerIf #SKIP_HOSTNAME_VERIFICATION
        curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
      CompilerEndIf
    CompilerEndIf
  EndIf
  
  
  ; This is the URL for your mailserver
  curl_easy_setopt_str(curl, #CURLOPT_URL, #MailServer)
  
  
  curl_easy_setopt_str(curl, #CURLOPT_MAIL_FROM, #FROM)
  
  *recipients = curl_slist_append(*recipients, #To)
  curl_easy_setopt(curl, #CURLOPT_MAIL_RCPT, *recipients)
  
  ; Build and set the message header list.
  For i = 0 To ArraySize(HeaderArray$()) - 1
    *headers = curl_slist_append(*headers, HeaderArray$(i))
  Next i
  curl_easy_setopt(curl, #CURLOPT_HTTPHEADER, *headers)
  
  ; Build the mime message.
  *mime = curl_mime_init(curl)
  
  ; The inline part is an alternative proposing the html and the text versions of the e-mail.
  *alt = curl_mime_init(curl)
  
  ; HTML message.
  *part = curl_mime_addpart(*alt)
  curl_mime_data(*part, inline_html$, #CURL_ZERO_TERMINATED)
  curl_mime_type(*part, "text/html")
  
  ; Text message.
  *part = curl_mime_addpart(*alt)
  curl_mime_data(*part, inline_text$, #CURL_ZERO_TERMINATED)
  
  ; Create the inline part.
  *part = curl_mime_addpart(*mime)
  curl_mime_subparts(*part, *alt)
  curl_mime_type(*part, "multipart/alternative")
  *slist = curl_slist_append(#Null, "Content-Disposition: inline")
  curl_mime_headers(*part, *slist, 1)
  
  ; Add the current source program As an attachment.
;   *part = curl_mime_addpart(*mime)
;   curl_mime_filedata(*part, "EMailWithHTML.pb")
  
  curl_easy_setopt(curl, #CURLOPT_MIMEPOST, *mime)
  
  ; Send the message
  res = curl_easy_perform(curl)
  ; Check For errors
  If res <> #CURLE_OK
    Debug "curl_easy_perform() failed: " + curl_easy_strerror(res)
  EndIf
  
  ; Free multipart message.
  curl_mime_free(*mime)
  
  ; Free lists.
  curl_slist_free_all(*headers)
  curl_slist_free_all(*recipients)
  
  curl_easy_cleanup(curl)
EndIf
Of course you have to adust the server and user stuff.
Define.curl_mime *mime, *alt
==> 'Structure not found: curl_mime'
Post Reply