File: message.cpp

package info (click to toggle)
mailio 0.25.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,232 kB
  • sloc: cpp: 11,743; makefile: 10
file content (144 lines) | stat: -rw-r--r-- 5,411 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*

message.cpp
-----------

Various encodings when dealing with the message.
The example keeps code snippets in the various encodings. To see them properly, change the file encoding as marked in the snippet.


Copyright (C) 2022, Tomislav Karastojkovic (http://www.alepho.com).

Distributed under the FreeBSD license, see the accompanying file LICENSE or
copy at http://www.freebsd.org/copyright/freebsd-license.html.

*/


#include <iostream>
#include <mailio/mailboxes.hpp>
#include <mailio/message.hpp>


using std::cout;
using std::endl;
using std::string;
using mailio::string_t;
using mailio::mail_address;
using mailio::mime;
using mailio::message;


int main()
{
    // Set the file encoding to UTF-8 to properly see the letters in this snippet.
    {
        message msg;
        msg.from(mail_address("mail io", "contact@mailio.dev"));
        msg.add_recipient(mail_address("mail io", "contact@mailio.dev"));
        // The subject is automatically determined as UTF-8 since it contains 8bit characters. It is encoded as Quoted Printable,
        msg.subject("Здраво, Свете!", mailio::codec::codec_t::QUOTED_PRINTABLE);
        msg.content("Hello, World!");
        string msg_str;
        msg.format(msg_str);
        cout << msg_str << endl;
        // The subject is printed as `=?UTF-8?Q?=D0=97=D0=B4=D1=80=D0=B0=D0=B2=D0=BE,_=D0=A1=D0=B2=D0=B5=D1=82=D0?=\r\n =?UTF-8?Q?=B5!?=`
    }

    // Set the file encoding to UTF-8 to properly see the letters in this snippet.
    {
        message msg;
        msg.from(mail_address("mail io", "contact@mailio.dev"));
        msg.add_recipient(mail_address("mail io", "contact@mailio.dev"));
        // The subject remains in 8bit because such header is set.
        msg.subject("Здраво, Свете!", mailio::codec::codec_t::UTF8);
        msg.content("Hello, World!");
        string msg_str;
        msg.format(msg_str);
        cout << msg_str << endl;
    }

    // Set the file encoding to ISO-8859-2 to properly see the letters in this snippet.
    {
        message msg;
        msg.from(mail_address("mail io", "contact@mailio.dev"));
        msg.add_recipient(mail_address("mail io", "contact@mailio.dev"));
        msg.content_transfer_encoding(mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
        msg.subject_raw(string_t("Zdravo, Svetiu!", "iso-8859-2"));
        msg.content("Hello, World!");
        string msg_str;
        msg.format(msg_str);
        cout << msg_str << endl;
        // The subject is printed as `Zdravo, Svetiću!` in the ISO-8859-2 encoding.
    }

    // Set the file encoding to ISO-8859-5 to properly see the letters in this snippet.
    {
        message msg;
        msg.from(mail_address("mail io", "contact@mailio.dev"));
        msg.add_recipient(mail_address("mail io", "contact@mailio.dev"));
        msg.content_transfer_encoding(mime::content_transfer_encoding_t::BASE_64);
        msg.subject_raw(string_t(", ", "iso-8859-5"));
        msg.content("Hello, World!");
        string msg_str;
        msg.format(msg_str);
        cout << msg_str << endl;
    }

    // Set the file encoding to UTF-8 to properly see the letters in this snippet.
    {
        message msg;
        msg.from(mail_address("mail io", "contact@mailio.dev"));
        msg.add_recipient(mail_address("mail io", "contact@mailio.dev"));
        msg.content_transfer_encoding(mime::content_transfer_encoding_t::BASE_64);
        msg.subject_raw(string_t("Здраво, Свете!", "utf-8",
            mailio::codec::codec_t::QUOTED_PRINTABLE));
        msg.content("Hello, World!");
        string msg_str;
        msg.format(msg_str);
        cout << msg_str << endl;
        // The subject is printed as `=?UTF-8?Q?=D0=97=D0=B4=D1=80=D0=B0=D0=B2=D0=BE,_=D0=A1=D0=B2=D0=B5=D1=82=D0=B5!?=`.
    }

    {
        string msg_str = "From: mail io <contact@mailio.dev>\r\n"
            "To: mail io <contact@mailio.dev>\r\n"
            "Date: Sat, 18 Jun 2022 05:56:34 +0000\r\n"
            "Subject: =?ISO-8859-2?Q?Zdravo,_Sveti=E6u!?=\r\n"
            "\r\n"
            "Zdravo, Svete!\r\n";
        message msg;
        msg.parse(msg_str);
        cout << msg.subject() << endl;
        // The subject is printed as `Zdravo, Svetiću!` in the ISO-8859-2 encoding.
    }

    {
        string msg_str = "From: mail io <contact@mailio.dev>\r\n"
            "To: mail io <contact@mailio.dev>\r\n"
            "Date: Sat, 18 Jun 2022 05:56:34 +0000\r\n"
            "Subject: =?ISO-8859-5?Q?=B7=D4=E0=D0=D2=DE,_=C1=D2=D5=E2=D5!?=\r\n"
            "\r\n"
            "Hello, World!\r\n";
        message msg;
        msg.parse(msg_str);
        cout << msg.subject() << endl;
        // The subject is printed as `Здраво, Свете!` in the ISO-8859-5 encoding.
    }

    {
        string msg_str = "From: mail io <contact@mailio.dev>\r\n"
            "To: mail io <contact@mailio.dev>\r\n"
            "Date: Sat, 18 Jun 2022 05:56:34 +0000\r\n"
            "Subject: =?UTF-8?Q?=D0=97=D0=B4=D1=80=D0=B0=D0=B2=D0=BE,_=D0=A1=D0=B2=D0=B5=D1=82=D0=B5!?=\r\n"
            "\r\n"
            "Hello, World!\r\n";
        message msg;
        msg.line_policy(mailio::codec::line_len_policy_t::MANDATORY);
        msg.parse(msg_str);
        cout << msg.subject() << endl;
        // The subject is printed as `Здраво, Свете!` in the UTF-8 encoding.
    }

    return EXIT_SUCCESS;
}