Comments (10)
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", "[email protected]"));
msg.add_recipient(mailio::mail_address("shakai2", "[email protected]"));
msg.add_cc_recipient(mailio::mail_address("shakai2", "[email protected]"));
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("[email protected]", "xxxxx", mailio::smtps::auth_method_t::LOGIN);
conn.submit(msg);
return EXIT_SUCCESS;
}
from mailio.
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.
from mailio.
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
from mailio.
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?
from mailio.
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);
The message seems to be broken and parts of code began to appear in the message body :
(start of the message contents)
-- Content-Type: text/plain; charsetutf-8 Content-Transfer-Encoding: Quoted-Printable
(message contents)
(message contents)
(and in the place where the image should be rendered)
-- Content-Type: image/jpg; name"ibj.jpg" Content-Transfer-Encoding: Base64 Content-Disposition: inline; filename"ibj.jpg" /9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAUDBAgICAgICAgICAgGCAgIBwcHBwgICAgICAgICAgI.........
- 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
from mailio.
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.
from mailio.
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", "[email protected]"));
msg.add_recipient(mailio::mail_address("shakai2", "[email protected]"));
msg.add_cc_recipient(mailio::mail_address("shakai2", "[email protected]"));
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("[email protected]", "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.
from mailio.
The Content-ID
header is available with the latest commit.
from mailio.
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", "[email protected]")); msg.add_recipient(mailio::mail_address("shakai2", "[email protected]")); msg.add_cc_recipient(mailio::mail_address("shakai2", "[email protected]")); 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("[email protected]", "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.
from mailio.
The
Content-ID
header is available with the latest commit.
Now it works perfectly, Thank you!
from mailio.
Related Issues (20)
- Problems compiling the example code with CMake HOT 1
- IMAP(Switching to SSL failed.) HOT 2
- IMAP list folder, encoding error ? HOT 12
- Switching to SSL failed HOT 8
- send imap quoted string HOT 9
- imap fetch command response parsing HOT 2
- parsing imap quoted string HOT 2
- segmentation fault in imap fetch function
- parsing imap resp-text
- Please, could anyone provide me an example of how to get the mail content as a unencoded string? HOT 2
- Linking errors HOT 11
- Is it ok to use `-std=c++17` instead of `-std=gnu++1z` HOT 1
- smtps::connection::submit response HOT 2
- add_bcc_recipient HOT 8
- Switching to SSL failed HOT 6
- Parsing failure. HOT 8
- Allow formatting of BCC addresses HOT 2
- when parsing the header of this mime crashed! HOT 14
- mailio stop working with vcpkg? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mailio.