xref: /curl/docs/libcurl/curl_mime_init.md (revision b935fd4a)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_mime_init
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_MIMEPOST (3)
9  - curl_mime_addpart (3)
10  - curl_mime_free (3)
11  - curl_mime_subparts (3)
12Protocol:
13  - HTTP
14  - IMAP
15  - SMTP
16---
17
18# NAME
19
20curl_mime_init - create a mime handle
21
22# SYNOPSIS
23
24~~~c
25#include <curl/curl.h>
26
27curl_mime *curl_mime_init(CURL *easy_handle);
28~~~
29
30# DESCRIPTION
31
32curl_mime_init(3) creates a handle to a new empty mime structure.
33This mime structure can be subsequently filled using the mime API, then
34attached to some easy handle using option CURLOPT_MIMEPOST(3) within
35a curl_easy_setopt(3) call or added as a multipart in another mime
36handle's part using curl_mime_subparts(3).
37
38*easy_handle* is used for part separator randomization and error
39reporting. Since 7.87.0, it does not need to be the final target handle.
40
41Using a mime handle is the recommended way to post an HTTP form, format and
42send a multi-part email with SMTP or upload such an email to an IMAP server.
43
44# EXAMPLE
45
46~~~c
47int main(void)
48{
49  CURL *easy = curl_easy_init();
50  curl_mime *mime;
51  curl_mimepart *part;
52
53  /* Build an HTTP form with a single field named "data", */
54  mime = curl_mime_init(easy);
55  part = curl_mime_addpart(mime);
56  curl_mime_data(part, "This is the field data", CURL_ZERO_TERMINATED);
57  curl_mime_name(part, "data");
58
59  /* Post and send it. */
60  curl_easy_setopt(easy, CURLOPT_MIMEPOST, mime);
61  curl_easy_setopt(easy, CURLOPT_URL, "https://example.com");
62  curl_easy_perform(easy);
63
64  /* Clean-up. */
65  curl_easy_cleanup(easy);
66  curl_mime_free(mime);
67}
68~~~
69
70# AVAILABILITY
71
72As long as at least one of HTTP, SMTP or IMAP is enabled. Added in 7.56.0.
73
74# RETURN VALUE
75
76A mime struct handle, or NULL upon failure.
77