RE: Sample sending SMTP HTML email
Date: Mon, 9 Jan 2012 10:18:40 +0000
> I am new to this group. An answer may exist that I haven't
> discovered, sorry in advance if that is the case.
> I am trying to send an HTML email via SMTP with libcurl. I have an html
> file and am using the curl_easy_setopt(curl, CURLOPT_READDATA, f);
> Where f is a FILE*. I need to set the Content-Type in the header
> otherwise it just comes through as text. I was trying to build a header
> and use curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); But
> it doesn't seem to get used. I also tried to put a complete list of
> values in the html file; but that only partially worked. The headers
> worked, but the content data was missing data.
Are you wanting to send just HTML content in your emaisl or are you wanting
to include alternative content in there as well? For example you may be
sending to someone who uses an email client that only supports plain text.
As such your email may contain some pre-amble which a number of email
clients set to "This is a multi-part message in MIME format.", followed by
plain text alternative content, html content and an epilogue ;-)
> The ideal mechanism for me would be to have the HTML file separate
> from the header, but I can work around that if I have to. Can anyone
> point me to a working example of sending SMTP HTML email?
You might want to consider using a READFUNCTION instead so you can send back
the correct header and the html from a file by generating the header in
your own read function, which then reads your file line by line (or in
A very basic HTML email is described below:
From: Test Address <your-email_at_domain.com>
Reply-To: Test Address <your-email_at_domain.com>
To: libcurl development <curl-library_at_cool.haxx.se>
Subject: Test Email
Date: Mon, 09 Jan 2012 10:00:00 +0000
Content-Type: text/html; charset="us-ascii"
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii">
Note: The email addresses in the From and Reply-To headers can be the a
description followed by the email address with brackets around it (as I have
given above) or simply just the email address. If you give "nice" text then
typically email clients will display this rather than the email address.
For HTML email to be decoded correctly you need to set the charset, I only
use "us-ascii" currently but you must also set the Content-Transfer-Encoding
and Mime-version headers. The encoding mechanism could be "quoted-printable"
as I have given above but "7bit" may be simpler for you in the first
instance. As you are probably aware the header and the body are then
separated with a blank line.
If you are wanting to include alternative content in messages then let me
know and I can provide some examples using both Plain Text and Rich Text but
these are a little more complicated as they involve sub-messages which may
in turn contain sub-messages (if you are wanting to include in-line images
for your HTML emails or file attachments).
List admin: http://cool.haxx.se/list/listinfo/curl-library
Received on 2012-01-09