Malay
Malay

Reputation: 51

How to send an attachment with email using libcurl and c language?

I am using curl library for email sending. I am able to send email successfully. I can see FROM, Subject and body properly at receiving end. Now my need is for attachment in the email. I have followed in this way: http://msdn.microsoft.com/en-us/library/ms526560(v=exchg.10).aspx

static const char *text[]={
  "Date: Wed, 06 Feb 2013 12:30:30 +1100\n",
  "To: " RECIPIENT "\n",
  "From: " MAILFROM "\n",
  "MIME-Version: 1.0",
  "Content-Type: multipart/mixed",
  "boundary=""XXXXX""\n",
  "Subject: email example message\n",
  "--XXXXX",
  "\n", /* empty line to divide headers from body, see RFC5322 */
  "The body of the message starts here.\n",
  "Check RFC5322.\n",
  "--XXXXX",
  "Content-Type: text/plain" "\n",
  "Content-Disposition: attachment" "\n",
  "filename="FILE_PATH "\n",    
  "--XXXXX--",
  NULL
};

They are suggesting RFC5322. But in that I could not find anything like attachment. Is it correct? Any other way using libcurl + C language is also welcomed.

I think the person named as Jorge has asked same kind of question on october 13,2012. But the answer is not there.

Upvotes: 3

Views: 8971

Answers (4)

zgrw
zgrw

Reputation: 127

It can be done as below;

static const char *payload_text[] = {
  "To: " TO "\r\n",
  "From: " FROM "(Example User)\r\n",
  "Cc: " CC "(Another example User)\r\n",
  "Subject: SMTPS Example\r\n",
  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  "User-Agent: My eMail Client\r\n",
  "MIME-Version: 1.0\r\n",
  "Content-Type: multipart/mixed;\r\n",
   " boundary=\"------------030203080101020302070708\"\r\n",    
  "\r\nThis is a multi-part message in MIME format.\r\n",
  "--------------030203080101020302070708\r\n",
  "Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
  "Content-Transfer-Encoding: 7bit\r\n",
  "\r\n", /* empty line to divide headers from body, see RFC5322 */
  "The body of the message starts here.\r\n",
  "\r\n",
  "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
  "Check RFC5322.\r\n\r\n",
  "--------------030203080101020302070708\r\n",
  "Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
  "  name=\"send.c\"\r\n",
  "Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
  "Content-Disposition: attachment;\r\n",
  "  filename=\"abc.txt\"\r\n",
  "\r\n",
  "bla bla file content is here\r\n",
  "--------------030203080101020302070708--\r\n",
  NULL
};

Upvotes: 0

youngmoney
youngmoney

Reputation: 21

The easiest way is to encode using something like base64 encoding which turns binary data into ASCII characters that are able to be sent via email. base64 encode the file and insert the string as below. Replace everything in caps with the correct stuff.

--XXXXboundary text
Content-Type: application/FILETYPE; name="FILENAME"
Content-Disposition: attachment; filename="FILENAME"
Content-Transfer-Encoding: base64
STRINGFROMBASE64ENCODING

Heres a thread on base64: How do I base64 encode (decode) in C?

Upvotes: 2

Brecht Sanders
Brecht Sanders

Reputation: 61

Maybe you should take a look at libquickmail (http://sf.net/p/libquickmail/). It does all the magic needed to attach files and it uses libcurl for the SMTP transport (or even without libcurl in libquickmaillight).

Upvotes: 0

sundaylover
sundaylover

Reputation: 1

I think you are right, but why did you attached a null file? I think you need put something in the final attachment body apartment.

For example:

--XXXXboundary text 
Content-Type: text/plain;
Content-Disposition: attachment;
        filename="test.txt"

this is the attachment text ---- this is attachment content

--XXXXboundary text--

Upvotes: 0

Related Questions