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