note description: "[ cURL https://curl.haxx.se/libcurl/c/smtp-mail.html For original C version, please see: https://curl.haxx.se/libcurl/c/smtp-mime.html This is a simple example showing how to send mime mail using libcurl's SMTP capabilities ]" status: "See notice at end of class." legal: "See notice at end of class." date: "$Date$" revision: "$Revision$" EIS: "name=cURL smtp mime example", "src=https://curl.haxx.se/libcurl/c/smtp-mime.html", "protocol=uri" class APPLICATION create make feature {NONE} -- Initialization make -- Run application, showing how to send e-mails with mime using cURL SMTP. local l_result: INTEGER l_from: STRING l_to: STRING l_cc: STRING recipients: POINTER headers: POINTER slists: POINTER curl_externals: CURL_EXTERNALS l_message: STRING mime: POINTER alt: POINTER part: POINTER do io.put_string ("Eiffel cURL SMTP example showing how to send e-mails with mime.") io.put_new_line print ("%NLibCurl Version:" + (create {CURL_OPT_CONSTANTS}).libcurl_version.out) -- add the from email. l_from := "sender@example.org" l_to := "addressee@example.net" l_cc := "info@example.org" create l_message.make_from_string (message) l_message.replace_substring_all ("$TO", l_to) l_message.replace_substring_all ("$CC", l_cc) l_message.replace_substring_all ("$FROM", l_from) if curl_easy.is_dynamic_library_exists then curl_handle := curl_easy.init -- This is the URL for your mailserver "smtp://mail.example.com" curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "smtp://127.0.0.1") -- Note that this option isn't strictly required, omitting it will result -- in libcurl sending the MAIL FROM command with empty sender data. All -- autoresponses should have an empty reverse-path, and should be directed -- to the address in the reverse-path which triggered them. Otherwise, -- they could cause an endless loop. See RFC 5321 Section 4.5.5 for more -- details. curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_mail_from, l_from) -- Add two recipients, in this particular case they correspond to the -- To: and Cc: addressees in the header, but they could be any kind of -- * recipient. create curl_externals recipients := curl_externals.slist_append (recipients, l_to) recipients := curl_externals.slist_append (recipients, l_cc) curl_easy.setopt_slist (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_mail_rcpt, recipients) -- Build and set the message header list. headers := curl_externals.slist_append (headers, "Date: Tue, 22 Aug 2017 14:08:43 +0100") headers := curl_externals.slist_append (headers, "To: " + l_to) headers := curl_externals.slist_append (headers, "From: " + l_from + "(Example User)") headers := curl_externals.slist_append (headers, "CC: " + l_cc + "(Another Example User)") headers := curl_externals.slist_append (headers, "Message-ID: ") headers := curl_externals.slist_append (headers, "Subject: example sending a MIME-formatted message") curl_easy.setopt_slist (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_httpheader, headers) -- Build the mime message. mime := curl_externals.curl_mime_init (curl_handle) -- The inline part is an alterative proposing the html and the text versions of the e-mail. alt := curl_externals.curl_mime_init (curl_handle) -- HTML message. part := curl_externals.curl_mime_addpart (alt) l_result := curl_externals.curl_mime_data (part, inline_html, {CURL_OPT_CONSTANTS}.curl_zero_terminated) check valid: l_result = {CURL_CODES}.curle_ok end l_result := curl_externals.curl_mime_type (part, "text/html") check valid: l_result = {CURL_CODES}.curle_ok end -- Text message part := curl_externals.curl_mime_addpart (alt) l_result := curl_externals.curl_mime_data (part, inline_text, {CURL_OPT_CONSTANTS}.curl_zero_terminated) l_result := curl_externals.curl_mime_type (part, "text/html") check valid: l_result = {CURL_CODES}.curle_ok end -- Create the inline part. part := curl_externals.curl_mime_addpart (mime) l_result := curl_externals.curl_mime_subparts (part, alt) check valid: l_result = {CURL_CODES}.curle_ok end l_result := curl_externals.curl_mime_type (part, "multipart/alternative") check valid: l_result = {CURL_CODES}.curle_ok end slists := curl_externals.slist_append (default_pointer, "Content-Disposition: inline") l_result := curl_externals.curl_mime_headers (part, slists, 1) check valid: l_result = {CURL_CODES}.curle_ok end -- Add the current source program as an attachment part := curl_externals.curl_mime_addpart (mime) l_result := curl_externals.curl_mime_filedata (part, "application.e") curl_easy.setopt_mime (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_mimepost, mime) l_result := curl_easy.perform (curl_handle) if l_result = {CURL_CODES}.curle_ok then print ("%NSMTP sent ok") else print ("Error: cURL Error[" + l_result.out + "]") end curl_externals.slist_free_all (recipients) curl_externals.slist_free_all (headers) curl_externals.curl_mime_free (mime) -- Always cleanup curl_easy.cleanup (curl_handle) else io.error.put_string ("cURL library not found!") io.error.put_new_line end end feature {NONE} -- Implementation curl_easy: CURL_EASY_EXTERNALS -- cURL easy externals. once create Result end curl_handle: POINTER -- cURL handle. header_text: STRING = "[ Date: Tue, 22 Aug 2017 14:08:43 +0100 To: $TO From: $FROM (Example User) Cc: $CC (Another example User) Message-ID: Subject: example sending a MIME-formatted message ]" message: STRING = "[ Date: Mon, 29 Nov 2010 21:54:29 +1100 To: $TO From: $FROM (Example User) Cc: $CC (Another example User) Subject: SMTP example message The body of the message starts here It could be a lot of lines, could be MIME encoded, whatever. Check RFC5322. ]" inline_html: STRING = "[

This is the inline HTML message of the e-mail.


It could be a lot of HTML data that would be displayed by e-mail viewers able to handle HTML.

]" inline_text2: STRING = "[ This is the inline text message of the e-mail. It could be a lot of lines that would be displayed in an e-mail viewer that is not able to handle HTML ]" inline_text: STRING = "[ Simple Transactional Email
 
This is preheader text. Some clients will show this text as a preview.

Hi there,

Sometimes you just want to send a simple HTML email with a simple design and clear call to action. This is it.

Call To Action

This is a really simple email template. Its sole purpose is to get the recipient to click the button with no distractions.

Good luck! Hope it works.

 
]" note copyright: "Copyright (c) 1984-2018, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software 356 Storke Road, Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Website http://www.eiffel.com Customer support http://support.eiffel.com ]" end