瀏覽代碼

使用libcurl发邮件ok

guohui 1 年之前
父節點
當前提交
bf4d8c1add
共有 6 個文件被更改,包括 131 次插入 和 99 次删除
  1. 1 1
      pro/.vscode/launch.json
  2. 1 0
      pro/src/common.h
  3. 5 4
      pro/src/dflt.c
  4. 1 1
      pro/src/lock.h
  5. 108 85
      pro/src/mail.c
  6. 15 8
      pro/src/sqlite_handle.c

+ 1 - 1
pro/.vscode/launch.json

@@ -16,7 +16,7 @@
             "externalConsole": false,
             "MIMode": "gdb",
             "miDebuggerPath": "/home/tronlong/T113/T113-i_v1.0/out/t113_i/tlt113-evm-emmc/longan/buildroot/host/opt/ext-toolchain/bin/arm-linux-gnueabi-gdb",
-            "miDebuggerServerAddress": "192.168.234.132:1234"
+            "miDebuggerServerAddress": "192.168.0.132:1234"
         }
     ]
 }

+ 1 - 0
pro/src/common.h

@@ -681,6 +681,7 @@ typedef struct
     char                server[64]; 			        //服务器
     char                port[10];    		            //端口
     char                auth[16];    			        //认证方式  
+    char                ca[8196];                       //ca cert
 }mail_send_t;
 typedef struct
 {

+ 5 - 4
pro/src/dflt.c

@@ -50,11 +50,12 @@ GlobalDeviceInfo DFLT_PARAS={
 mail_send_t DFLT_MAIL_SEND={
 
     .mode = 1,
-    .server = "smtp.163.com",
-    .port = "25",
-    .account = "rcp064867@163.com",
-    .password = "YWKVURARCJXYXQVS",
+    .server = "smtp.gmail.com",
+    .port = "587",
+    .account = "jiuci4902@gmail.com",
+    .password = "wmfoedxaycdwllvs",
     .auth = "PLAIN",
+    .ca = "",
 };
 
 mqtt_info_t DFLT_MQTT={

+ 1 - 1
pro/src/lock.h

@@ -5,7 +5,7 @@
 enum {
     LOCK_ID_DB=0,
     LOCK_ID_MQTT,
-    LOCK_ID_SMTP,
+    LOCK_ID_MAIL,
     LOCK_ID_PARAS,
     LOCK_ID_SENSOR,
     LOCK_ID_BREAKER,

+ 108 - 85
pro/src/mail.c

@@ -7,9 +7,9 @@
 #include "elog.h"
 #include "lock.h"
 #include "sqlite_handle.h"
+#include "mail.h"
 
-
-//#define USE_LIBCURL
+#define USE_LIBCURL
 
 #if 1
     #define LOGD            log_d
@@ -22,10 +22,6 @@
 #endif
 
 
-#ifdef USE_LIBCURL
-#include <curl/curl.h>
-#endif
-
 typedef struct {
     char sub[128];
     char body[2048];
@@ -35,10 +31,6 @@ typedef struct {
     handle_t      list;
     mail_info_t   *info;
 
-#ifdef USE_LIBCURL
-    CURL          *curl;
-#endif
-
     GlobalDeviceManager *dm;
     int           inited;
 }mail_handle_t;
@@ -46,35 +38,33 @@ static mail_handle_t maHandle;
 
 
 #ifdef USE_LIBCURL
-
+#include <curl/curl.h>
 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;
+    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;
+    }
  
-  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)
 {
@@ -93,35 +83,62 @@ static void fill_send_buff(const char *to,const char *from,const char *subject,c
 }
 static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char *attach, int multi)
 {
-    CURLcode res = CURLE_OK;
+    int i=0;
+    CURLcode r=CURLE_OK;
+    char tmp[100];
     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;
+    curl_mime *mm=NULL;
     
+    lock_s_hold(LOCK_ID_MAIL);
+    if(!sub || !body || send->mode==0) {
+        goto out;
+    }
+
+    CURL *curl = curl_easy_init();
+    if(!curl) {
+        goto out;
+    }
+
     sprintf(url_buff,"%s:%s",send->server,send->port);
+    r = curl_easy_setopt(curl,CURLOPT_URL,url_buff);
+
+    //curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "EXPN");
+    //curl_easy_setopt(curl, CURLOPT_MAIL_AUTH, "<secret@cave>");
+    //curl_easy_setopt(curl, CURLOPT_SASL_AUTHZID, "ursel");
     
-    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);
+    //sprintf(tmp, "AUTH=%s", send->auth);
+    //curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, tmp);
     
-    //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);
+    r = curl_easy_setopt(curl, CURLOPT_USERNAME, send->account);        //setting user
+    r = curl_easy_setopt(curl, CURLOPT_PASSWORD, send->password);      //setting password
+
     
-    curl_easy_setopt(h->curl, CURLOPT_READFUNCTION, payload_source);
-    curl_easy_setopt(h->curl, CURLOPT_READDATA, &upload_ctx);
+    if(strcmp(send->port, "587")==0) { //tls
+        r = curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);    //setting openssl/tls
+        //curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
+    }
+
+    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
+    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
+    //curl_easy_setopt(curl, CURLOPT_USERAGENT, CLIENT_USERAGENT);
+
+    r = curl_easy_setopt(curl, CURLOPT_MAIL_FROM, send->account);
+    r = curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
+    r = curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
     
+    if(attach) {
+        //mm = curl_mime_init(curl);
+        if(mm) {
+            //curl_mime_addpart(mm);
+            //curl_mime_free(mm);
+        }
+    }
+
     if(multi) {
         for(int i=0; i<MAIL_RECV_MAX; i++) {
             if(recv[i].use && strlen(recv[i].account)>0) {
@@ -140,20 +157,24 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
     }
     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");
+    r = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+    r = curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
+    r = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+
+    r = curl_easy_perform(curl);
+    if(r != CURLE_OK) {
+        LOGE("send mail failed, %s\n", curl_easy_strerror(r));
+    }
+    else {
+        LOGD("send mail ok!\n");
     }
     curl_slist_free_all(recipients);
-    //curl_easy_cleanup(h->curl);
-    
-    return res;
+
+out:
+    if(curl) curl_easy_cleanup(curl);
+    lock_s_release(LOCK_ID_MAIL);
+
+    return (r==CURLE_OK)?0:-1;
 }
 
 #else
@@ -4100,10 +4121,7 @@ smtp_attachment_clear_all(struct smtp *const smtp){
   smtp->num_attachment = 0;
 }
 
-#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(mail_send_t *send, void *addr, char *sub, char *body, char *attach, int multi)
 {
     int i,r=-1,flag=0;
@@ -4112,7 +4130,7 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
     enum smtp_connection_security security=SMTP_SECURITY_STARTTLS;
     enum smtp_authentication_method auth=SMTP_AUTH_PLAIN;
 
-    lock_s_hold(LOCK_ID_SMTP);
+    lock_s_hold(LOCK_ID_MAIL);
     if(!sub || !body || send->mode==0) {
         goto out;
     }
@@ -4178,7 +4196,9 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
             goto quit;
         }
     
-        r = smtp_attachment_add_mem(smtp, attach, "attachment", -1);
+        if(attach) {
+            r = smtp_attachment_add_mem(smtp, attach, "attachment", -1);
+        }
         smtp_mail(smtp, body);
     }
     
@@ -4189,7 +4209,7 @@ quit:
     }
 
 out:
-    lock_s_release(LOCK_ID_SMTP);
+    lock_s_release(LOCK_ID_MAIL);
     return r;
 }
 #endif
