Send Attachment with email

Just starting out? Need help? Post your questions and find answers here.
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Send Attachment with email

Post by cecilcheah »

Hi

I have found an example in the trick and treat section on how to send email, but it does not send the attachment as attached to email. This is the email i got back:
Test...

--PBSendMailv1.0_Boundry_16062003001205
Content-Type: image/jpeg; name="cheque2.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: Attachment; filename="cheque2.jpg"



--PBSendMailv1.0_Boundry_16062003001205--
Anyone has a good example on how to send attachment? If anyone can help, i can release my latest Capture Image programme very soon with lots of features and options. This is the last hurdle.

Please help

Cecil
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Look like there is some info here : viewtopic.php?t=6536&highlight=email+attachments
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Post by cecilcheah »

This is the code i used, but it does not help. No attachment sent at all.

Anyone has done this sending attachment before?

Cecil
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Hmm looks like something failed somewhere, maybe the encoding or reading of the file, the code i posted doesn't have enough checks in it, and should only really be used as a guide :)
--Kale

Image
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Post by cecilcheah »

After much investigation, i find a few problems:

1. The file is not really attached, but embedded inside the email.
2. If it is base 64 encoded, nothing is really being read.

Your code:

Code: Select all

If AllocateMemory(0, InputBufferLength, 0) 
                                                    OutputBufferLength.l = InputBufferLength + InputBufferLength/3 + 2 
                                                    If OutputBufferLength < 64 : OutputBufferLength = 64 : EndIf
                                                    Debug "OutputBufferLength : " + Str(OutputBufferLength)
                                                    If AllocateMemory(1, OutputBufferLength, 0) 
                                                        ReadData(UseMemory(0), InputBufferLength) 
                                                        Debug "InputBufferLength: " + Str(InputBufferLength)
                                                        Base64Encoder(UseMemory(0), InputBufferLength, UseMemory(1), OutputBufferLength) 
                                                        Debug "Data: " + PeekS(UseMemory(1), OutputBufferLength)
                                                        SendMailData(PeekS(UseMemory(1), OutputBufferLength) + Chr(13) + Chr(10)) 
                                                        Debug GetFilePart(Attachments()) + " (base64) Encoded" 
                                                    Else 
                                                        Debug "ERROR: Unable to allocate memory for Bank 1 to process " + GetFilePart(Attachments()) 
                                                        ProcedureReturn 0 
                                                    EndIf 
                                                Else 
                                                    Debug "ERROR: Unable to allocate memory for Bank 0 to process " + GetFilePart(Attachments()) 
                                                    ProcedureReturn 0 
                                                EndIf 
Can anyone tell me how can i send an attachment as an attachment, not embedded inside an email, and how to read the data into the OutputBuffer correctly and send it?

Thanks

Cecil
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

>1. The file is not really attached, but embedded inside the email.

Yes thats correct, all email, wether it has attachments or not is sent as plain ascii text. All binary files that need to be attached are encoded using base64 which reads the bytes of the binary file and converts them to ordinary ascii.

You can read about how it works here:
http://www.freesoft.org/CIE/RFC/1521/

The encoded file are mearly included in the email and seperated by boundries so the recieving email program (providing its MIME compatible) can parse out the files and decode them from base64 back to binary. All boundries contain information on the file name and MIME type.

>2. If it is base 64 encoded, nothing is really being read.

it seems that way, you should maybe add more checks in the code to make sure any attachments are being encoded and sent as data through the open connection.

>and how to read the data into the OutputBuffer correctly and send it?

Its all there in the source (see the PB help files on base64 encoding and the RFC above), im afraid your just gonne have to take a good read and try to understand it :)
--Kale

Image
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Actually IIRC PB uses a small freeware command line email utility to send email with attachments, yeah!, you can find it here:

Discussed here:
viewtopic.php?t=6123

Email utility (blat.exe):
http://www.interlog.com/~tcharron/blat.html

Theres also a DLL version of Blat:
http://www.geocities.com/toby_korn/blat/
--Kale

Image
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Post by cecilcheah »

Hi

Can you tell me where you get the source from, which you use to translate to PB code.

Thanks

Cecil
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I didnt translate anything i just coded the example from the RFC, i think you best bet is to look into using the Blat.dll.
--Kale

Image
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Try this:

http://www.garyw.uklinux.net/PB/Email%20Example.zip

I've coded a little example for you using the blat.dll :) infact it works so well i think i prefer this to my last pure PB version (all the hard work has already been done!) :twisted:
--Kale

Image
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Excellent Kale, thanks very much!!!!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply