Header and body
MIME format is text split by the CRLF characters into the header and the body part. The only version defined so far is 1.0, and because of the way it is developed, not very likely that it will define other versions in the future. That means that various RFC documents extend MIME. The header keeps informations like sender, receiver, subject, message type, encoding, etc. The body stores the message content as well the attachments.
Content type
Beside the plain text, one might want to send the rich text format like HTML. In such cases, the content type which is plain text by default should be set to HTML:
#include <cstdlib>
#include <mailio/message.hpp>
using mailio::message;
int main()
{
message msg;
msg.content_type(message::media_type_t::TEXT, "html");
msg.from(mail_address("mailio", "address@mailio.dev"));
msg.add_recipient(mail_address("mailio", "address@mailio.dev"));
msg.subject("Hello, World!");
msg.content("<html><head></head><body><h1>Zdravo, Svete!</h1></body></html>");
return EXIT_SUCCESS;
}
The content type is consisting of the media type and subtype. The media types are defined for the categories of a text, image, audio, video
and application. (The multipart and message types will be explained later.) So in this case, the media type is again set to the text but
the subtype is changed from plain to html.
In the case of attaching files, the attachment types and subtypes were also set:
ifstream ifs1("aleph0.png", std::ios::binary);
ifstream ifs2("cv.txt");
list<tuple<std::istream&, string_t, message::content_type_t>> atts;
atts.push_back(make_tuple(std::ref(ifs1), "aleph0.png", message::content_type_t(message::media_type_t::IMAGE, "png")));
atts.push_back(make_tuple(std::ref(ifs2), "cv.txt", message::content_type_t(message::media_type_t::TEXT, "txt")));
msg.attach(atts);
In total, there are two content types of the two attachments, plus the content type of the message itself. Since the message itself contains
additional parts (attachments), then its type is multipart with the subtype mixed:
Content-Type: multipart/mixed; boundary="my_bound"
The attach method does set the message content type automatically to multipart/mixed
, so there
is no need to set it separately for the msg
object. The additional attribute
boundary
is used to set boundaries between various parts of the message.
Content transfer encoding
Since the MIME format is initially defined for seven bit characters, there is a need to encode message bodies with extended ASCII or UTF-8 characters. Two possible encodings for that purpose are Base64 and Quoted printable. By setting the content transfer encoding header, the message content will be encoded appropriately, so the non-ASCII body could be sent correctly.
#include <cstdlib>
#include <mailio/message.hpp>
using mailio::message;
int main()
{
message msg;
msg.content_type(message::media_type_t::TEXT, "plain", "utf-8");
msg.content_transfer_encoding(mime::content_transfer_encoding_t::BASE_64);
msg.from(mail_address("mailio", "address@mailio.dev"));
msg.add_recipient(mail_address("mailio", "address@mailio.dev"));
msg.subject("Hello, World!");
msg.content("Здраво, Свете!");
return EXIT_SUCCESS;
}
The content type is also specifying charset as the third parameter, together with the type and subtype. Instead of Base64, the Quoted
printable encoding can be used:
msg.content_transfer_encoding(mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
The end result for an email client would be the same, although the encodings are different.