.jpg attachments

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

.jpg attachments

Post by trags3 » Wed Jul 23, 2014 7:47 pm

I have a nearly finished app that takes a screenshot of a card, saves on the mobile device as a .jpg file & uploads it to a server through a secure connection.
The server (Livecode server) accepts the email address information and sends an email to 2 or 3 people with the screen shot as an attachment.
I have no trouble opening the attachment with gmail, the server email that I have on the same server that sends the email, my cox.net email.
Yahoo on the other hand is not able to view or open the .jpg file. (Both ymail.com and yahoo.com)
I am afraid that there will be other servers other than Yahoo that will likely have problems.
The email is pretty much the same as shown in the livecode server tutorials below.
Any suggestions?

Tom
-- mail
--
-- Emails the given message to the recipients specified.
-- Each address passed can have a name attached in the form "name <address>".
-- Addresses can be passed as comma separated lists.
-- Attachements can be added by passing an array (interger indexed or otherwise).
-- with each attachment itself being an array.
--
-- pTo - The addresses to send the message to
-- pSub - The message subject
-- pMsg - The message body
-- pFrom - The address of the message sender
-- pCc - Any cc addresses
-- pBcc - Any Bcc addresses
-- pHtml - Boolean, if the message is to be sent as html
-- pAtts - Array of all attachments to send, each attachment of the form:
-- * name: the name of the attachment
-- * path: the absolute path to the attachment
-- * type: the mime type of the attachment, defaults to
-- application/octet-stream


command mail pReturnPath, pTo, pSub, pMsg, pFrom, pCc, pBcc, pHtml, pAtts
local tMsg

-- build the message header, adding the from, to and subject details
-- we also put any cc addresses in here, but not bcc (bcc addresses hidden)
put "From:" && pFrom & return & "To:" && pTo & return after tMsg
if pCc is not empty then
put "Cc:" && pCc & return after tMsg
end if
if pBcc is not empty then
put "Bcc:" && pBcc & return after tMsg
end if
put "Subject:" && pSub & return after tMsg

-- if there are any attachments, we must send this email as multipart
-- with the message body and each attachment forming a part
-- we do this by specifying the message as multipart and generating a unique boundary
if pAtts is an array then
local tBoundary
put "boundary" & the seconds into tBoundary
put "MIME-Version: 1.0" & return & "Content-Type: multipart/mixed; boundary=" & \
wrapQ(tBoundary) & return & "--" & tBoundary & return after tMsg
end if

-- add the actual message body, setting the content type appropriatly
if pHtml is true then
put "Content-Type: text/html;" & return & return after tMsg
else
put "Content-Type: text/plain;" & return & return after tMsg
end if
put pMsg & return after tMsg

-- add each attachment as a new part of the message, sepearting using
-- the generated boundary
if pAtts is an array then
put "--" & tBoundary & return after tMsg
repeat for each element tAtt in pAtts
if (tAtt["binary_data"] <> empty) or (there is a file tAtt["path"]) then
if tAtt["type"] is empty then
get "application/octet-stream"
else
get tAtt["type"]
end if

if tAtt["binary_data"] = empty then
put URL ("binfile:" & tAtt["path"]) into tAtt["binary_data"]
end if
put "Content-Type:" && it & "; name=" & wrapQ(tAtt["name"]) & ";" & \
return & "Content-Transfer-Encoding: base64;" & return & return & \
base64Encode(tAtt["binary_data"]) & return & "--" & \
tBoundary & return after tMsg
end if
end repeat
end if

-- send the mail by piping the message we have just built to the sendmail command
-- we must also send a copy of the message to the bcc addresses
put "echo" && wrapQ(shellEscape(tMsg)) && "| /usr/sbin/sendmail -f" && quote & "<" & shellEscape(pReturnPath) & ">" & quote && "-oi -t" into tCmd
# put tCmd
put shell(tCmd)

end mail

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: .jpg attachments

Post by Simon » Thu Jul 24, 2014 1:45 am

I take it the email came with an attachment that couldn't be opened?
Did you catch the name of the attached file? I've seen the extension (.jpg in this case) getting screwed up before.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: .jpg attachments

Post by trags3 » Thu Jul 24, 2014 5:12 am

Thanks Simon, The problem was that the email was sent to multiple addresses with an attachment. All of the addresses could receive the email.(even the Yahoo accounts) but the Yahoo addresses could not open the attachment while all the others could. It turns out that Ymail.com and Yahoo.com can not handle encrypted attachments, and that is the problem encrypted attachments.
Glad to find out that the problem was not with LiceCode or something I did. I am pretty sure I can fix it. Something to be aware of. :D
Tom

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: .jpg attachments

Post by bn » Thu Jul 24, 2014 7:54 am

