113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci/* <DESC> 2613498266Sopenharmony_ci * Send email on behalf of another user with SMTP 2713498266Sopenharmony_ci * </DESC> 2813498266Sopenharmony_ci */ 2913498266Sopenharmony_ci 3013498266Sopenharmony_ci#include <stdio.h> 3113498266Sopenharmony_ci#include <string.h> 3213498266Sopenharmony_ci#include <curl/curl.h> 3313498266Sopenharmony_ci 3413498266Sopenharmony_ci/* 3513498266Sopenharmony_ci * This is a simple example show how to send an email using libcurl's SMTP 3613498266Sopenharmony_ci * capabilities. 3713498266Sopenharmony_ci * 3813498266Sopenharmony_ci * Note that this example requires libcurl 7.66.0 or above. 3913498266Sopenharmony_ci */ 4013498266Sopenharmony_ci 4113498266Sopenharmony_ci/* The libcurl options want plain addresses, the viewable headers in the mail 4213498266Sopenharmony_ci * can get a full name as well. 4313498266Sopenharmony_ci */ 4413498266Sopenharmony_ci#define FROM_ADDR "<ursel@example.org>" 4513498266Sopenharmony_ci#define SENDER_ADDR "<kurt@example.org>" 4613498266Sopenharmony_ci#define TO_ADDR "<addressee@example.net>" 4713498266Sopenharmony_ci 4813498266Sopenharmony_ci#define FROM_MAIL "Ursel " FROM_ADDR 4913498266Sopenharmony_ci#define SENDER_MAIL "Kurt " SENDER_ADDR 5013498266Sopenharmony_ci#define TO_MAIL "A Receiver " TO_ADDR 5113498266Sopenharmony_ci 5213498266Sopenharmony_cistatic const char *payload_text = 5313498266Sopenharmony_ci "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n" 5413498266Sopenharmony_ci "To: " TO_MAIL "\r\n" 5513498266Sopenharmony_ci "From: " FROM_MAIL "\r\n" 5613498266Sopenharmony_ci "Sender: " SENDER_MAIL "\r\n" 5713498266Sopenharmony_ci "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@" 5813498266Sopenharmony_ci "rfcpedant.example.org>\r\n" 5913498266Sopenharmony_ci "Subject: SMTP example message\r\n" 6013498266Sopenharmony_ci "\r\n" /* empty line to divide headers from body, see RFC 5322 */ 6113498266Sopenharmony_ci "The body of the message starts here.\r\n" 6213498266Sopenharmony_ci "\r\n" 6313498266Sopenharmony_ci "It could be a lot of lines, could be MIME encoded, whatever.\r\n" 6413498266Sopenharmony_ci "Check RFC 5322.\r\n"; 6513498266Sopenharmony_ci 6613498266Sopenharmony_cistruct upload_status { 6713498266Sopenharmony_ci size_t bytes_read; 6813498266Sopenharmony_ci}; 6913498266Sopenharmony_ci 7013498266Sopenharmony_cistatic size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) 7113498266Sopenharmony_ci{ 7213498266Sopenharmony_ci struct upload_status *upload_ctx = (struct upload_status *)userp; 7313498266Sopenharmony_ci const char *data; 7413498266Sopenharmony_ci size_t room = size * nmemb; 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { 7713498266Sopenharmony_ci return 0; 7813498266Sopenharmony_ci } 7913498266Sopenharmony_ci 8013498266Sopenharmony_ci data = &payload_text[upload_ctx->bytes_read]; 8113498266Sopenharmony_ci 8213498266Sopenharmony_ci if(data) { 8313498266Sopenharmony_ci size_t len = strlen(data); 8413498266Sopenharmony_ci if(room < len) 8513498266Sopenharmony_ci len = room; 8613498266Sopenharmony_ci memcpy(ptr, data, len); 8713498266Sopenharmony_ci upload_ctx->bytes_read += len; 8813498266Sopenharmony_ci 8913498266Sopenharmony_ci return len; 9013498266Sopenharmony_ci } 9113498266Sopenharmony_ci 9213498266Sopenharmony_ci return 0; 9313498266Sopenharmony_ci} 9413498266Sopenharmony_ci 9513498266Sopenharmony_ciint main(void) 9613498266Sopenharmony_ci{ 9713498266Sopenharmony_ci CURL *curl; 9813498266Sopenharmony_ci CURLcode res = CURLE_OK; 9913498266Sopenharmony_ci struct curl_slist *recipients = NULL; 10013498266Sopenharmony_ci struct upload_status upload_ctx = { 0 }; 10113498266Sopenharmony_ci 10213498266Sopenharmony_ci curl = curl_easy_init(); 10313498266Sopenharmony_ci if(curl) { 10413498266Sopenharmony_ci /* This is the URL for your mailserver. In this example we connect to the 10513498266Sopenharmony_ci smtp-submission port as we require an authenticated connection. */ 10613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com:587"); 10713498266Sopenharmony_ci 10813498266Sopenharmony_ci /* Set the username and password */ 10913498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_USERNAME, "kurt"); 11013498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_PASSWORD, "xipj3plmq"); 11113498266Sopenharmony_ci 11213498266Sopenharmony_ci /* Set the authorization identity (identity to act as) */ 11313498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_SASL_AUTHZID, "ursel"); 11413498266Sopenharmony_ci 11513498266Sopenharmony_ci /* Force PLAIN authentication */ 11613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, "AUTH=PLAIN"); 11713498266Sopenharmony_ci 11813498266Sopenharmony_ci /* Note that this option is not strictly required, omitting it will result 11913498266Sopenharmony_ci * in libcurl sending the MAIL FROM command with empty sender data. All 12013498266Sopenharmony_ci * autoresponses should have an empty reverse-path, and should be directed 12113498266Sopenharmony_ci * to the address in the reverse-path which triggered them. Otherwise, 12213498266Sopenharmony_ci * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more 12313498266Sopenharmony_ci * details. 12413498266Sopenharmony_ci */ 12513498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_ADDR); 12613498266Sopenharmony_ci 12713498266Sopenharmony_ci /* Add a recipient, in this particular case it corresponds to the 12813498266Sopenharmony_ci * To: addressee in the header. */ 12913498266Sopenharmony_ci recipients = curl_slist_append(recipients, TO_ADDR); 13013498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 13113498266Sopenharmony_ci 13213498266Sopenharmony_ci /* We are using a callback function to specify the payload (the headers and 13313498266Sopenharmony_ci * body of the message). You could just use the CURLOPT_READDATA option to 13413498266Sopenharmony_ci * specify a FILE pointer to read from. */ 13513498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 13613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 13713498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 13813498266Sopenharmony_ci 13913498266Sopenharmony_ci /* Send the message */ 14013498266Sopenharmony_ci res = curl_easy_perform(curl); 14113498266Sopenharmony_ci 14213498266Sopenharmony_ci /* Check for errors */ 14313498266Sopenharmony_ci if(res != CURLE_OK) 14413498266Sopenharmony_ci fprintf(stderr, "curl_easy_perform() failed: %s\n", 14513498266Sopenharmony_ci curl_easy_strerror(res)); 14613498266Sopenharmony_ci 14713498266Sopenharmony_ci /* Free the list of recipients */ 14813498266Sopenharmony_ci curl_slist_free_all(recipients); 14913498266Sopenharmony_ci 15013498266Sopenharmony_ci /* curl will not send the QUIT command until you call cleanup, so you 15113498266Sopenharmony_ci * should be able to reuse this connection for additional messages 15213498266Sopenharmony_ci * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and 15313498266Sopenharmony_ci * calling curl_easy_perform() again. It may not be a good idea to keep 15413498266Sopenharmony_ci * the connection open for a long time though (more than a few minutes may 15513498266Sopenharmony_ci * result in the server timing out the connection), and you do want to 15613498266Sopenharmony_ci * clean up in the end. 15713498266Sopenharmony_ci */ 15813498266Sopenharmony_ci curl_easy_cleanup(curl); 15913498266Sopenharmony_ci } 16013498266Sopenharmony_ci 16113498266Sopenharmony_ci return (int)res; 16213498266Sopenharmony_ci} 163