[SOLVED] How do I use TSNet Indy to send an email with attachment (pdf)?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

[SOLVED] How do I use TSNet Indy to send an email with attachment (pdf)?

Post by karmacomposer » Tue Jul 24, 2018 4:01 pm

I am trying to email a pdf form and it fails. I am using code for the TSNet external, but not sure how to use it (do you INCLUDE it in your stack)?

I did find the External in my RunRev Ext folder under tsNet_Indy_1.3.4

Here is the code:

Code: Select all

local tUrl, tEmailMessage, tSender, tRecipient, tSettings, tFilePath, tFileName, tFileData, tResult, tBytes, tResponseHeaders
      
      put "smtp://mail.mfelkerco.com:587/" into tUrl
      put "Attached is a new PDA Form from Aflac" into tEmailMessage
      
      put tPDFPath into tFilePath
      set the itemDel to slash
      put the last item of tFilePath into tFileName
      put URL("binfile:" & tFilePath) into tFileData
      
      put "xxxxxxx" into tSettings["username"]
      put "xxxxxxxxxxx" into tSettings["password"]
      put "email address" into tSender
      put "email address" & cr into tRecipient
      put "email address" after tRecipient
      
      -- Enable TLS for SMTP
      put true into tSettings["use_ssl"]
      
      -- create the smtp message
      put createSmtpMessage(tTo, tCc, tSender, tSubject, tMessage, tMessageHTML, tFileName, tFileData) into tPostData
      
      -- send email
      put tsNetSmtpSync(tURL, tSender, tRecipients, tPostData, tResponseHeaders,tBytes,tSettings) into tResult
      
      
      if the first word of tResult is "tsneterr:" then
         answer "Error" && tResult && "returned from server"
      else
         answer "E-mail sent"
      end if
I changed the username, password and email addresses to be anonymous to protect the innocent. LOL

And the function:

Code: Select all

Function createSmtpMessage pTo, pCc, pFrom, pSubject, pMessage, pMessageHTML, tFileName, tFileData

put "Date:" && the internet date & cr into tPostData
put "From:" && pFrom & cr after tPostData
put "To: " after tPostData
repeat for each line tRecipient in pTo
   put tRecipient & ", " after tPostData
end repeat
delete char -1 of tPostData
put cr after tPostData

put "Cc: " after tPostData
repeat for each line tRecipient in pCC
   put tRecipient & ", " after tPostData
end repeat
delete char -1 of tPostData
put cr after tPostData

put "Boundary." & the seconds into tBoundary
-- Create a multi-part MIME message
put "MIME-Version: 1.0" & cr after tPostData
put "Content-Type: multipart/mixed;" & cr after tPostData
put " boundary=" & quote & tBoundary & quote & cr after tPostData
--put "Subject: "&"=?UTF-8?B?"& base64Encode (textEncode(pSubject,"UTF-8"))&"?=" & cr & cr after tPostData
--The above was the original line.
--The base64encode function wraps the encoded data at 72 characters.
--So if the string, which is returned by that function, has more than 72 characters, then you have to remove the CR(s) from the returned string.
--Otherwise the CR breaks the smtp header and the subject is not displayed correctly in the email or more worse could happen.

-- So better use something like this:
put base64Encode (textEncode(pSubject,"UTF-8")) into pSubject

-- replace CR right away 
-- Of course we  could first check if the number of chars is >72, but why? If it´s there it will be replaced. If not, nothing happens.
replace cr with "" in pSubject

put "Subject: "&"=?UTF-8?B?"& pSubject &"?=" & cr & cr after tPostData

put "This is a multi-part message in MIME format." & cr after tPostData
-- Add the first "part" of the message
put "--" & tBoundary & cr after tPostData

-- Inside this part, we want both the HTML and plain text alternatives
put "Content-Type: multipart/alternative;" & cr after tPostData
put " boundary=" & quote & tBoundary & "_mesg" & quote & cr & cr after tPostData

-- The plain text version
put "--" & tBoundary & "_mesg" & cr after tPostData
put "Content-Type: text/plain; charset=utf-8" & cr after tPostData
put "Content-Transfer-Encoding: quoted-printable" & cr & cr after tPostData
put textEncode(pMessage, "UTF-8") & cr & cr after tPostData