Hi Tom,
Glad to find out that the problem was not with LiceCode
don't let the good people from Edinburgh see what you call their programing language :) :)

Kind regards
Bernd

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: .jpg attachments

Post by trags3 » Thu Jul 24, 2014 1:13 pm

bn wrote:Hi Tom,
Glad to find out that the problem was not with LiceCode
don't let the good people from Edinburgh see what you call their programing language :) :)

Kind regards
Bernd
Oops, Total typo :oops: LiveCode is GREAT!!
Tom

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: .jpg attachments

Post by trags3 » Thu Jul 24, 2014 7:35 pm

It turns out that the attachment I was sending was not encrypted. Here is the advice I got from the HostM Client support team explaining what the problem was and how to fix it.
They actually fixed my server code.

... rather than piping in the message from a file, the mail handler was trying to pipe it in via an echo in the shell (which is not a good way to do it and can cause issues),
Just thought someone might like to know.

Tom

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: .jpg attachments

Post by trags3 » Sat Jul 26, 2014 8:44 pm

trags3 wrote:
... rather than piping in the message from a file, the mail handler was trying to pipe it in via an echo in the shell (which is not a good way to do it and can cause issues),
Just thought someone might like to know.

Tom
Just a quick update as I am sure others are bound to run into the problem I had. The evidence that there was a problem was some emails could receive and open the .jpg attachment that I sent with LiveCode server script while others could not open the attachment at all. Yahoo addresses could not handle it at all. The server script from the Live Code Lessons can be found here:
http://lessons.runrev.com/m/4070/l/8184 ... er-scripts
the replacement script provided by HostM client services is as follows:

-- escape shell characters: use this function before passing data to the shell
function shellEscape pText
repeat for each char tChar in "\`!$" & quote
replace tChar with ("\" & tChar) in pText
end repeat
return pText
end shellEscape

-- wrap quotes around text
function q pText
return quote & pText & quote
end q


command mail pReturnPath, pTo, pSub, pMsg, pFrom, pCc, pBcc, pHtml, pAtts

local tMsg

put "From:" && pFrom & cr & "To:" && pTo & cr after tMsg
if pCc is not empty then
put "Cc:" && pCc & cr after tMsg
end if
if pBcc is not empty then
put "Bcc:" && pBcc & cr after tMsg
end if
put "Subject:" && pSub & cr after tMsg

put "MIME-Version: 1.0" & cr after tMsg
if pAtts is an array then
local tBoundary
put "======_" & the milliseconds into tBoundary
put "Content-Type: multipart/mixed; boundary=" & q(tBoundary) & cr after tMsg
else
if pHtml is true then
put "Content-Type: text/html; charset=" & q("utf-8") & cr after tMsg
else
put "Content-Type: text/plain; charset=" & q("utf-8") & cr after tMsg
end if
end if
put cr after tMsg

if pAtts is an array then
put "--" & tBoundary & cr after tMsg
put "MIME-Version: 1.0" & cr after tMsg
if pHtml is true then
put "Content-Type: text/html; charset=" & q("utf-8") & cr & cr after tMsg
else
put "Content-Type: text/plain; charset=" & q("utf-8") & cr & cr after tMsg
end if
end if
put pMsg & cr & cr after tMsg

if pAtts is an array then
repeat for each element tAtt in pAtts
if tAtt["type"] is empty then
put "application/octet-stream" into tAtt["type"]
end if
if (tAtt["base64_data"] = empty) then
if (tAtt["binary_data"] <> empty) then
put base64Encode(tAtt["binary_data"]) into tAtt["base64_data"]
delete variable tAtt["binary_data"]
else if (tAtt["path"] <> empty) then
put base64Encode(url ("binfile:" & tAtt["path"])) into tAtt["base64_data"]
end if
end if
put "--" & tBoundary & cr after tMsg
put "MIME-Version: 1.0" & cr after tMsg
put "Content-Type:" && tAtt["type"] & "; name=" & q(tAtt["name"]) & cr after tMsg
put "Content-Transfer-Encoding: base64" & cr after tMsg
put "Content-Disposition: attachment; filename=" & q(tAtt["name"]) & cr & cr after tMsg
put tAtt["base64_data"] & cr & cr after tMsg
end repeat
put "--" & tBoundary & "--" & cr after tMsg
end if

put the tempName into tFile
put tMsg into url ("binfile:" & tFile)
put "/usr/sbin/sendmail -f" && quote & "<" & shellEscape(pReturnPath) & ">" & quote && "-oi -t <" && q(tFile) into tCmd
put shell(tCmd)
delete file tFile
end mail

Hope this helps someone. This seems to work or all the email addresses I have tried. Cox,gmail, Yahoo and a couple of others.

Tom

Post Reply