@@ -4240,15 +4260,6 @@ int mail_init(void)
     h->dm = dm;
     h->info = &h->dm->mailInfo;
 
-#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;
@@ -4265,11 +4276,6 @@ int mail_deinit(void)
 #ifdef USE_MAIL
     mail_handle_t *h=&maHandle;
 
-#ifdef USE_LIBCURL
-    free(h->curl);
-    h->curl = 0;
-#endif
-
     thread_stop(THREAD_ID_MAIL);
     xlist_free(h->list);
     h->inited = 0;
@@ -4297,7 +4303,7 @@ static int post_data(char *sub, char *body, char *attach)
 }
 
 
-int mail_send(char *sub, char *body, char *attachment)
+int mail_send(char *sub, char *body, char *attach)
 {
     int r=-1;
 
@@ -4307,13 +4313,13 @@ int mail_send(char *sub, char *body, char *attachment)
     if(!h->inited) {
         return -1;
     }
-    r = send_mail(&h->info->send, h->info->recv, sub, body, attachment, 1);
+    r = send_mail(&h->info->send, h->info->recv, sub, body, attach, 1);
 #endif
 
     return r;
 }
 
-int mail_post(char *sub, char *body, char *attachment)
+int mail_post(char *sub, char *body, char *attach)
 {
     int r=-1;
 
@@ -4323,7 +4329,7 @@ int mail_post(char *sub, char *body, char *attachment)
     if(!h->inited) {
         return -1;
     }
-    r = post_data(sub, body, attachment);
+    r = post_data(sub, body, attach);
 #endif
 
     return r;
@@ -4460,7 +4466,7 @@ int mail_post_alarm(int type, int subtype, int alarm_type, char *content)
 
 
 
-int smtp_test(void)
+int mail_test(void)
 {
     int r=0,cnt=0;
     
@@ -4473,8 +4479,9 @@ int smtp_test(void)
       .id = 0,
       .mode = 1,
 
+#if 0
       .account = "rcp064867@163.com",
-      .password = "UODRIFWDFBTJTVLW",
+      .password = "YWKVURARCJXYXQVS",
       .server = "smtp.163.com",
 
       .port = "25",
@@ -4484,6 +4491,21 @@ int smtp_test(void)
       .auth = "PLAIN",
       //.auth = "LOGIN",
       //.auth = "CRAM-MD5",
+#else
+      .account = "jiuci4902@gmail.com",
+      .password = "wmfoedxaycdwllvs",
+      .server = "smtp.gmail.com",
+
+      //.port = "25",
+      //.port = "465",
+      .port = "587",
+
+      //.auth = "PLAIN",
+      .auth = "LOGIN",
+      //.auth = "CRAM-MD5",
+#endif
+
+
     },
 
       .recv = {
@@ -4493,6 +4515,7 @@ int smtp_test(void)
           {3, 1, "jiuci4902@gmail.com"},
           {4, 1, "cijiu0219@hotmail.com"},
           {5, 1, "cijiu990219@outlook.com"},
+          {6, 1, "iauther@163.com"},
       }
   };
   *h->info = info;

