karastojko / mailio

mailio is a cross platform C++ library for MIME format and SMTP, POP3 and IMAP protocols. It is based on standard C++ 17 and Boost library.
Other
372 stars 98 forks source link

[Question] Image in the body of the message #139

Closed shakai2 closed 11 months ago

shakai2 commented 1 year ago

Please, could anyone provide me an example of how to place an image in the body of an HTML message ?

karastojko commented 1 year ago

Just generate the HTML content and set the appropriate content type, something like this:

msg.content_type(message::media_type_t::TEXT, "html", "utf-8");
msg.content("<html><body><img src=\"www.mailio.dev/image.png\"></body></html>");

Let me know whether this helps or you need more concrete example.

shakai2 commented 1 year ago

Hi @karastojko sorry for my lack of details.

I'm trying to place an embedded image in a HTML message.

I saw examples in other languages using CID in the image src: <img src="cid:img1" /> , and then create a MIME object with the image as content and the header with Content-ID: 'img1'.

I was able to create and send the message in HTML following the examples, but how do I create the MIME and put the image and CID in it?

Thanks in advance

karastojko commented 1 year ago

There is no Content ID in the header. Since it is of the same format as the Message ID, I believe I can easily add it. Let me try to play with HTML messages and I'l let you know about the results.

In the meantime, can you confirm that you have tried to set the Content Disposition header and it did not work?

shakai2 commented 1 year ago

I hadn't tried with the Content Disposition header before. So I just tried it and the result was:


-  With Content Disposition:  `embd_img.content_disposition(mailio::mime::content_disposition_t::INLINE);`  And with an attached file, The message was displayed correctly in Outlook with the embedded image. However in Gmail the embedded image appears in the attachment list along with the other file, and is not displayed in the body of the message

I didn't understand what the relationship between the attached file `msg.attach(atts)` and the mime object `msg.add_part(mime_img)`

Setting the Content Disposition header didn't work completely
karastojko commented 1 year ago

Can you please provider me a minimal example of this problem so I could try to reproduce it?

Regarding the Content-ID, let me play a little bit with the Content-ID header and the HTML content to see the results.

shakai2 commented 12 months ago

Here is the example code:

#include <fstream>
#include <sstream>
#include <mailio/message.hpp>
#include <mailio/mime.hpp>
#include <mailio/smtp.hpp>

int main(){
    std::string html_msg = R"html(
        <html>
            <head>
            </head> 
            <body>
            <div style="color:red; font-size:8rem; margin-left: 5rem;">
                Hello !
            </div>
            <div style="color:green; font-size:3rem; margin-left: 2rem; margin-top:2rem;">
                Testing
            </div>
            <div style="color:black; font-size:1.6rem; margin-left: 2rem; margin-top:2rem;">
                the image should appear below...
            </div>
            <div style="margin-left: 1.5rem; margin-top:1rem;">
                <img src="cid:ibj.jpg" />
            </div>
            </body>
        </html>
    )html";

    mailio::message msg;
    msg.header_codec(mailio::message::header_codec_t::BASE64);
    msg.from(mailio::mail_address("My test", "xxx@xxx.xxx.xx"));
    msg.add_recipient(mailio::mail_address("shakai2", "xxxxx@gmail.com"));
    msg.add_cc_recipient(mailio::mail_address("shakai2", "xxxx@xxxx.xxx.xx"));
    msg.subject("Test image 1");

    msg.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
    msg.content_type(mailio::message::media_type_t::TEXT, "html", "utf-8");
    msg.content(html_msg);

    std::ifstream in("teste.jpg");
    std::ostringstream out;
    out << in.rdbuf();
    std::string imgcontent = out.str();

    mailio::mime img;
    img.header_codec(mailio::mime::header_codec_t::BASE64);
    img.content_type(mailio::mime::media_type_t::IMAGE,"jpg");
    img.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::BASE_64);
    img.content_disposition(mailio::mime::content_disposition_t::INLINE);
    img.content(imgcontent);
    img.name("ibj.jpg");
    msg.add_part(img);

    mailio::smtps conn("xxxxxx.xxx.xx", 465);
    conn.authenticate("xxxxx@xxx.xxx.xx", "xxxxx", mailio::smtps::auth_method_t::LOGIN);
    conn.submit(msg);

    return EXIT_SUCCESS;
}

Scre

karastojko commented 12 months ago

So, you would like to create the HTML content together with an image. You have correctly set the image as a MIME part. Since you are dealing with the MIME parts, that means that the HTML also has to be a MIME part:

mailio::mime html;
html.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
html.content_type(mailio::message::media_type_t::TEXT, "html", "utf-8");
html.content(html_msg);
msg.add_part(html);

Additionally, the message itself has to be aware of the multipart structure, so you have to do:

msg.content_type(message::media_type_t::MULTIPART, "related");
msg.boundary("myrandomstring");

So, the overall code should look like

