Attaching a file
In addition to the simple message which sets the sender, recipient, subject and content, one can attach external files to the message.
To do that, use the standard file stream classes to access desired files. In the example, it is assumed that the files
aleph0.png
and infinity.png
already exist in the
example's directory. Put these files into the list of tuples which is then attached to the message:
#include <cstdlib>
#include <fstream>
#include <list>
#include <mailio/message.hpp>
using mailio::message;
using mailio::mail_address;
using std::ifstream;
using std::string;
using std::tuple;
using std::make_tuple;
using std::list;
int main()
{
message msg;
msg.from(mail_address("mailio", "address@mailio.dev"));
msg.add_recipient(mail_address("mailio", "address@mailio.dev"));
msg.subject("Zdravo, Svete!");
msg.content("Hello, World!");
ifstream ifs1("aleph0.png", std::ios::binary);
ifstream ifs2("infinity.png", std::ios::binary);
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);
return EXIT_SUCCESS;
}
The attaching list contains tuples of three elements: the first one is the file itself to be attached, the second one is the name of
the file meant to be in the attachment (not necessarily the same as the file name on disk), the third element is the content type. Every
content type consists of the type and subtype. The type is defined within the enum
mime::media_type_t
, while the subtype is an arbitrary string.