1 //=============================================================================
3 // Contents: Definitions for MessageWithAttachments
4 // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
5 // WWW: http://www.fwb.gulf.net/~dwsauder/mimepp.html
9 // Copyright (c) 1996, 1997 Douglas W. Sauder
10 // All rights reserved.
12 // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
13 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
14 // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
15 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
18 // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 // PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
20 // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
21 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 //=============================================================================
26 #include <sys/types.h>
28 #include <mimelib/string.h>
29 #include <mimelib/utility.h>
33 MessageWithAttachments::MessageWithAttachments()
38 MessageWithAttachments::~MessageWithAttachments()
43 void MessageWithAttachments::SetText(const DwString
& aStr
)
45 // Create a body part and set the necessary fields
47 MultipartBodyPart part
;
48 part
.SetType(DwMime::kTypeText
);
49 part
.SetSubtype(DwMime::kSubtypePlain
);
50 part
.SetCte(DwMime::kCte7bit
);
52 // Set the string as the body of the body part
56 // Set this body part as the first one
62 int MessageWithAttachments::NumberOfAttachments() const
64 int n
= NumberOfParts() - 1;
65 return (n
>= 0) ? n
: 0;
69 void MessageWithAttachments::Attach7bitFile(const char* aFilename
,
70 int aType
, int aSubtype
)
72 // Get the file contents
75 PutFileInString(aFilename
, str
);
77 // Create a body part and set the necessary fields
79 MultipartBodyPart part
;
81 part
.SetSubtype(aSubtype
);
82 part
.SetCte(DwMime::kCte7bit
);
84 // Set content-disposition to attachment, with filename parameter
85 // (see RFC-1806 for information on this *experimental* header field)
87 DwString contDisp
= "attachment; filename=";
89 contDisp
+= aFilename
;
91 part
.SetContentDisposition(contDisp
);
93 // Set the file contents as the body of the body part
97 // Make sure this is not the first part, since that is reserved for
100 if (NumberOfParts() == 0) {
101 SetBodyPart(1, part
);
109 void MessageWithAttachments::Attach8bitFile(const char* aFilename
,
110 int aType
, int aSubtype
)
112 // Get the file contents
115 PutFileInString(aFilename
, str
);
117 // Encode using quoted-printable encoding
120 DwEncodeQuotedPrintable(str
, encStr
);
122 // Create a body part and set the necessary fields
124 MultipartBodyPart part
;
126 part
.SetSubtype(aSubtype
);
127 part
.SetCte(DwMime::kCteQuotedPrintable
);
129 // Set content-disposition to attachment, with filename parameter
130 // (see RFC-1806 for information on this *experimental* header field)
132 DwString contDisp
= "attachment; filename=";
134 contDisp
+= aFilename
;
136 part
.SetContentDisposition(contDisp
);
138 // Set the encoded file contents as the body of the body part
140 part
.SetBody(encStr
);
142 // Make sure this is not the first part, since that is reserved for
145 if (NumberOfParts() == 0) {
146 SetBodyPart(1, part
);
154 void MessageWithAttachments::AttachBinaryFile(const char* aFilename
,
155 int aType
, int aSubtype
)
157 // Get the file contents
160 PutFileInString(aFilename
, str
);
162 // Encode using base64 encoding
165 DwEncodeBase64(str
, encStr
);
167 // Create a body part and set the necessary fields
169 MultipartBodyPart part
;
171 part
.SetSubtype(aSubtype
);
172 part
.SetCte(DwMime::kCteBase64
);
174 // Set content-disposition to attachment, with filename parameter
175 // (see RFC-1806 for information on this *experimental* header field)
177 DwString contDisp
= "attachment; filename=";
179 contDisp
+= aFilename
;
181 part
.SetContentDisposition(contDisp
);
183 // Set the encoded file contents as the body of the body part
185 part
.SetBody(encStr
);
187 // Make sure this is not the first part, since that is reserved for
190 if (NumberOfParts() == 0) {
191 SetBodyPart(1, part
);
199 int MessageWithAttachments::PutFileInString(const char* aFilename
,
204 int k
= stat(aFilename
, &statBuf
);
209 int fileSize
= (int) statBuf
.st_size
;
213 int bufSize
= fileSize
+ 8; // a little elbow room added
214 char* buf
= new char[bufSize
];
216 // Read the file into the buffer
218 FILE* fp
= fopen(aFilename
, "rb");
227 if (feof(fp
) || len
== fileSize
) {
235 // Place the buffer in the string
237 str
.TakeBuffer(buf
, bufSize
, 0, len
);