|
|
@@ -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;
|