+ 15 - 8
pro/src/sqlite_handle.c

@@ -4246,7 +4246,8 @@ int dev_mail_init(sqlite3 *db, mail_info_t *info)
                             "password TEXT,"
                             "server TEXT,"
                             "port TEXT,"
-                            "auth TEXT);", table);
+                            "auth TEXT,"
+                            "ca TEXT);", table);
         r = sqlite3_exec(db, temp, 0, 0, &err_msg);
         if(r!=SQLITE_OK) {
             log_e("___ %s create failed, %s\n%s\n", table, temp, err_msg); sqlite3_free(err_msg);
@@ -4281,6 +4282,11 @@ int dev_mail_init(sqlite3 *db, mail_info_t *info)
         strcpy(send->server, pResult[ncol+4]);
         strcpy(send->port, pResult[ncol+5]);
         strcpy(send->auth, pResult[ncol+6]);
+
+        send->ca[0] = 0;
+        if(pResult[ncol+7] && (strlen(pResult[ncol+7])>1)) {
+            strcpy(send->ca, pResult[ncol+7]);
+        }
     }
     ////////////////////////////////////////////////////////////////////////
 
@@ -4325,18 +4331,18 @@ int dev_mail_update_send(sqlite3 *db, mail_send_t *send)
     int i,r=-1,exist=0,cnt=0;
 
 #ifdef USE_MAIL
-    char temp[1024];
+    char temp[9216];
     sqlite3_stmt *stmt;
     char *err_msg=NULL;
     char *table=MAIL_MANAGER_TABLE;
 
     cnt = get_count(db, table);
     if(cnt>0) {
-        sprintf(temp, "UPDATE %s SET id=%d, mode=%d, account='%s', password='%s', server='%s', port='%s', auth='%s' WHERE id=%d;", 
-                table, send->id, send->mode, send->account, send->password, send->server, send->port, send->auth, send->id);
+        sprintf(temp, "UPDATE %s SET id=%d,mode=%d,account='%s',password='%s',server='%s',port='%s',auth='%s',ca='%s' WHERE id=%d;", 
+                table, send->id,send->mode,send->account,send->password,send->server,send->port,send->auth,send->ca[0]?send->ca:"",send->id);
         r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
         if(r != SQLITE_OK) {
-            log_e("___sw_update_port failed, %s, %s\n\n", temp, err_msg);
+            log_e("___dev_mail_update_send failed, %s, %s\n\n", temp, err_msg);
             sqlite3_free(err_msg);
         }
         else {
@@ -4345,10 +4351,10 @@ int dev_mail_update_send(sqlite3 *db, mail_send_t *send)
     }
 
     if(exist==0) {
-        sprintf(temp, "INSERT INTO %s (id,mode,account,password,server,port,auth) VALUES (?,?,?,?,?,?,?);", table);
+        sprintf(temp, "INSERT INTO %s (id,mode,account,password,server,port,auth,ca) VALUES (?,?,?,?,?,?,?,?);", table);
         r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
         if (r != SQLITE_OK) {
-            log_e("___dev_mail_update_man, sqlite3_prepare_v2 failed, %s\n", sqlite3_errmsg(db));
+            log_e("___dev_mail_update_send, sqlite3_prepare_v2 failed, %s\n", sqlite3_errmsg(db));
             r = -1; goto quit;
         }
 
@@ -4359,12 +4365,13 @@ int dev_mail_update_send(sqlite3 *db, mail_send_t *send)
         sqlite3_bind_text(stmt, 5, send->server,   -1, NULL);
         sqlite3_bind_text(stmt, 6, send->port,     -1, NULL);
         sqlite3_bind_text(stmt, 7, send->auth,     -1, NULL);
+        sqlite3_bind_text(stmt, 8, send->ca,       -1, NULL);
 
         r = sqlite3_step(stmt);
         sqlite3_finalize(stmt);
 
         if (r != SQLITE_DONE) {
-            log_e("___sw_update_all, sqlite3_step failed\n");
+            log_e("___dev_mail_update_send, sqlite3_step failed\n");
             r = -1;
         }
         else {