cURL / Mailing Lists / curl-library / Single Mail


Re: Sample sending SMTP HTML email

From: brett kaylor <>
Date: Mon, 9 Jan 2012 14:49:01 -0800 (PST)

Thanks Steve. I don't intend to include alternative content too; just HTML. I will try the READFUNCTION.

----- Original Message -----
From: Steve Holme <>
To: 'libcurl development' <>
Sent: Monday, January 9, 2012 3:18 AM
Subject: RE: Sample sending SMTP HTML email

Hi Brett,

> I am new to this group. An answer may exist that I haven't
> discovered, sorry in advance if that is the case.

Welcome aboard.

> 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
blocks) afterwards.

A very basic HTML email is described below:

From: Test Address <>
Reply-To: Test Address <>
To: libcurl development <>
Subject: Test Email
Date: Mon, 09 Jan 2012 10:00:00 +0000
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Mime-version: 1.0

<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii">
<p>Hello World</p>

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).

Kind Regards


List admin:

List admin:
Received on 2012-01-09