Server : send email with PDF attachment

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Server : send email with PDF attachment

Post by bangkok » Sat Dec 17, 2011 12:28 am

Following the lesson :

http://lessons.runrev.com/s/lessons/m/4 ... er-Scripts

... i've tried the script.

No luck.

As soon as I try to add a PDF file as attachment, it doesn't work.

The mail is sent, but the attachment is "corrupted".

the header says : "X-Amavis-Alert: BAD HEADER SECTION, MIME error: error: part did not end with expected boundary"

Do you have any idea ?
<?rev

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 wrapQ pText
return quote & pText & quote
end wrapQ


-- 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 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 & "Subject:" && pSub & return into tMsg
if pCc is not empty then
put "Cc:" && pCc & return after tMsg
end if

-- 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
if there is a file pAtts["path"] then
put "Content-Type:" && pAtts["type"]& "; name=" & wrapQ(pAtts["name"]) & ";" & return & "Content-Transfer-Encoding: base64;" & return & return & base64Encode(URL ("binfile:" & pAtts["path"])) & return & "--" & tBoundary & return after tMsg
end if
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
get shell("echo" && wrapQ(shellEscape(tMsg)) && "| /usr/sbin/sendmail" && wrapQ(shellEscape(pTo)) && "-f" && wrapQ(shellEscape(pFrom)))
end mail

put "myname@xxxxxxx.com" into pTo
put "this is a test" into pSub
put "aaaaaaaaaaaaaaaaa" into pMsg
put "alert@xxxx.com" into pFrom
put empty into pCc
put empty into pBcc
put false into pHtml
put "TOPRINT" into pAtts["name"]
put "TOPRINT.pdf" into pAtts["path"]
put "application/pdf" into pAtts["type"]

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

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Server : send email with PDF attachment

Post by bangkok » Thu Dec 29, 2011 11:19 am

Sorry guys.

Anyone could give me a clue on this issue ?

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Server : send email with PDF attachment

Post by bangkok » Thu Dec 29, 2011 9:46 pm

Solved !

.... after 2 hours of painfull trials and errors.

There is one typo/mistake in the script.


Instead of :

Code: Select all

"Content-Transfer-Encoding: Base64;" & return & return & 
it should be :

Code: Select all

"Content-Transfer-Encoding: Base64" & return & return
[without the ";" after Base64]

bmatichuk
Posts: 15
Joined: Tue Oct 12, 2010 7:19 am

Re: Server : send email with PDF attachment

Post by bmatichuk » Wed Aug 08, 2012 12:01 pm

Shell doesn't work on the iPhone

You can use mandrill to send email using posts.

TorstenHolmer
Posts: 57
Joined: Mon Oct 28, 2013 1:23 pm

Re: Server : send email with PDF attachment

Post by TorstenHolmer » Fri Mar 19, 2021 3:38 pm

bangkok wrote:
Thu Dec 29, 2011 9:46 pm
Solved !
[without the ";" after Base64]
Thank your very much, this post saved me!!!
I sent a note to the lesson makers to correct the code.

Cheers,
Torsten

Post Reply

Return to “CGIs and the Server”