|
|
@@ -1,31 +1,162 @@
|
|
|
-/**
|
|
|
- * @file
|
|
|
- * @brief SMTP client library.
|
|
|
- * @author James Humphrey (mail@somnisoft.com)
|
|
|
- * @version 1.00
|
|
|
- *
|
|
|
- * This SMTP client library allows the user to send emails to an SMTP server.
|
|
|
- * The user can include custom headers and MIME attachments.
|
|
|
- *
|
|
|
- * This software has been placed into the public domain using CC0.
|
|
|
- */
|
|
|
+#include <stdint.h>
|
|
|
+#include <pthread.h>
|
|
|
+#include "cfg.h"
|
|
|
+#include "common.h"
|
|
|
+#include "thread.h"
|
|
|
+#include "xlist.h"
|
|
|
+#include "elog.h"
|
|
|
+#include "lock.h"
|
|
|
+#include "sqlite_handle.h"
|
|
|
|
|
|
-/**
|
|
|
- * @mainpage smtp-client
|
|
|
- *
|
|
|
- * This section contains documentation generated directly from the source
|
|
|
- * code.
|
|
|
- *
|
|
|
- * To view the repository details, visit the main smtp-client page at
|
|
|
- * <a href='https://www.somnisoft.com/smtp-client'>
|
|
|
- * www.somnisoft.com/smtp-client
|
|
|
- * </a>.
|
|
|
- */
|
|
|
|
|
|
-#include "cfg.h"
|
|
|
-#include <pthread.h>
|
|
|
+//#define USE_LIBCURL
|
|
|
+
|
|
|
+#if 1
|
|
|
+ #define LOGD log_d
|
|
|
+ #define LOGE log_e
|
|
|
+ #define LOGW log_w
|
|
|
+#else
|
|
|
+ #define LOGD printf
|
|
|
+ #define LOGE printf
|
|
|
+ #define LOGW printf
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+#ifdef USE_LIBCURL
|
|
|
+#include <curl/curl.h>
|
|
|
+#endif
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ char sub[128];
|
|
|
+ char body[2048];
|
|
|
+ char attach[128];
|
|
|
+}mail_pkt_t;
|
|
|
+typedef struct {
|
|
|
+ handle_t list;
|
|
|
+ mail_info_t *info;
|
|
|
+
|
|
|
+#ifdef USE_LIBCURL
|
|
|
+ CURL *curl;
|
|
|
+#endif
|
|
|
+
|
|
|
+ GlobalDeviceManager *dm;
|
|
|
+ int inited;
|
|
|
+}mail_handle_t;
|
|
|
+static mail_handle_t maHandle;
|
|
|
+
|
|
|
+
|
|
|
+#ifdef USE_LIBCURL
|
|
|
+
|
|
|
+static char send_buff[1024] = {0};
|
|
|
+struct upload_status
|
|
|
+{
|
|
|
+ size_t bytes_read;
|
|
|
+};
|
|
|
+
|
|
|
+static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
|
|
|
+{
|
|
|
+ struct upload_status *upload_ctx = (struct upload_status *)userp;
|
|
|
+ const char *data;
|
|
|
+ size_t room = size * nmemb;
|
|
|
+
|
|
|
+ if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ data = &send_buff[upload_ctx->bytes_read];
|
|
|
+
|
|
|
+ if(data) {
|
|
|
+ size_t len = strlen(data);
|
|
|
+ if(room < len)
|
|
|
+ len = room;
|
|
|
+ memcpy(ptr, data, len);
|
|
|
+ upload_ctx->bytes_read += len;
|
|
|
+ return len;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static void fill_send_buff(const char *to,const char *from,const char *subject,const char *body)
|
|
|
+{
|
|
|
+ time_t timer;//time_t就是long int 类型
|
|
|
+ struct tm *tblock;
|
|
|
+ timer = time(NULL);
|
|
|
+ tblock = localtime(&timer);
|
|
|
+ memset(send_buff,0,1024);
|
|
|
+ sprintf(send_buff,"Subject: %s\r\n"
|
|
|
+ "From: %s\r\n"
|
|
|
+ "To: %s\r\n"
|
|
|
+ "Date: %s\r\n"
|
|
|
+ "\r\n"
|
|
|
+ "%s\r\n"
|
|
|
+ "\r\n",subject,from,to,asctime(tblock),body);
|
|
|
+}
|
|
|
+static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char *attach, int multi)
|
|
|
+{
|
|
|
+ CURLcode res = CURLE_OK;
|
|
|
+ char url_buff[256]={0};
|
|
|
+ char to_buff[256]={0};
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
+ struct upload_status upload_ctx = { 0 };
|
|
|
+ struct curl_slist *recipients = NULL;
|
|
|
+ mail_recv_t *recv=(mail_recv_t*)addr;
|
|
|
+
|
|
|
+ sprintf(url_buff,"%s:%s",send->server,send->port);
|
|
|
+
|
|
|
+ res = curl_easy_setopt(h->curl, CURLOPT_URL,url_buff);
|
|
|
+ LOGD("url------%s %d\n",url_buff,res);
|
|
|
+
|
|
|
+ res = curl_easy_setopt(h->curl, CURLOPT_USERNAME,send->account); //setting user
|
|
|
+ LOGD("usrname------%s %d\n",send->account,res);
|
|
|
+
|
|
|
+ res = curl_easy_setopt(h->curl, CURLOPT_PASSWORD, send->password); //setting password
|
|
|
+ LOGD("passworld------%s %d\n",send->password,res);
|
|
|
+
|
|
|
+ //res = curl_easy_setopt(h->curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); //setting openssl/tls
|
|
|
+ LOGD("SSL------ %d\n",res);
|
|
|
+
|
|
|
+ //curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
|
|
|
+ res = curl_easy_setopt(h->curl, CURLOPT_MAIL_FROM,send->account);
|
|
|
+ LOGD("From------%s %d\n",send->account,res);
|
|
|
+
|
|
|
+ curl_easy_setopt(h->curl, CURLOPT_READFUNCTION, payload_source);
|
|
|
+ curl_easy_setopt(h->curl, CURLOPT_READDATA, &upload_ctx);
|
|
|
+
|
|
|
+ if(multi) {
|
|
|
+ for(int i=0; i<MAIL_RECV_MAX; i++) {
|
|
|
+ if(recv[i].use && strlen(recv[i].account)>0) {
|
|
|
+ recipients = curl_slist_append(recipients, recv[i].account);
|
|
|
+ //recipients = curl_slist_append(recipients, CC_MAIL);
|
|
|
+ strcat(to_buff,recv[i].account);
|
|
|
+ strcat(to_buff,";");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ char *sendto=(char*)addr;
|
|
|
+ recipients = curl_slist_append(recipients,sendto);
|
|
|
+ strcat(to_buff,sendto);
|
|
|
+ strcat(to_buff,";");
|
|
|
+ }
|
|
|
+ fill_send_buff(to_buff,send->account,sub,body);
|
|
|
+
|
|
|
+ curl_easy_setopt(h->curl, CURLOPT_UPLOAD, 1L);
|
|
|
+ res = curl_easy_setopt(h->curl, CURLOPT_MAIL_RCPT, recipients);
|
|
|
+ LOGD("rcpt------ %d\n",res);
|
|
|
+ res = curl_easy_setopt(h->curl, CURLOPT_VERBOSE, 1L);
|
|
|
+ LOGD("verbose------ %d\n",res);
|
|
|
+ res = curl_easy_perform(h->curl);
|
|
|
+ LOGD("send------ %d\n",res);
|
|
|
+ if(res != CURLE_OK) {
|
|
|
+ LOGE("send failed!!!!\n");
|
|
|
+ }
|
|
|
+ curl_slist_free_all(recipients);
|
|
|
+ //curl_easy_cleanup(h->curl);
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
|
|
|
-#ifdef USE_SMTP
|
|
|
+#else
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
#include <stddef.h>
|
|
|
@@ -503,8 +634,6 @@ void smtp_attachment_clear_all(struct smtp *const smtp);
|
|
|
*/
|
|
|
#define SMTP_INTERNAL_DEFINE
|
|
|
|
|
|
-#include "smtp.h"
|
|
|
-
|
|
|
/*
|
|
|
* The SMTP_TEST converts some library routines into special test seams which
|
|
|
* allows the test program to control whether they fail. For example, we can
|
|
|
@@ -3971,36 +4100,11 @@ smtp_attachment_clear_all(struct smtp *const smtp){
|
|
|
smtp->num_attachment = 0;
|
|
|
}
|
|
|
|
|
|
-/////////////////////////////////////////////////////
|
|
|
-#include <stdint.h>
|
|
|
-#include "common.h"
|
|
|
-#include "thread.h"
|
|
|
-#include "xlist.h"
|
|
|
-#include "elog.h"
|
|
|
-#include "lock.h"
|
|
|
-#include "sqlite_handle.h"
|
|
|
-
|
|
|
-
|
|
|
-//https://bgithub.xyz/embeddedmz/mailclient-cpp/tree/master/MAIL
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-#if 1
|
|
|
- #define LOGD log_d
|
|
|
- #define LOGE log_e
|
|
|
- #define LOGW log_w
|
|
|
-#else
|
|
|
- #define LOGD printf
|
|
|
- #define LOGE printf
|
|
|
- #define LOGW printf
|
|
|
-#endif
|
|
|
-
|
|
|
#define MAIL_CONNECTION_SECURITY SMTP_SECURITY_STARTTLS
|
|
|
#define MAIL_FLAGS (SMTP_DEBUG|SMTP_NO_CERT_VERIFY) /* Do not verify cert. */
|
|
|
#define MAIL_CAFILE NULL
|
|
|
#define MAIL_AUTH SMTP_AUTH_PLAIN
|
|
|
-static int send_mail(smtp_send_t *send, void *addr, char *sub, char *body, int multi)
|
|
|
+static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char *attach, int multi)
|
|
|
{
|
|
|
int i,r=-1,flag=0;
|
|
|
struct smtp *smtp=NULL;
|
|
|
@@ -4043,8 +4147,8 @@ static int send_mail(smtp_send_t *send, void *addr, char *sub, char *body, int m
|
|
|
}
|
|
|
|
|
|
if(multi) {
|
|
|
- smtp_recv_t *recv=(smtp_recv_t*)addr;
|
|
|
- for(i=0; i<SMTP_RECV_MAX; i++) {
|
|
|
+ mail_recv_t *recv=(mail_recv_t*)addr;
|
|
|
+ for(i=0; i<MAIL_RECV_MAX; i++) {
|
|
|
if(recv[i].use && strlen(recv[i].account)>0) {
|
|
|
r = smtp_address_add(smtp, SMTP_ADDRESS_TO, recv[i].account, recv[i].account);
|
|
|
if(r != SMTP_STATUS_OK) {
|
|
|
@@ -4074,7 +4178,7 @@ static int send_mail(smtp_send_t *send, void *addr, char *sub, char *body, int m
|
|
|
goto quit;
|
|
|
}
|
|
|
|
|
|
- //r = smtp_attachment_add_mem(smtp, "test.txt", "Test email attachment.", -1);
|
|
|
+ r = smtp_attachment_add_mem(smtp, attach, "attachment", -1);
|
|
|
smtp_mail(smtp, body);
|
|
|
}
|
|
|
|
|
|
@@ -4088,43 +4192,29 @@ out:
|
|
|
lock_s_release(LOCK_ID_SMTP);
|
|
|
return r;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-typedef struct {
|
|
|
- char sub[128];
|
|
|
- char body[2048];
|
|
|
-}smtp_pkt_t;
|
|
|
-typedef struct {
|
|
|
- handle_t list;
|
|
|
- smtp_info_t *info;
|
|
|
-
|
|
|
- GlobalDeviceManager *dm;
|
|
|
- int inited;
|
|
|
-}smtp_handle_t;
|
|
|
-static smtp_handle_t smHandle;
|
|
|
-
|
|
|
+#endif
|
|
|
+////////////////////////////////////////////////////////////////////////////
|
|
|
static void sleep_ms(int ms)
|
|
|
{
|
|
|
usleep(ms*1000);
|
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
-void* smtp_thread(void *arg)
|
|
|
+void* mail_thread(void *arg)
|
|
|
{
|
|
|
-#ifdef USE_SMTP
|
|
|
+#ifdef USE_MAIL
|
|
|
int r;
|
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
|
- smtp_handle_t *h=(smtp_handle_t*)th->arg;
|
|
|
+ mail_handle_t *h=(mail_handle_t*)th->arg;
|
|
|
list_node_t *ln=NULL;
|
|
|
- smtp_pkt_t *pkt=NULL;
|
|
|
- smtp_send_t *send=&h->info->send;
|
|
|
- smtp_recv_t *recv=h->info->recv;
|
|
|
+ mail_pkt_t *pkt=NULL;
|
|
|
+ mail_send_t *send=&h->info->send;
|
|
|
+ mail_recv_t *recv=h->info->recv;
|
|
|
|
|
|
while(th->quit==0) {
|
|
|
r = xlist_take_node(h->list, &ln, 0);
|
|
|
if(r==0) {
|
|
|
- pkt = (smtp_pkt_t*)ln->data.buf;
|
|
|
- send_mail(send, recv, pkt->sub, pkt->body, 1);
|
|
|
+ pkt = (mail_pkt_t*)ln->data.buf;
|
|
|
+ send_mail(send, recv, pkt->sub, pkt->body, pkt->attach, 1);
|
|
|
xlist_back_node(h->list, ln);
|
|
|
}
|
|
|
sleep_ms(100);
|
|
|
@@ -4135,11 +4225,11 @@ void* smtp_thread(void *arg)
|
|
|
}
|
|
|
|
|
|
|
|
|
-int smtp_init(void)
|
|
|
+int mail_init(void)
|
|
|
{
|
|
|
-#ifdef USE_SMTP
|
|
|
+#ifdef USE_MAIL
|
|
|
list_cfg_t lc;
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
GlobalDeviceManager *dm=&__globalDeviceManage;
|
|
|
|
|
|
lc.mode = LIST_FULL_FIFO;
|
|
|
@@ -4148,25 +4238,39 @@ int smtp_init(void)
|
|
|
h->list = xlist_init(&lc);
|
|
|
|
|
|
h->dm = dm;
|
|
|
- h->info = &h->dm->smtpInfo;
|
|
|
+ h->info = &h->dm->mailInfo;
|
|
|
|
|
|
- dev_smtp_init(dm->db, h->info);
|
|
|
- thread_start(THREAD_ID_SMTP, h);
|
|
|
+#ifdef USE_LIBCURL
|
|
|
+ if(!h->curl){
|
|
|
+ h->curl = curl_easy_init();
|
|
|
+ if(!h->curl) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ dev_mail_init(dm->db, h->info);
|
|
|
+ thread_start(THREAD_ID_MAIL, h);
|
|
|
h->inited = 1;
|
|
|
|
|
|
- //smtp_test();
|
|
|
+ //mail_test();
|
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-int smtp_deinit(void)
|
|
|
+int mail_deinit(void)
|
|
|
{
|
|
|
-#ifdef USE_SMTP
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+#ifdef USE_MAIL
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
|
|
|
- thread_stop(THREAD_ID_SMTP);
|
|
|
+#ifdef USE_LIBCURL
|
|
|
+ free(h->curl);
|
|
|
+ h->curl = 0;
|
|
|
+#endif
|
|
|
+
|
|
|
+ thread_stop(THREAD_ID_MAIL);
|
|
|
xlist_free(h->list);
|
|
|
h->inited = 0;
|
|
|
#endif
|
|
|
@@ -4175,16 +4279,17 @@ int smtp_deinit(void)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int post_data(char *sub, char *body)
|
|
|
+static int post_data(char *sub, char *body, char *attach)
|
|
|
{
|
|
|
int r=0;
|
|
|
|
|
|
-#ifdef USE_SMTP
|
|
|
- smtp_pkt_t pkt;
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+#ifdef USE_MAIL
|
|
|
+ mail_pkt_t pkt;
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
|
|
|
snprintf(pkt.sub, sizeof(pkt.sub), "%s", sub);
|
|
|
snprintf(pkt.body, sizeof(pkt.body), "%s", body);
|
|
|
+ snprintf(pkt.body, sizeof(pkt.body), "%s", body);
|
|
|
r = xlist_append(h->list, 0, &pkt, sizeof(pkt));
|
|
|
#endif
|
|
|
|
|
|
@@ -4192,47 +4297,47 @@ static int post_data(char *sub, char *body)
|
|
|
}
|
|
|
|
|
|
|
|
|
-int smtp_send(char *sub, char *body)
|
|
|
+int mail_send(char *sub, char *body, char *attachment)
|
|
|
{
|
|
|
int r=-1;
|
|
|
|
|
|
-#ifdef USE_SMTP
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+#ifdef USE_MAIL
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
|
|
|
if(!h->inited) {
|
|
|
return -1;
|
|
|
}
|
|
|
- r = send_mail(&h->info->send, h->info->recv, sub, body, 1);
|
|
|
+ r = send_mail(&h->info->send, h->info->recv, sub, body, attachment, 1);
|
|
|
#endif
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
-int smtp_post(char *sub, char *body)
|
|
|
+int mail_post(char *sub, char *body, char *attachment)
|
|
|
{
|
|
|
int r=-1;
|
|
|
|
|
|
-#ifdef USE_SMTP
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+#ifdef USE_MAIL
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
|
|
|
if(!h->inited) {
|
|
|
return -1;
|
|
|
}
|
|
|
- r = post_data(sub, body);
|
|
|
+ r = post_data(sub, body, attachment);
|
|
|
#endif
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
|
|
|
-int smtp_post_alarm(int type, int subtype, int alarm_type, char *content)
|
|
|
+int mail_post_alarm(int type, int subtype, int alarm_type, char *content)
|
|
|
{
|
|
|
int r=0;
|
|
|
|
|
|
-#ifdef USE_SMTP
|
|
|
- smtp_pkt_t pkt;
|
|
|
+#ifdef USE_MAIL
|
|
|
+ mail_pkt_t pkt;
|
|
|
char temp[128];
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
|
|
|
if(!h->inited) {
|
|
|
return -1;
|
|
|
@@ -4347,7 +4452,7 @@ int smtp_post_alarm(int type, int subtype, int alarm_type, char *content)
|
|
|
}
|
|
|
|
|
|
replace_sequences(content, h->dm->alarmTab);
|
|
|
- r = post_data(temp, content);
|
|
|
+ r = post_data(temp, content, NULL);
|
|
|
#endif
|
|
|
|
|
|
return r;
|
|
|
@@ -4359,11 +4464,11 @@ int smtp_test(void)
|
|
|
{
|
|
|
int r=0,cnt=0;
|
|
|
|
|
|
-#ifdef USE_SMTP
|
|
|
+#ifdef USE_MAIL
|
|
|
char temp[200];
|
|
|
- smtp_handle_t *h=&smHandle;
|
|
|
+ mail_handle_t *h=&maHandle;
|
|
|
|
|
|
- smtp_info_t info={
|
|
|
+ mail_info_t info={
|
|
|
.send = {
|
|
|
.id = 0,
|
|
|
.mode = 1,
|
|
|
@@ -4396,7 +4501,7 @@ int smtp_test(void)
|
|
|
char *body="hello everyone";
|
|
|
while(cnt++<5) {
|
|
|
sprintf(temp, "%s %d", body, cnt);
|
|
|
- r = smtp_send(sub, temp);
|
|
|
+ r = mail_send(sub, temp, NULL);
|
|
|
sleep(2);
|
|
|
}
|
|
|
#endif
|