1 //=============================================================================
3 // Contents: Definitions for MessageWithAttachments
4 // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
5 // WWW: http://www.fwb.gulf.net/~dwsauder/mimepp.html
7 // Copyright (c) 1996, 1997 Douglas W. Sauder
8 // All rights reserved.
10 // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
11 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12 // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
13 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
16 // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 // PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
18 // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
19 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
21 //=============================================================================
24 #include <sys/types.h>
26 #include <mimelib/string.h>
27 #include <mimelib/utility.h>
31 MessageWithAttachments::MessageWithAttachments()
36 MessageWithAttachments::~MessageWithAttachments()
41 void MessageWithAttachments::SetText(const DwString
& aStr
)
43 // Create a body part and set the necessary fields
45 MultipartBodyPart part
;
46 part
.SetType(DwMime::kTypeText
);
47 part
.SetSubtype(DwMime::kSubtypePlain
);
48 part
.SetCte(DwMime::kCte7bit
);
50 // Set the string as the body of the body part
54 // Set this body part as the first one
60 int MessageWithAttachments::NumberOfAttachments() const
62 int n
= NumberOfParts() - 1;
63 return (n
>= 0) ? n
: 0;
67 void MessageWithAttachments::Attach7bitFile(const char* aFilename
,
68 int aType
, int aSubtype
)
70 // Get the file contents
73 PutFileInString(aFilename
, str
);
75 // Create a body part and set the necessary fields
77 MultipartBodyPart part
;
79 part
.SetSubtype(aSubtype
);
80 part
.SetCte(DwMime::kCte7bit
);
82 // Set content-disposition to attachment, with filename parameter
83 // (see RFC-1806 for information on this *experimental* header field)
85 DwString contDisp
= "attachment; filename=";
87 contDisp
+= aFilename
;
89 part
.SetContentDisposition(contDisp
);
91 // Set the file contents as the body of the body part
95 // Make sure this is not the first part, since that is reserved for
98 if (NumberOfParts() == 0) {
107 void MessageWithAttachments::Attach8bitFile(const char* aFilename
,
108 int aType
, int aSubtype
)
110 // Get the file contents
113 PutFileInString(aFilename
, str
);
115 // Encode using quoted-printable encoding
118 DwEncodeQuotedPrintable(str
, encStr
);
120 // Create a body part and set the necessary fields
122 MultipartBodyPart part
;
124 part
.SetSubtype(aSubtype
);
125 part
.SetCte(DwMime::kCteQuotedPrintable
);
127 // Set content-disposition to attachment, with filename parameter
128 // (see RFC-1806 for information on this *experimental* header field)
130 DwString contDisp
= "attachment; filename=";
132 contDisp
+= aFilename
;
134 part
.SetContentDisposition(contDisp
);
136 // Set the encoded file contents as the body of the body part
138 part
.SetBody(encStr
);
140 // Make sure this is not the first part, since that is reserved for
143 if (NumberOfParts() == 0) {
144 SetBodyPart(1, part
);
152 void MessageWithAttachments::AttachBinaryFile(const char* aFilename
,
153 int aType
, int aSubtype
)
155 // Get the file contents
158 PutFileInString(aFilename
, str
);
160 // Encode using base64 encoding
163 DwEncodeBase64(str
, encStr
);
165 // Create a body part and set the necessary fields
167 MultipartBodyPart part
;
169 part
.SetSubtype(aSubtype
);
170 part
.SetCte(DwMime::kCteBase64
);
172 // Set content-disposition to attachment, with filename parameter
173 // (see RFC-1806 for information on this *experimental* header field)
175 DwString contDisp
= "attachment; filename=";
177 contDisp
+= aFilename
;
179 part
.SetContentDisposition(contDisp
);
181 // Set the encoded file contents as the body of the body part
183 part
.SetBody(encStr
);
185 // Make sure this is not the first part, since that is reserved for
188 if (NumberOfParts() == 0) {
189 SetBodyPart(1, part
);
197 int MessageWithAttachments::PutFileInString(const char* aFilename
,
202 int k
= stat(aFilename
, &statBuf
);
207 int fileSize
= (int) statBuf
.st_size
;
211 int bufSize
= fileSize
+ 8; // a little elbow room added
212 char* buf
= new char[bufSize
];
214 // Read the file into the buffer
216 FILE* fp
= fopen(aFilename
, "rb");
225 if (feof(fp
) || len
== fileSize
) {
233 // Place the buffer in the string
235 str
.TakeBuffer(buf
, bufSize
, 0, len
);