PureSMTP library : mail & attachments (AUTH supported)

All PureFORM, JaPBe, Libs and useful code maintained by gnozal

Moderator: gnozal

Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

@gnozal

Hi, your library is fantastic, but unfortunately I have hit a hard stone with it. :cry:

I'm trying to compose a multi-part html email with in-line images and I can not do so unless you allow Pure_SMTP to remove the default content type and transfer encoding.

When I send an email with your library it sends by default the headers for content type and transfer encoding:

Code: Select all

From - Wed Aug 04 02:11:46 2010
X-Account-Key: account4
X-UIDL: UID45-1279760133
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
Return-Path: <christian.uceda@test.lcl>
X-Original-To: test@test.lcl
Delivered-To: test@test.lcl
Received: from Apollo (brutus.test.lcl [192.168.249.80])
	by barask10.test.lcl (Postfix) with SMTP id 298B928451;
	Wed,  4 Aug 2010 02:11:47 +0200 (CEST)
From: Christian Uceda <christian.uceda@test.lcl>
To: <test@test.lcl>
Subject: Test Email X
X-Mailer: PureSMTP
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1" <------------ (Need to remove)
Content-Transfer-Encoding: 7bit <------------ (Need to remove)
Message-Id: <20100804001148.298B928451@barask10.test.lcl>
Date: Wed,  4 Aug 2010 02:11:47 +0200 (CEST)
I tried to do: PureSMTP_SetContentType("") but logically it defaults to 'Content-Type: text/plain; charset="iso-8859-1"', and as far as I have been able to see the 'Content-Transfer-Encoding: 7bit' can not be disabled either and it sits there by default.

In order to send a multi-part email with html and in-line images, the header structure must be like the one below, with the content type at the very end of the header list, and with no encoding which I specifiy for each section of the email individually.

Code: Select all

From - Wed Aug 04 02:11:46 2010
X-Account-Key: account4
X-UIDL: UID45-1279760133
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
Return-Path: <christian.uceda@test.lcl>
X-Original-To: test@test.lcl
Delivered-To: test@test.lcl
Received: from Apollo (brutus.test.lcl [192.168.249.80])
	by barask10.test.lcl (Postfix) with SMTP id 298B928451;
	Wed,  4 Aug 2010 02:11:47 +0200 (CEST)
From: Christian Uceda <christian.uceda@test.lcl>
To: <test@test.lcl>
Subject: Test Email X
X-Mailer: PureSMTP
MIME-Version: 1.0
Message-Id: <20100804001148.298B928451@barask10.test.lcl>
Date: Wed,  4 Aug 2010 02:11:47 +0200 (CEST)
Content-Type: multipart/alternative; <------------ (Good)
 boundary="------------040006060302020507070601" <------------ (Good)
Setting the PureSMTP_SetContentType to "Content-Type: multipart/alternative;" does not fix the issue, as long as those two headers remain there, with them being sent by default I can not make the email display at all on the email client. (outlook/thunderbird)

This is a big issue for me, could you please add some switch somewhere so the default content type and transfer encodings can be disabled if one needs to? :?:
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

Christian Uceda wrote:Setting the PureSMTP_SetContentType to "Content-Type: multipart/alternative;" does not fix the issue, as long as those two headers remain there, with them being sent by default I can not make the email display at all on the email client. (outlook/thunderbird)
This is a big issue for me, could you please add some switch somewhere so the default content type and transfer encodings can be disabled if one needs to? :?:
The library was not designed to do this.
Did you try sending the images as attachments ?

Currently, it works like this :
1. Without attachments :

Code: Select all

                "Content-Type: " + PureSMTP_ContentType + #CRLF
                "Content-Transfer-Encoding: 7bit" + #CRLF
2. With attachments :

Code: Select all

               "Content-Type: multipart/mixed; boundary=" + #g + Boundary + #g + #CRLF
                #CRLF
Message :

Code: Select all

                "--" + Boundary + #CRLF
                "Content-Type: " + PureSMTP_ContentType + #CRLF
                "Content-Transfer-Encoding: 7bit" + #CRLF
For each attachment :

Code: Select all

                "--" + Boundary + #CRLF
                "Content-Type: " + GetMIMEType(GetExtensionPart(Attachment)) + "; name=" + #g + GetFilePart(Attachment) + #g + #CRLF)
If File Is Binary :

Code: Select all

                      "Content-Transfer-Encoding: base64" + #CRLF
                      "Content-Disposition: attachment; filename=" + #g + GetFilePart(Attachment) + #g + #CRLF
                      #CRLF