int main()
{
    std::string html_msg = R"html(
        <html>
            <head>
            </head> 
            <body>
            <div style="color:red; font-size:8rem; margin-left: 5rem;">
                Hello !
            </div>
            <div style="color:green; font-size:3rem; margin-left: 2rem; margin-top:2rem;">
                Testing
            </div>
            <div style="color:black; font-size:1.6rem; margin-left: 2rem; margin-top:2rem;">
                the image should appear below...
            </div>
            <div style="margin-left: 1.5rem; margin-top:1rem;">
                <img src="cid:ibj.jpg" />
            </div>
            </body>
        </html>
    )html";

    mailio::message msg;
    msg.content_type(message::media_type_t::MULTIPART, "related");
    msg.header_codec(mailio::message::header_codec_t::BASE64);
    msg.from(mailio::mail_address("My test", "xxx@xxx.xxx.xx"));
    msg.add_recipient(mailio::mail_address("shakai2", "xxxxx@gmail.com"));
    msg.add_cc_recipient(mailio::mail_address("shakai2", "xxxx@xxxx.xxx.xx"));    
    msg.subject("Test image 1");
    msg.boundary("myrandomstring");

    mailio::mime html;
    html.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
    html.content_type(mailio::message::media_type_t::TEXT, "html", "utf-8");
    html.content(html_msg);
    msg.add_part(html);

    std::ifstream in("teste.jpg");
    std::ostringstream out;
    out << in.rdbuf();
    std::string imgcontent = out.str();

    mailio::mime img;
    img.header_codec(mailio::mime::header_codec_t::BASE64);
    img.content_type(mailio::mime::media_type_t::IMAGE,"jpg");
    img.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::BASE_64);
    img.content_disposition(mailio::mime::content_disposition_t::INLINE);
    img.content(imgcontent);
    img.name("ibj.jpg");
    msg.add_part(img);

    mailio::smtps conn("xxxxxx.xxx.xx", 465);
    conn.authenticate("xxxxx@xxx.xxx.xx", "xxxxx", mailio::smtps::auth_method_t::LOGIN);    
    conn.submit(msg);

    return EXIT_SUCCESS;
}

Let me know if this solves the problem of the email format.

Maybe I should set automatically the boundary for the message and thus spare the effort for a programmer to do it. I will add an examples for the multipart messages, it is not obvious how it can be done with mailio.

Regarding the Content-ID, I am adding the support for it, I'll post a message when it's done.

karastojko commented 12 months ago

The Content-ID header is available with the latest commit.

shakai2 commented 11 months ago

So, you would like to create the HTML content together with an image. You have correctly set the image as a MIME part. Since you are dealing with the MIME parts, that means that the HTML also has to be a MIME part:

mailio::mime html;
html.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
html.content_type(mailio::message::media_type_t::TEXT, "html", "utf-8");
html.content(html_msg);
msg.add_part(html);

Additionally, the message itself has to be aware of the multipart structure, so you have to do:

msg.content_type(message::media_type_t::MULTIPART, "related");
msg.boundary("myrandomstring");

So, the overall code should look like

int main()
{
    std::string html_msg = R"html(
        <html>
            <head>
            </head> 
            <body>
            <div style="color:red; font-size:8rem; margin-left: 5rem;">
                Hello !
            </div>
            <div style="color:green; font-size:3rem; margin-left: 2rem; margin-top:2rem;">
                Testing
            </div>
            <div style="color:black; font-size:1.6rem; margin-left: 2rem; margin-top:2rem;">
                the image should appear below...
            </div>
            <div style="margin-left: 1.5rem; margin-top:1rem;">
                <img src="cid:ibj.jpg" />
            </div>
            </body>
        </html>
    )html";

    mailio::message msg;
    msg.content_type(message::media_type_t::MULTIPART, "related");
    msg.header_codec(mailio::message::header_codec_t::BASE64);
    msg.from(mailio::mail_address("My test", "xxx@xxx.xxx.xx"));
    msg.add_recipient(mailio::mail_address("shakai2", "xxxxx@gmail.com"));
    msg.add_cc_recipient(mailio::mail_address("shakai2", "xxxx@xxxx.xxx.xx"));    
    msg.subject("Test image 1");
    msg.boundary("myrandomstring");

    mailio::mime html;
    html.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
    html.content_type(mailio::message::media_type_t::TEXT, "html", "utf-8");
    html.content(html_msg);
    msg.add_part(html);

    std::ifstream in("teste.jpg");
    std::ostringstream out;
    out << in.rdbuf();
    std::string imgcontent = out.str();

    mailio::mime img;
    img.header_codec(mailio::mime::header_codec_t::BASE64);
    img.content_type(mailio::mime::media_type_t::IMAGE,"jpg");
    img.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::BASE_64);
    img.content_disposition(mailio::mime::content_disposition_t::INLINE);
    img.content(imgcontent);
    img.name("ibj.jpg");
    msg.add_part(img);

    mailio::smtps conn("xxxxxx.xxx.xx", 465);
    conn.authenticate("xxxxx@xxx.xxx.xx", "xxxxx", mailio::smtps::auth_method_t::LOGIN);    
    conn.submit(msg);

    return EXIT_SUCCESS;
}

Let me know if this solves the problem of the email format.

Maybe I should set automatically the boundary for the message and thus spare the effort for a programmer to do it. I will add an examples for the multipart messages, it is not obvious how it can be done with mailio.

Regarding the Content-ID, I am adding the support for it, I'll post a message when it's done.

Yes it solved the format problem.

shakai2 commented 11 months ago

The Content-ID header is available with the latest commit.

Now it works perfectly, Thank you!