-- The HTML version
put "--" & tBoundary & "_mesg" & cr after tPostData
put "Content-Type: text/html; charset=utf-8" & cr after tPostData
put "Content-Transfer-Encoding: quoted-printable" & cr & cr after tPostData
put textEncode(pMessageHTML, "UTF-8") & cr & cr after tPostData

put "--" & tBoundary & "_mesg--" & cr & cr after tPostData

if tFileName is not empty then
   put "--" & tBoundary & cr after tPostData
   put "Content-Type: application/pdf;" & cr after tPostData
   put " name="& quote & tFileName & quote & cr after tPostData
   put "Content-Transfer-Encoding: base64" & cr after tPostData
   put "Content-Disposition: attachment;" & cr after tPostData
   put " filename="& quote & tFileName & quote & cr & cr after tPostData
   
   put base64encode(tFileData) & cr & cr after tPostData
end if
-- Add the closing "part" tag
put "--" & tBoundary & "--" & cr after tPostData

return tPostData
end createSmtpMessage
I found this code doing a search and, according to what I found, TSNet seems to be included with a commercial license - I upgraded to a commercial license, so not sure what else I need to do to make this work. I don't even know if this is current or accurate.

I found it here:

Code: Select all

https://forums.livecode.com/viewtopic.php?t=28503
I guess it would be helpful to include the error I get:

Code: Select all

Error tsneterr: (55) RCPT failed: 501
returned from server
Any help is appreciated.

If there is another, simpler way to send an email with a pdf attached in LiveCode NOT using Thunderbird or any 3rd party software, i'm all ears.

Thanks.

Mike
Last edited by karmacomposer on Thu Jul 26, 2018 8:03 pm, edited 4 times in total.

karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

Re: Does TSNet come with LiveCode Indie Commercial?

Post by karmacomposer » Tue Jul 24, 2018 4:21 pm

I looked in my RunRev folder and under the folder Ext there is tsNet_Indy_1.3.4, so it is there. How do I use it?

Mike

charlesBUSd8qF
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 39
Joined: Wed Apr 11, 2012 10:28 pm

Re: How do I use TSNet Indy to send an email with attachment (pdf)?

Post by charlesBUSd8qF » Wed Jul 25, 2018 7:00 am

Hi Mike,

If you are using the commercial edition of LC, tsNet will automatically be available in the IDE. When building standalones, make sure that you select tsNet in the list of inclusions.

There are two things I can see from your code....

You are storing the e-mail addresses you want the e-mail sent to into a variable called tRecipient, however the call to the createSmtpMessage function is expecting these addresses to be passed in two variables: tTo and tCc. This means the body of the e-mail message won't be created with the appropriate To: and Cc: e-mail headers.

When the call to tsNetSmtpSync is made, it is passed a variable called tRecipients (note the s at the end) that hasn't been defined in your code. This is why you are receiving an error code back from tsNet indicating that there was a failure when setting the RCPT for the e-mail (it would be an empty recipient).

In the stack where I am guessing you got a lot of that code from (https://downloads.techstrategies.com.au ... l.livecode), you can see the following lines:

Code: Select all

   put field "smtp_to" into tTo
   put field "smtp_cc" into tCc
   put field "smtp_to" & cr & field "smtp_cc" & cr & field "smtp_bcc" into tRecipients
This puts the e-mail addresses that you want the e-mail sent to into tTo and tCc (for the createSmtpMessage function) as well as into tRecipients (for the tsNetSmtpSync function).

Hope that helps!

Cheers,

Charles

karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

Re: How do I use TSNet Indy to send an email with attachment (pdf)?

Post by karmacomposer » Thu Jul 26, 2018 4:00 pm

I my case, I am hard coding the email addresses, so no fields.

Therefore, I assume I can use the code...

Code: Select all

put "email.email.com" into ...
Correct?

Mike

karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

Re: How do I use TSNet Indy to send an email with attachment (pdf)?

Post by karmacomposer » Thu Jul 26, 2018 8:03 pm

Thank you. That was it. Now it works perfectly.

Mike

crokyweb
Posts: 14
Joined: Fri Apr 15, 2022 1:55 pm

Re: [SOLVED] How do I use TSNet Indy to send an email with attachment (pdf)?

Post by crokyweb » Fri Apr 15, 2022 2:20 pm

with your method of sending pdf attachments I encountered a problem in sending emails with pdf attachments to gmail addresses.
to other addresses it works but not to address gmail
Suggestions?

Post Reply

Return to “Talking LiveCode”