Not binary :

Code: Select all

                      "Content-Transfer-Encoding: 7bit" + #CRLF
                      "Content-Disposition: attachment; filename=" + #g + GetFilePart(Attachment) + #g + #CRLF
                      #CRLF
What should be optional here ?
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

Let me explain it better, yesterday I did not do a good job as I was really tired.

I'm sending HTML messages with in-line images, this is the images are attachments with a special header that allows the html code on the email to display the images as part of the email as if you are viewing a small website on an email message.

To achieve that I'm composing the entire body of the email plus boundaries and even doing the encoding of the images with the right headers myself.

I'm doing all of this work because pure_SMTP attaches the images as regular attachments. This is fine as I understand that sending multi-part html messages with in-line images was not part of the design of the library.

I'm using Pure_SMTP only to talk to the SMTP server, you did a fine job on that :), it would be a waste of effort for me to duplicate the job you did so well :)

An email in html with in-line images, needs the following body structure: (Note: I learn this structure from the way thunderbird composes messages)

Code: Select all

Content-Type: multipart/alternative;
 boundary="------------040006060302020507070601"

This is a multi-part message in MIME format.
--------------040006060302020507070601
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

  Hi this a message
  

--------------040006060302020507070601
Content-Type: multipart/related;
 boundary="------------040308020001050609000802"


--------------040308020001050609000802
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<div align='left'>
<p>Hi this is a message</p>
<a href='http://www.google.com'>
<img src='cid:image1.gif' alt='image1' style='border-style: none;' height='56' width='200'>
<img src='cid:image2.gif' alt='image2' style='border-style: none;' height='56' width='200'>
<br>
</a>
</div>
<u>Hola! á</u>
</body>
</html>

--------------040308020001050609000802
Content-Type: image/gif;
 name="image1.gif"
Content-Transfer-Encoding: base64
Content-ID: <image1.gif>
Content-Disposition: inline;
 filename="image1.gif"
 
 R0lGODlhyAA4APcAAFoCBCSCbInCtARFK6iEdCdqXMHj3IyuoAQ9KYRCNNjCtIhZXNPz7GCj
kpO9tGISCARbPLyNlKdwZNjO1HojFOzj3FyCdH8uNI9cVKLTycejnARmRKSSiCp4Y5BNTGQY
HJp0dOL19MTSzHEyLM+vtH4UBPzu+DSSfICzpNSypOzq3HcFBr+Eed3FxXclJM27uaLIxOrU
1/f19opORB9oU1KFfoRiXKh9ebGZl7rV1Dt6bMLq4nSmnGCVh8ilpRJeTAROPDRtXZdCOXE2
N38aCejb2MyZmaGDhI+0qw1oVN3t7NrMytK9xHSSjLhuYJxOUPz+/KZlYFQMCKbFvIskG8is
qJA9NkeEcnMdD3gMBeG8uGSbjP<the rest has been removed for clarity>

--------------040308020001050609000802
Content-Type: image/gif;
 name="image2.gif"
Content-Transfer-Encoding: base64
Content-ID: <image2.gif>
Content-Disposition: inline;
 filename="image2.gif"
 
R0lGODlhIAFoAdUkAP///wAAAO0gJGdkZKako/JnSTY1NGotI/eXefvJtO84K/7v5/BKM/SA
YfFZPv3WxE5LSh8fHvmvlf3i1VpYV4WCghIREXVycvaMbfijhvN0VUJAPysqKbm3t/q8pJWS
k8zKyuLg4GBgYO/v7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAACQALAAA
AAAgAWgBAAb/QJJwSCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8
Tq/b7/i8fs/v+/9HIyIBhIWGh4<the rest has been removed for clarity>
 
 --------------040308020001050609000802--

--------------040006060302020507070601--
The problem is that by default Pure_SMTP is always sending a content type header plus a content encoding header, no matter what.

In this particular type of email I need to disable those default content type and encoding headers because I need to send custom ones on the body of the
email.

The ones you sent by default plus the ones I try top send confuse the email client (tested on Outlook and Mozilla Thunderbird) and make the email display blank.

So I would either need a mechanism to disable the ones you send by default, or you to support the type of message I'm trying to send.

The last option is a no-no, I understand is unreasonable and a lot of work on your part. Besides I did it already, all I need is a way for Pure_SMTP to not send the "content-type" and "content-transfer-encoding" on demand.

Something like this:

On my program I do:

Code: Select all

PureSMTP_SetContentType("-disabled")
so Pure_SMTP does not process the headers

