Re: [gmime-devel] Create Message Example without steams



Hi Paul,

On 10/16/2013 12:44 AM, Paul C wrote:
Hey guys, sorry to post this newbie question, but I'm used to phpmailer and I'm switching my email injector over to C, so I choose Gmime. I'm looking at the examples and I can't seem to find one in the context I'm looking for. I'm pulling the html message, text message, and any attachments from a database, not a file, so I'm getting a little confused on how to cut out the stream functions and just use variables to create a new message instead.

Streams are actually perfect for this sort of thing because you could write a custom stream class that loads data from a database.

Anyway, I gather from your email that you probably aren't comfortable with writing your own stream, so I would suggest using GMimeStreamMem.

I've written in C before but I'm not that great with it, so if anyone has an example of how to create a message with gmime with the content of each part already assigned to variables, that would help a lot.

"assigned to variables" doesn't help much because variables can be of any type. Streams are variables, too ;-)

I'm going to assume that what you have are char pointers/arrays:

GMimeStreamMem *mem = g_mime_stream_mem_new_with_buffer (variable, strlen (variable));

Now you have a stream :-)


Something like with a variable holding the text message, another variable holding the html message, and the attachment's file contents already assigned to a 3rd variable. Any example with all or even just a part of this would be good, I'll be making a socket connection to postfix after the message is created, that part I already have. I realize the socket to postfix is a stream, but my current code has the entire message (headers, and all mime parts) content in a variable.

I don't know what type of variables you have that are holding a text message, html message and attachment file contents. Are the first two GMimeMessages and the last is a char*? What do you want to do with these variables? Your question is way too vague to answer :-\

Are you asking how to parse a text message and how to parse an html message? Do you even really mean a "text message" and an "html message"? Or do you mean that you have some text and html content that you want to use as the body of a message and you are asking how to create it?

I'm going to *guess* that you mean that you have some text and html that you want to send as a multipart/alternative (i.e. the text and the html are more-or-less the same content, just in different formats).

GMimeMessage *message;
GMimeDataWrapper *content;
GMimeStreamMem *mem;
GMimeMultipart *multipart;
GMimePart *part;

/* create the multipart/alternative part */
multipart = g_mime_multipart_new_with_subtype ("alternative");

/* create the text/plain part and add it to the multipart/alternative */
mem = g_mime_stream_mem_new_with_buffer (text, strlen (text));
content = g_mime_data_wrapper_new_with_stream ((GMimeStream *) mem, GMIME_CONTENT_ENCODING_DEFAULT);
g_object_unref (mem);

part = g_mime_part_new_with_type ("text", "plain");
/* if the charset of the text isn't US-ASCII, you will need to set the charset... utf-8, iso-8859-1, etc */ /*g_mime_object_set_content_type_parameter ((GMimeObject *) part, "charset", "utf-8");*/ g_mime_part_set_content_encoding (part, GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE);
g_mime_part_set_content_object (part, content);
g_object_unref (content);

g_mime_multipart_add (multipart, (GMimeObject *) part);

/* create the text/html part and add it to the multipart/alternative */
mem = g_mime_stream_mem_new_with_buffer (text, strlen (html));
content = g_mime_data_wrapper_new_with_stream ((GMimeStream *) mem, GMIME_CONTENT_ENCODING_DEFAULT);
g_object_unref (mem);

part = g_mime_part_new_with_type ("text", "html");
/* if the charset of the html isn't US-ASCII, you will need to set the charset... utf-8, iso-8859-1, etc */ /*g_mime_object_set_content_type_parameter ((GMimeObject *) part, "charset", "utf-8");*/ g_mime_part_set_content_encoding (part, GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE);
g_mime_part_set_content_object (part, content);
g_object_unref (content);

g_mime_multipart_add (multipart, (GMimeObject *) part);

/* create the message and add the multipart/alternative */
message = g_mime_message_new (TRUE);
g_mime_message_set_mime_part (message, (GMimeObject *) multipart);
g_object_unref (multipart);

/* TODO: set some headers like sender, subject, date, to, cc, etc */


Hopefully that answers your question,

Jeff



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]