Code: Select all

if PureSMTP_ContentType <> "-disabled"
	"Content-Type: " + PureSMTP_ContentType + #CRLF
	"Content-Transfer-Encoding: 7bit" + #CRLF
endif
Did I explain it in a clear way? I apologize if I'm making this confusing for you, I'm not a native English speaker
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

Christian Uceda wrote:Let me explain it better, yesterday I did not do a good job as I was really tired
....
Did I explain it in a clear way?
Yes, I think I got it.
I modified the code in a test library like this :

Code: Select all

                ; #ContentTypeDisabled = "!disabled!"
                ;
                If PureSMTP_ContentType = #ContentTypeDisabled
                  SendAMessage("Content-Type: " + PureSMTP_ContentType + #CRLF)
                  SendAMessage("Content-Transfer-Encoding: 7bit" + #CRLF) 
                EndIf
                ;
                ;
So you have to call PureSMTP_SetContentType("!disabled!").
I have compiled the test library (ANSI version only) and it is available here : http://gnozal.ucoz.com/PureSMTP_Test.zip
Could you test it to see if it fits your needs ?
Christian Uceda wrote: I apologize if I'm making this confusing for you, I'm not a native English speaker
Neither am I, so no need to apologize :wink:
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

I replaced the file PureSMTP on C:\Program Files (x86)\PureBasic\PureLibraries\UserLibraries (is this right?) and tried to set the PureSMTP_SetContentType("!disabled!")

Tried to send an email using the newer lib but on the resulting email code I got:

Code: Select all

Content-Type: !disabled!
Content-Transfer-Encoding: 7bit
Just when I was about to reply to your post I noticed this on your previous reply

; #ContentTypeDisabled = "!disabled!"
;
If PureSMTP_ContentType = #ContentTypeDisabled
SendAMessage("Content-Type: " + PureSMTP_ContentType + #CRLF)
SendAMessage("Content-Transfer-Encoding: 7bit" + #CRLF)
EndIf
and it should be: If PureSMTP_ContentType <> #ContentTypeDisabled

could it be?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

Christian Uceda wrote:I replaced the file PureSMTP on C:\Program Files (x86)\PureBasic\PureLibraries\UserLibraries (is this right?)
Yes
Christian Uceda wrote: and it should be: If PureSMTP_ContentType <> #ContentTypeDisabled
Oops !
Please download again (same link) :oops:
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

Brilliant!

It works like a charm now!

Thanks gnozal, I really appreciate it.

If you want, once I clean up my html-inline code up a little I can send it to you so you can have a look, and who knows, you may even like it to extend PureSMTP :D

Again many thanks. :D :D :D :D :D :D
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

Christian Uceda wrote:Brilliant!
It works like a charm now!
Thanks.

I have updated the library with this new functionality.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

Hi again gnozal

I'm completing my program, I'm finally sending the dreaded multi-part email messages with in-line attachments.

All was good but unfortunately I found another issue with the library, this one I fought it for days because I was convinced that it was me who was doing something wrong.

I'm sorry to be a pain in the a** :cry: , but basically you're adding an empty line at the end of the header section, if that empty line is there no email client decodes the email properly. :cry:

Code: Select all

From - Sat Aug 14 03:04:36 2010
X-Account-Key: account4
X-UIDL: UID72-1279760133
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
Return-Path: <cuf@domain.lcl>
X-Original-To: test@domain.lcl
Delivered-To: test@domain.lcl
Received: from PureSMTP (brutus.domain.lcl [192.168.249.80])
	by barask10.domain.lcl (Postfix) with SMTP id BAAD528478;
	Sat, 14 Aug 2010 03:03:34 +0200 (CEST)
From: Christian Uceda <cuf@domain.lcl>
To: <test@domain.lcl>
Subject: Test Email 777
X-Mailer: PureSMTP
MIME-Version: 1.0
Date: Sat, 14 Aug 2010 03:03:32 +0200 (CEST)
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Message-Id: <20100814010334.BAAD528478@barask10.domain.lcl>
                                                <----------------------------------- Here is the empty line
1st line of email body

Code: Select all

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Message-Id: <20100814010334.BAAD528478@barask10.domain.lcl>
1st line of email body                    <----------------------------------- It should be like this
When the line is present there, the multi-part email looks like this:

Image

And when the line is removed editing the message file by hand and reimporting it into the email software (Thunderbird 3.1.2) it looks like it should:

Image
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

Christian Uceda wrote:... but basically you're adding an empty line at the end of the header section, if that empty line is there no email client decodes the email properly.
Afaik, separating the header from the body by a CRLF ('empty line') is required by the SMTP protocol.
And all the traces I have studied (including 'multipart/mixed' or 'multipart/related') seem to confirm this : there is always a CRLF between header and body.
Wikipedia tells the same thing :
... the content of the message, as opposed to its envelope. It consists of a message header and a message body separated by an empty line ...
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

Hmmm, you're absolutely right that blank line is mandatory;

I gave it another go and realized what's really happening is this: because I was sending the the content-type as part of the message body, when the library sends the empty line that has to separate the headers from the message body, the message breaks because the first content-type/boundary mark is not of the headers and I made it separate.

Code: Select all

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100802 Lightning/1.0b2 Thunderbird/3.1.2
MIME-Version: 1.0
To: "test@domain.lcl >> \"Test user (xxx)\"" <test@domain.lcl>
Subject: Inline test
                                                         <-- correct empty line here at the end of the header section
Content-Type: multipart/alternative;        <-- content type as part of msg body and not part of headers
 boundary="------------060006050803020803050202"
                                                          
This is a multi-part message in MIME format.
--------------060006050803020803050202
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
So the only logical conclusion is to do this the right way and to send the content type as part of the headers:

Code: Select all

  PureSMTP_SetContentType("!disabled!")
  str_email_contenttype = "multipart/alternative;" + Chr(10)
  str_email_contenttype + " boundary=" + Chr(32) + str_first-boundary + Chr(32)
  PureSMTP_AddHeader("Content-Type", str_email_contenttype)
And voila it works! :D :D :D :D :D

I knew it was my fault :oops:
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

Hi Gnozal

Sorry to pester you again, but PureSMTP doesn't work when I create a thread safe executable, it does compile and run but PureSMTP_OpenSMTPConnection() does nothing and always returns -2 :cry:

(When creating the executable I'm using: UserlibUnicodeThreadsafe)

I have tested this on two different computers, on both I used a network sniffer and it doesn't show any traffic at all when running the threadsafe executable.

When not building a threadsafe executable it works fine and I can even see the DNS resolution traffic, when on threadsafe executable even no DNS traffic can be seen.

Could you please have a look? :cry: :cry:
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

Christian Uceda wrote:Hi Gnozal
Sorry to pester you again, but PureSMTP doesn't work when I create a thread safe executable, it does compile and run but PureSMTP_OpenSMTPConnection() does nothing and always returns -2 :cry:
-2 is #PureSMTP_NoConnection.
Either InitNetwork() or OpenNetworkConnection() failed.

I just tested with our server (Microsoft ESMTP MAIL Service).
All 4 library versions (ANSI, THREAD, UNICODE, THREAD+UNICODE) succeeded with a basic PureSMTP_SendMail() test.
PureSMTP_OpenSMTPConnection() did not fail.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

I just tested with our server (Microsoft ESMTP MAIL Service).
All 4 library versions (ANSI, THREAD, UNICODE, THREAD+UNICODE) succeeded with a basic PureSMTP_SendMail() test.
PureSMTP_OpenSMTPConnection() did not fail.
I'm so shocked :shock: that I did two more tests in virtual machines and tested again on my two real computers,

I'm testing this time using the same example code you supply on the help file.

I tested it on VirtualBox:

Running: Windows XP + SP3 = It doesn't work on threaded, as soon as I take the threaded out it works
Running: Windows 2000 server + SP4 + Latest patches = It doesn't work on threaded, as soon as I take the threaded out it works

On physical machines:

Windows 7 32bits = It doesn't work on threaded, as soon as I take the threaded out it works
Windows 2008R2 64bits = It doesn't work on threaded, as soon as I take the threaded out it works

Gnozal I do not doubt you tried, so there is definitively something wrong somewhere, did you tried with the same version available here: "http://gnozal.ucoz.com/PureSMTP_450.zip" (7.01) and with PB version 4.50?

I'm starting to get preoccupied because thrust me this could be a big issue for me.... :(

Could anybody else help me and verify this on his machine?
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: PureSMTP library : mail & attachments (AUTH supported)

Post by Christian Uceda »

Hi again,

Tried on my brother's computer, Win7 x64 with PB 4.50 32bits (the same version in all the other tests)

And again the same results :(

I can not for the life of me find anything wrong other than the threaded options.

For verification I would really need a kind third party willing to test this too. :cry:

At least to find out whether I'm getting crazy or not. :lol:
Post Reply