4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <sys/types.h>
31 #include <glib/gstdio.h>
37 #include "email-internal-types.h"
39 #include "email-core-global.h"
40 #include "email-core-utils.h"
41 #include "email-storage.h"
42 #include "email-core-smtp.h"
43 #include "email-core-event.h"
44 #include "email-core-mailbox.h"
45 #include "email-core-mail.h"
46 #include "email-core-mime.h"
47 #include "email-core-smime.h"
48 #include "email-core-account.h"
49 #include "email-core-imap-mailbox.h"
50 #include "email-core-mailbox-sync.h"
51 #include "email-core-signal.h"
52 #include "email-core-alarm.h"
53 #include "email-utilities.h"
54 #include "email-convert.h"
55 #include "email-debug-log.h"
56 #include "email-core-gmime.h"
57 #include "email-core-container.h"
63 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
64 static int emcore_get_report_mail_body(char *multi_user_name, ENVELOPE *envelope, BODY **multipart_body, int *err_code);
66 static int emcore_make_envelope_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope);
67 static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code);
68 char *emcore_generate_content_id_string(const char *hostname, int *err);
70 /* Functions from uw-imap-toolkit */
71 /* extern void *fs_get(size_t size); */
72 extern void rfc822_date(char *date);
73 extern long smtp_send(SENDSTREAM *stream, char *command, char *args);
74 extern long smtp_rcpt(SENDSTREAM *stream, ADDRESS *adr, long* error);
76 #ifndef __FEATURE_SEND_OPTMIZATION__
77 extern long smtp_soutr(void *stream, char *s);
80 #ifdef __FEATURE_SEND_OPTMIZATION__
81 extern long smtp_soutr_test(void *stream, char *s);
87 void mail_send_notify(email_send_status_t status, int total, int sent, int account_id, int mail_id, int err_code)
89 EM_DEBUG_FUNC_BEGIN("status[%d], total[%d], sent[%d], account_id[%d], mail_id[%d], err_code[%d]", status, total, sent, account_id, mail_id, err_code);
92 case EMAIL_SEND_CONNECTION_FAIL:
93 case EMAIL_SEND_FINISH:
97 case EMAIL_SEND_PROGRESS:
101 emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, total, sent, status, account_id, mail_id, -1, err_code);
105 /* ------ rfc822 handle ---------------------------------------------------*/
106 long buf_flush(void *stream, char *string)
108 EM_DEBUG_FUNC_BEGIN("stream[%p], string[%s]", stream, string);
114 #define RFC822_READ_BLOCK_SIZE 1024
115 #define RFC822_STRING_BUFFER_SIZE 1536
117 static char *emcore_find_img_tag(char *source_string)
119 EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
121 int cur = 0, string_length;
125 string_length = EM_SAFE_STRLEN(source_string);
127 for (cur = 0; cur < string_length; cur++) {
128 if (source_string[cur] == 'I' || source_string[cur] == 'i') {
130 if (source_string[cur] == 'M' || source_string[cur] == 'm') {
132 if (source_string[cur] == 'G' || source_string[cur] == 'g') {
133 EM_DEBUG_FUNC_END("%s", source_string + cur - 2);
134 return source_string + cur - 2;
139 EM_DEBUG_FUNC_END("Can't find");
143 #define CONTENT_ID_BUFFER_SIZE 512
144 static char *emcore_replace_inline_image_path_with_content_id(char *source_string, BODY *root_body, int *err_code)
146 EM_DEBUG_FUNC_BEGIN("source_string[%p], root_body[%p], err_code[%p]", source_string, root_body, err_code);
148 int err = EMAIL_ERROR_NONE;
149 char content_id_buffer[CONTENT_ID_BUFFER_SIZE] = {0,};
150 char file_name_buffer[512] = {0,};
151 char old_string[512] = {0,};
152 char new_string[512] = {0,};
153 char *result_string = NULL;
154 char *input_string = NULL;
155 BODY *cur_body = NULL;
156 PART *cur_part = NULL;
158 if (!source_string || !root_body) {
159 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM [%p] [%p]", source_string, root_body);
160 err = EMAIL_ERROR_INVALID_PARAM;
164 input_string = EM_SAFE_STRDUP(source_string);
166 cur_body = root_body;
167 if (root_body->type != TYPEMULTIPART) {
168 EM_DEBUG_LOG("The body is not multipart : type[%d]", root_body->type);
173 cur_part = cur_body->nested.part;
175 if (strcasecmp(cur_body->subtype, "RELATED") == 0) {
176 EM_DEBUG_LOG("Related part");
178 cur_body = &(cur_part->body);
180 if (cur_body->disposition.type && cur_body->disposition.type[0] == 'i') { /* Is inline content? */
181 EM_DEBUG_LOG("Has inline content");
182 memset(content_id_buffer, 0, CONTENT_ID_BUFFER_SIZE);
184 EM_SAFE_STRNCPY(content_id_buffer, cur_body->id + 1, CONTENT_ID_BUFFER_SIZE - 1); /* Removing <, > */
186 char *last_bracket = rindex(content_id_buffer, '>');
187 if (last_bracket) *last_bracket = NULL_CHAR;
189 /* if (emcore_get_attribute_value_of_body_part(cur_body->parameter, "name", file_name_buffer, CONTENT_ID_BUFFER_SIZE, false, &err)) { */
190 if (emcore_get_attribute_value_of_body_part(cur_body->parameter, "name", file_name_buffer, CONTENT_ID_BUFFER_SIZE, true, &err)) {
191 EM_DEBUG_LOG_SEC("Content-ID[%s], filename[%s]", content_id_buffer, file_name_buffer);
192 SNPRINTF(new_string, CONTENT_ID_BUFFER_SIZE, "\"cid:%s\"", content_id_buffer);
193 SNPRINTF(old_string, CONTENT_ID_BUFFER_SIZE, "\"%s\"", file_name_buffer);
194 result_string = em_replace_string(input_string, old_string, new_string);
200 EM_SAFE_FREE(input_string);
201 input_string = result_string;
202 result_string = NULL; /* prevent 34868 */
205 cur_part = cur_part->next;
210 cur_body = &(cur_part->body);
217 result_string = EM_SAFE_STRDUP(input_string);
220 EM_SAFE_FREE(input_string);
224 EM_DEBUG_FUNC_END("ret[%s]", result_string);
225 return result_string;
228 static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
230 EM_DEBUG_FUNC_BEGIN("fp[%d]", fp);
231 char *file_path = NULL;
232 char buf[RFC822_STRING_BUFFER_SIZE + 1] = {0};
233 char *img_tag_pos = NULL;
235 char *replaced_string = NULL;
239 int error = EMAIL_ERROR_NONE;
242 char *tmp_file_path = NULL;
243 FILE *fp_html = NULL;
244 FILE *fp_write = NULL;
245 char *full_buf = NULL;
247 file_path = body->sparep;
248 if (!file_path || EM_SAFE_STRLEN (file_path) == 0) {
249 EM_DEBUG_LOG("There is no file path");
250 switch (body->encoding) {
253 p = cpystr((const char *)body->contents.text.data);
254 len = body->contents.text.size;
259 EM_DEBUG_LOG("p[%s]", p);
260 fprintf(fp, "%s"CRLF_STRING CRLF_STRING, p);
264 EM_SAFE_FREE(body->sparep);
269 if (body->type == TYPETEXT && body->subtype && toupper(body->subtype[0]) == 'H') {
270 EM_DEBUG_LOG ("HTML Part");
273 unsigned int byte_read = 0;
275 if (stat (file_path, &st_buf) < 0) {
276 EM_DEBUG_EXCEPTION ("stat [%s] error [%d]", file_path, errno);
277 error = EMAIL_ERROR_FILE ;
281 error = em_fopen(file_path, "r", &fp_html);
282 if (error != EMAIL_ERROR_NONE) {
283 EM_DEBUG_EXCEPTION("em_fopen [%s] error [%d] ", file_path, error);
287 if (S_ISREG (st_buf.st_mode) && st_buf.st_size == 0) {
288 EM_DEBUG_LOG ("file_path[%s] is empty size", file_path);
289 error = EMAIL_ERROR_FILE ;
293 if (!(full_buf = (char*) em_malloc (sizeof (char) * (st_buf.st_size + 1)))) {
294 EM_DEBUG_EXCEPTION ("em_malloc failed");
295 error = EMAIL_ERROR_OUT_OF_MEMORY;
299 byte_read = fread (full_buf, sizeof(char), st_buf.st_size, fp_html);
301 if (byte_read <= 0) {
302 EM_SAFE_FREE (full_buf);
303 if (ferror (fp_html)) {
304 EM_DEBUG_EXCEPTION("fread [%s] error [%d]", file_path, errno);
305 error = EMAIL_ERROR_FILE ;
309 EM_DEBUG_LOG_DEV("==================================================");
310 EM_DEBUG_LOG_DEV("fread : %s", full_buf);
311 EM_DEBUG_LOG_DEV("==================================================");
313 img_tag_pos = emcore_find_img_tag(full_buf);
316 replaced_string = emcore_replace_inline_image_path_with_content_id(full_buf, root_body, &error);
317 if (replaced_string) {
318 EM_DEBUG_LOG("emcore_replace_inline_image_path_with_content_id succeeded");
319 EM_DEBUG_LOG_DEV("==================================================");
320 EM_DEBUG_LOG_DEV("replaced_string : %s", replaced_string);
321 EM_DEBUG_LOG_DEV("==================================================");
323 if (!emcore_get_temp_file_name (&tmp_file_path, &error)) {
324 EM_DEBUG_EXCEPTION (" em_core_get_temp_file_name failed[%d]", error);
327 EM_DEBUG_LOG_DEV("tmp file path : %s", tmp_file_path);
329 error = em_fopen(tmp_file_path, "w", &fp_write);
330 if (error != EMAIL_ERROR_NONE) {
331 EM_DEBUG_EXCEPTION_SEC ("em_fopen [%s] error [%d]", tmp_file_path, error);
335 if (fseek (fp_write, 0, SEEK_SET) < 0) {
336 EM_DEBUG_EXCEPTION("fseek failed : error [%d]", errno);
337 error = EMAIL_ERROR_FILE;
341 if (fprintf(fp_write, "%s", replaced_string) <= 0) {
342 EM_DEBUG_EXCEPTION("fprintf failed : error [%d]", errno);
343 error = EMAIL_ERROR_FILE;
347 file_path = tmp_file_path;
355 EM_DEBUG_LOG_DEV("Opening a file[%s]", file_path);
357 error = em_open(file_path, O_RDONLY, 0, &fd);
358 if (error != EMAIL_ERROR_NONE) {
359 EM_DEBUG_EXCEPTION_SEC("open[%s] error [%d]", file_path, error);
365 memset(&buf, 0x00, RFC822_STRING_BUFFER_SIZE + 1);
366 nread = read(fd, buf, (body->encoding == ENCBASE64 ? 57 : RFC822_READ_BLOCK_SIZE - 2));
369 EM_DEBUG_LOG_DEV("Can't read anymore : nread[%d]", nread);
371 } else if (nread < 0) {
372 EM_DEBUG_EXCEPTION ("read [%s] error [%d]", file_path, errno);
373 error = EMAIL_ERROR_FILE;
379 /* EM_DEBUG_LOG("body->encoding[%d]", body->encoding); */
380 switch (body->encoding) {
381 case ENCQUOTEDPRINTABLE:
382 p = (char *) rfc822_8bit ((unsigned char *)buf, (unsigned long)nread, (unsigned long *)&len);
385 tmp_p = g_base64_encode((guchar *)buf, (gsize)nread);
386 p = g_strconcat(tmp_p, "\r\n", NULL);
388 len = EM_SAFE_STRLEN(p);
392 //prepend additional dot to the string start with dot
393 if (p && p[0] == '.') {
396 p = g_strconcat(".", tmp_p, NULL);
400 if (!p && buf[0] == '.') {
401 p = g_strconcat(".", buf, NULL);
404 len = EM_SAFE_STRLEN((p?p:buf));
405 EM_DEBUG_LOG_DEV("line[%s]", (p?p:buf));
407 nwrite = fprintf (fp, "%s", (p?p:buf));
409 EM_DEBUG_EXCEPTION("fprintf error: nwrite[%d] len[%d] error[%d]", nwrite, len, errno);
410 error = EMAIL_ERROR_FILE ;
415 if (body->encoding == ENCQUOTEDPRINTABLE || body->encoding == ENCBASE64)
416 fprintf(fp, CRLF_STRING);
418 fprintf(fp, CRLF_STRING);
420 FINISH_OFF: /* prevent 34226 */
422 /* cleanup local vars */
423 EM_SAFE_FREE (body->sparep);
424 EM_SAFE_CLOSE (fd); /*prevent 34498*/
427 g_remove(tmp_file_path);
428 EM_SAFE_FREE(tmp_file_path);
433 EM_SAFE_FREE(full_buf);
434 EM_SAFE_FREE (replaced_string);
439 static int emcore_write_rfc822_body(BODY *body, BODY *root_body, FILE *fp, int *err_code)
441 EM_DEBUG_FUNC_BEGIN("body[%p], fp[%p], err_code[%p]", body, fp, err_code);
443 PARAMETER *param = NULL;
445 char *p = NULL, *bndry = NULL, buf[1025];
446 int error = EMAIL_ERROR_NONE;
448 switch (body->type) {
450 EM_DEBUG_LOG_DEV("body->type = TYPEMULTIPART");
452 part = body->nested.part;
454 for (param = body->parameter; param; param = param->next) {
455 if (strcasecmp(param->attribute, "BOUNDARY") == 0) {
456 bndry = param->value;
462 p = buf; p[0] = '\0';
464 rfc822_write_body_header(&p, &part->body);
466 fprintf(fp, "--%s"CRLF_STRING, bndry);
467 if (body->subtype && (body->subtype[0] == 'S' || body->subtype[0] == 's')) {
468 if ((error = emcore_write_body (body, root_body, fp)) != EMAIL_ERROR_NONE) {
469 EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
472 fprintf(fp, "--%s"CRLF_STRING, bndry);
475 fprintf(fp, "%s"CRLF_STRING, buf);
477 emcore_write_rfc822_body(&part->body, root_body, fp, err_code);
478 } while ((part = part->next));
480 fprintf(fp, "--%s--"CRLF_STRING, bndry);
484 EM_DEBUG_LOG_DEV("body->type is not TYPEMULTIPART");
486 if ((error = emcore_write_body (body, root_body, fp)) != EMAIL_ERROR_NONE) {
487 EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
497 static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t input_priority,
498 email_mail_report_t input_report_flag, char **data)
500 EM_DEBUG_FUNC_BEGIN("env[%p], body[%p], data[%p]", env, body, data);
502 int error = EMAIL_ERROR_NONE;
506 char *header_buffer = NULL;
507 size_t header_buffer_lenth = 0;
509 int address_count = 0;
510 ADDRESS *index = NULL;
513 EM_DEBUG_EXCEPTION("Invalid Parameters");
514 error = EMAIL_ERROR_INVALID_PARAM;
520 rfc822_encode_body_7bit(env, body); /* if contents.text.data isn't NULL, the data will be encoded. */
534 header_buffer_lenth = (env->subject ? EM_SAFE_STRLEN(env->subject) : 0);
535 header_buffer_lenth += address_count * MAX_EMAIL_ADDRESS_LENGTH;
536 header_buffer_lenth += 8192;
538 EM_DEBUG_LOG("header_buffer_lenth [%d]", header_buffer_lenth);
540 if (!(header_buffer = em_malloc(header_buffer_lenth))) {
541 EM_DEBUG_EXCEPTION(" malloc failed...");
542 error = EMAIL_ERROR_OUT_OF_MEMORY;
546 /* write at start of buffer */
547 buf.beg = buf.cur = header_buffer;
548 buf.end = header_buffer + header_buffer_lenth - 1;
552 /* rfc822_output_header(&buf, env, body, NIL, T); */ /* including BCC */
553 rfc822_output_header(&buf, env, body, NIL, NIL); /* Excluding BCC */
555 EM_DEBUG_LOG("header_buffer [%d]", strlen(header_buffer));
558 gchar **tokens = g_strsplit(header_buffer, "CHARSET=X-UNKNOWN", 2);
560 if (g_strv_length(tokens) > 1) {
564 charset = g_path_get_basename(body->sparep);
566 if (charset != NULL) {
567 if ((pHtml = strstr(charset, ".htm")) != NULL)
568 charset[pHtml-charset] = '\0';
571 SNPRINTF(header_buffer, header_buffer_lenth, "%sCHARSET=%s%s", tokens[0], charset, tokens[1]);
575 EM_DEBUG_EXCEPTION("body->sparep is NULL");
582 gchar **tokens = g_strsplit(header_buffer, "To: undisclosed recipients: ;\015\012", 2);
583 if (g_strv_length(tokens) > 1)
584 SNPRINTF(header_buffer, header_buffer_lenth, "%s%s", tokens[0], tokens[1]);
588 EM_DEBUG_LOG_DEV(" =============================================================================== "
589 LF_STRING"%s"LF_STRING
590 " =============================================================================== ", header_buffer);
592 if (EM_SAFE_STRLEN(header_buffer) > 2)
593 *(header_buffer + EM_SAFE_STRLEN(header_buffer) - 2) = '\0';
595 if (input_report_flag) {
596 char string_buf[512] = {0x00, };
598 if(input_report_flag & EMAIL_MAIL_REPORT_DSN) {
599 /* DSN (delivery status) */
600 /* change content-type */
601 /* Content-Type: multipart/report; */
602 /* report-type= delivery-status; */
603 /* boundary="----=_NextPart_000_004F_01C76EFF.54275C50" */
606 if(input_report_flag & EMAIL_MAIL_REPORT_MDN) {
607 /* MDN (read receipt) */
608 /* Content-Type: multipart/report; */
609 /* report-type= disposition-notification; */
610 /* boundary="----=_NextPart_000_004F_01C76EFF.54275C50" */
613 if(input_report_flag & EMAIL_MAIL_REQUEST_MDN) {
614 /* require read status */
615 rfc822_address(string_buf, env->from);
616 if (EM_SAFE_STRLEN(string_buf))
617 SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth - EM_SAFE_STRLEN(header_buffer), "Disposition-Notification-To: %s"CRLF_STRING, string_buf);
621 if (input_priority > 0) { /* priority (1:high 3:normal 5:low) */
622 SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-Priority: %d"CRLF_STRING, input_priority);
624 switch (input_priority) {
625 case EMAIL_MAIL_PRIORITY_HIGH:
626 SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-MSMail-Priority: High"CRLF_STRING);
628 case EMAIL_MAIL_PRIORITY_NORMAL:
629 SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-MSMail-Priority: Normal"CRLF_STRING);
631 case EMAIL_MAIL_PRIORITY_LOW:
632 SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-MSMail-Priority: Low"CRLF_STRING);
637 SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), CRLF_STRING);
639 if (data && EM_SAFE_STRLEN(*data) > 0) {
640 fname = EM_SAFE_STRDUP(*data);
644 if (!emcore_get_temp_file_name(&fname, &error)) {
645 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed[%d]", error);
650 error = em_fopen(fname, "w+", &fp);
651 if (error != EMAIL_ERROR_NONE) {
652 EM_DEBUG_EXCEPTION("em_fopen failed[%s] [%d]", fname, error);
656 fprintf(fp, "%s", header_buffer);
659 if (!emcore_write_rfc822_body(body, body, fp, &error)) {
660 EM_DEBUG_EXCEPTION("emcore_write_rfc822_body failed[%d]", error);
669 #ifdef USE_SYNC_LOG_FILE
670 emstorage_copy_file(fname, "/tmp/phone2pc.eml", false, NULL);
673 if (error == EMAIL_ERROR_NONE) {
675 *data = EM_SAFE_STRDUP(fname);
677 else if (fname != NULL) {
682 EM_SAFE_FREE(header_buffer);
688 INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas, int move_flag)
690 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list [%p], input_attachment_count [%d], input_meeting_request [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
692 /* IF an account is being deleted, there is no need to add the mail of the account */
694 extern key_t del_account_key; /* in emcore_delete_account */
695 int *del_account_id = NULL;
696 /* get the segment.*/
698 if ((shmid = shmget (del_account_key, sizeof (int), 0666)) != -1) {
699 /* attach the segment to current process space */
700 if ((del_account_id = (int*) shmat (shmid, NULL, 0)) != (int*) -1) {
701 /* compare two account ids */
702 EM_DEBUG_LOG ("del_id[%d] account_id[%d]",*del_account_id, input_mail_data->account_id);
703 if (*del_account_id == input_mail_data->account_id) {
704 EM_DEBUG_LOG ("SKIP adding mail: the account is being deleted");
705 return EMAIL_ERROR_ACCOUNT_NOT_FOUND;
709 EM_DEBUG_LOG ("read del_id, account_id [%d]", (del_account_id? *del_account_id:-100),input_mail_data->account_id);
712 int err = EMAIL_ERROR_NONE;
713 int attachment_id = 0, thread_id = -1, thread_item_count = 0, latest_mail_id_in_thread = -1;
714 int i = 0, rule_len, priority_sender = 0, blocked = 0, local_attachment_count = 0, local_inline_content_count = 0;
715 int mailbox_id_spam = 0, mailbox_id_target = 0;
716 int mail_smime_flag = 0;
717 char name_buf[MAX_PATH] = {0x00, };
718 char path_buf[MAX_PATH] = {0, };
719 char *body_text_file_name = NULL;
720 char *dl_error = NULL;
721 void *dl_handle = NULL;
722 int (*convert_mail_data_to_smime)(char*, emstorage_account_tbl_t*, email_mail_data_t*, email_attachment_data_t*, int, email_mail_data_t**, email_attachment_data_t**, int*);
724 int attachment_count = 0;
725 email_mail_data_t *mail_data = NULL;
726 email_attachment_data_t *attachment_data_list = NULL;
727 emstorage_mail_tbl_t *converted_mail_tbl = NULL;
728 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
729 emstorage_attachment_tbl_t attachment_tbl = { 0 };
730 emstorage_account_tbl_t *account_tbl_item = NULL;
731 emstorage_rule_tbl_t *rule = NULL;
732 struct stat st_buf = { 0 };
733 char mailbox_id_param_string[10] = {0,};
734 char errno_buf[ERRNO_BUF_SIZE] = {0};
735 int updated_thread_id = 0;
736 char *prefix_path = NULL;
737 char real_file_path[MAX_PATH] = {0};
739 #ifdef __FEATURE_BODY_SEARCH__
740 char *stripped_text = NULL;
743 /* Validating parameters */
744 if (!input_mail_data || !(input_mail_data->account_id) || !(input_mail_data->mailbox_id)) {
745 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
746 err = EMAIL_ERROR_INVALID_PARAM;
750 if ((err = emcore_is_storage_full()) == EMAIL_ERROR_MAIL_MEMORY_FULL) {
751 EM_DEBUG_EXCEPTION("Storage is full");
755 if (!emstorage_get_account_by_id(multi_user_name, input_mail_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_item, true, &err)) {
756 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed. account_id[%d] err[%d]", input_mail_data->account_id, err);
757 err = EMAIL_ERROR_INVALID_ACCOUNT;
761 if (input_from_eas == 0 && input_mail_data->smime_type && input_mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_DRAFT) {
762 dl_handle = dlopen("libemail-smime-api.so.1", RTLD_LAZY);
764 EM_DEBUG_EXCEPTION("Open failed : [%s]", dl_handle);
765 err = EMAIL_ERROR_INVALID_PATH;
770 convert_mail_data_to_smime = dlsym(dl_handle, "emcore_convert_mail_data_to_smime_data");
771 if ((dl_error = dlerror()) != NULL) {
772 EM_DEBUG_EXCEPTION("Symbol open failed [%s]", err);
773 err = EMAIL_ERROR_SYSTEM_FAILURE;
777 if ((err = convert_mail_data_to_smime(multi_user_name, account_tbl_item, input_mail_data, input_attachment_data_list, input_attachment_count, &mail_data, &attachment_data_list, &attachment_count)) != EMAIL_ERROR_NONE) {
778 EM_DEBUG_EXCEPTION("S/MIME failed");
784 mail_data = input_mail_data;
785 attachment_data_list = input_attachment_data_list;
786 attachment_count = input_attachment_count;
789 mailbox_id_target = mail_data->mailbox_id;
791 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
792 err = emcore_get_container_path(multi_user_name, &prefix_path);
793 if (err != EMAIL_ERROR_NONE) {
794 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
798 prefix_path = strdup("");
801 if (input_from_eas == 0 &&
802 !(input_mail_data->message_class & EMAIL_MESSAGE_CLASS_SMART_REPLY) &&
803 !(input_mail_data->message_class & EMAIL_MESSAGE_CLASS_SMART_FORWARD) ) {
804 if (mail_data->file_path_plain) {
805 memset(real_file_path, 0x00, sizeof(real_file_path));
806 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_plain);
808 if (stat(real_file_path, &st_buf) < 0) {
809 EM_DEBUG_EXCEPTION_SEC("mail_data->file_path_plain, stat(\"%s\") failed...", mail_data->file_path_plain);
810 EM_DEBUG_EXCEPTION("%s", EM_STRERROR(errno_buf));
811 err = EMAIL_ERROR_INVALID_MAIL;
816 if (mail_data->file_path_html) {
817 memset(real_file_path, 0x00, sizeof(real_file_path));
818 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_html);
820 if (stat(real_file_path, &st_buf) < 0) {
821 EM_DEBUG_EXCEPTION_SEC("mail_data->file_path_html, stat(\"%s\") failed...", mail_data->file_path_html);
822 EM_DEBUG_EXCEPTION("%s", EM_STRERROR(errno_buf) );
823 err = EMAIL_ERROR_INVALID_MAIL;
828 if (attachment_count && attachment_data_list) {
829 for (i = 0; i < attachment_count; i++) {
830 if (attachment_data_list[i].save_status) {
831 memset(real_file_path, 0x00, sizeof(real_file_path));
832 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment_data_list[i].attachment_path);
834 if (!attachment_data_list[i].attachment_path || stat(real_file_path, &st_buf) < 0) {
835 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", attachment_data_list[i].attachment_path);
836 err = EMAIL_ERROR_INVALID_ATTACHMENT;
843 if (!input_mail_data->full_address_from)
844 input_mail_data->full_address_from = EM_SAFE_STRDUP(account_tbl_item->user_email_address);
846 /* check for email_address validation */
847 if ((err = em_verify_email_address_of_mail_data (mail_data)) != EMAIL_ERROR_NONE) {
848 EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_data failed [%d]", err);
852 if (mail_data->report_status & EMAIL_MAIL_REPORT_MDN) {
853 /* check read-report mail */
854 if(!mail_data->full_address_to) { /* A report mail should have 'to' address */
855 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
856 err = EMAIL_ERROR_INVALID_PARAM;
859 /* Create report mail body */
861 if (!emcore_make_rfc822_file_from_mail(multi_user_name, mail_src, NULL, NULL, NULL, &err)) {
862 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
868 else { /* For Spam handling */
869 email_option_t *opt = &account_tbl_item->options;
870 EM_DEBUG_LOG_SEC("block_address [%d], block_subject [%d]", opt->block_address, opt->block_subject);
872 /* For eas moving from spambox to other mailbox */
873 if (mail_data->save_status != EMAIL_MAIL_STATUS_SAVED) {
874 if (opt->block_address || opt->block_subject) {
875 int is_completed = false;
878 if (!opt->block_address)
879 type = EMAIL_FILTER_SUBJECT;
880 else if (!opt->block_subject)
881 type = EMAIL_FILTER_FROM;
883 if (!emstorage_get_rule(multi_user_name, ALL_ACCOUNT, type, 0, &rule_len, &is_completed, &rule, true, &err) || !rule)
884 EM_DEBUG_LOG("No proper rules. emstorage_get_rule returns [%d]", err);
886 if (rule && !emcore_check_rule(mail_data->full_address_from, mail_data->subject, rule, rule_len, &priority_sender, &blocked, &err)) {
887 EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
891 mail_data->tag_id = PRIORITY_SENDER_TAG_ID;
893 if (blocked && (mail_data->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)) {
894 EM_DEBUG_LOG("mail[%d] added to spambox", mail_data->mail_id);
895 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail_data->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_id_spam, false, &err)) {
896 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
901 mailbox_id_target = mailbox_id_spam;
907 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id_target, (emstorage_mailbox_tbl_t**)&mailbox_tbl)) != EMAIL_ERROR_NONE) {
908 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
912 if (!emstorage_increase_mail_id(multi_user_name, &mail_data->mail_id, true, &err)) {
913 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
917 EM_DEBUG_LOG("mail_data->mail_size [%d]", mail_data->mail_size);
919 if(mail_data->mail_size == 0)
920 emcore_calc_mail_size(multi_user_name, mail_data, attachment_data_list, attachment_count, &(mail_data->mail_size)); /* Getting file size before file moved. */
922 EM_DEBUG_LOG("input_from_eas [%d] mail_data->body_download_status [%d]", input_from_eas, mail_data->body_download_status);
924 if (input_from_eas == 0|| mail_data->body_download_status) {
925 if (!emstorage_create_dir(multi_user_name, mail_data->account_id, mail_data->mail_id, 0, &err)) {
926 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
930 if (mail_data->file_path_plain) {
931 memset(name_buf, 0x00, sizeof(name_buf));
932 memset(path_buf, 0x00, sizeof(path_buf));
933 memset(real_file_path, 0x00, sizeof(real_file_path));
934 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_plain);
936 EM_DEBUG_LOG_SEC("mail_data->file_path_plain [%s]", mail_data->file_path_plain);
937 /* EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH); */
939 if ( (err = em_get_file_name_from_file_path(real_file_path, &body_text_file_name)) != EMAIL_ERROR_NONE) {
940 EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
941 err = EMAIL_ERROR_INVALID_FILE_PATH;
947 EM_SAFE_STRNCPY(body_text_file_name, UNKNOWN_CHARSET_PLAIN_TEXT_FILE, MAX_PATH);
949 EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH);
952 if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id,
953 0, body_text_file_name, name_buf, path_buf,
954 sizeof(path_buf), &err)) {
955 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
959 if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
960 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
963 if (!mail_data->body_download_status)
964 mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
966 EM_SAFE_FREE(mail_data->file_path_plain);
967 mail_data->file_path_plain = EM_SAFE_STRDUP(path_buf);
970 if (mail_data->file_path_html) {
971 memset(name_buf, 0x00, sizeof(name_buf));
972 memset(path_buf, 0x00, sizeof(path_buf));
973 memset(real_file_path, 0x00, sizeof(real_file_path));
974 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_html);
976 EM_DEBUG_LOG_SEC("mail_data->file_path_html [%s]", mail_data->file_path_html);
977 /* EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH); */
979 EM_SAFE_FREE(body_text_file_name);
981 if ( (err = em_get_file_name_from_file_path(real_file_path, &body_text_file_name)) != EMAIL_ERROR_NONE) {
982 EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
983 err = EMAIL_ERROR_INVALID_FILE_PATH;
988 EM_SAFE_STRNCPY(body_text_file_name, UNKNOWN_CHARSET_HTML_TEXT_FILE, MAX_PATH);
990 EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH);
993 if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id,
994 0, body_text_file_name, name_buf, path_buf,
995 sizeof(path_buf), &err)) {
996 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1000 if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
1001 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1005 if (!mail_data->body_download_status)
1006 mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
1008 EM_SAFE_FREE(mail_data->file_path_html);
1009 mail_data->file_path_html = EM_SAFE_STRDUP(path_buf);
1013 if (mail_data->file_path_mime_entity) {
1014 memset(name_buf, 0x00, sizeof(name_buf));
1015 memset(path_buf, 0x00, sizeof(path_buf));
1016 memset(real_file_path, 0x00, sizeof(real_file_path));
1017 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_mime_entity);
1019 EM_DEBUG_LOG_SEC("mail_data->file_path_mime_entity [%s]", mail_data->file_path_mime_entity);
1021 if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id,
1022 0, "mime_entity", name_buf, path_buf,
1023 sizeof(path_buf), &err)) {
1024 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1028 if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
1029 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1033 EM_SAFE_FREE(mail_data->file_path_mime_entity);
1034 mail_data->file_path_mime_entity = EM_SAFE_STRDUP(path_buf);
1037 if (!mail_data->date_time) {
1038 /* time isn't set */
1039 mail_data->date_time = time(NULL);
1042 /* Generate message_id */
1043 if (!input_from_eas) {
1044 mail_data->message_id = emcore_generate_content_id_string("org.tizen.email", NULL);
1045 mail_data->server_mail_id = strdup("0");
1048 mail_data->mailbox_id = mailbox_id_target;
1049 mail_data->mailbox_type = mailbox_tbl->mailbox_type;
1050 mail_data->server_mail_status = !input_from_eas;
1051 if(mail_data->save_status == EMAIL_MAIL_STATUS_NONE)
1052 mail_data->save_status = EMAIL_MAIL_STATUS_SAVED;
1054 /* Getting attachment count */
1055 for (i = 0; i < attachment_count; i++) {
1056 if (attachment_data_list[i].inline_content_status== 1)
1057 local_inline_content_count++;
1059 local_attachment_count++;
1062 mail_data->inline_content_count = local_inline_content_count;
1063 mail_data->attachment_count = local_attachment_count;
1065 EM_DEBUG_LOG("inline_content_count [%d]", local_inline_content_count);
1066 EM_DEBUG_LOG("attachment_count [%d]", local_attachment_count);
1068 EM_DEBUG_LOG("preview_text[%p]", mail_data->preview_text);
1069 if (mail_data->preview_text == NULL) {
1070 if ( (err = emcore_get_preview_text_from_file(multi_user_name,
1071 mail_data->file_path_plain,
1072 mail_data->file_path_html,
1073 MAX_PREVIEW_TEXT_LENGTH,
1074 &(mail_data->preview_text))) != EMAIL_ERROR_NONE) {
1075 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
1077 if (err != EMAIL_ERROR_EMPTY_FILE)
1082 if (!em_convert_mail_data_to_mail_tbl(mail_data, 1, &converted_mail_tbl, &err)) {
1083 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed [%d]", err);
1087 converted_mail_tbl->mailbox_id = mailbox_tbl->mailbox_id;
1089 /* Fill address information */
1090 emcore_fill_address_information_of_mail_tbl(multi_user_name, converted_mail_tbl);
1092 /* Fill thread id */
1093 if(mail_data->thread_id == 0) {
1094 if (emstorage_get_thread_id_of_thread_mails(multi_user_name, converted_mail_tbl, &thread_id, &latest_mail_id_in_thread, &thread_item_count) != EMAIL_ERROR_NONE)
1095 EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed");
1097 if (thread_id == -1) {
1098 converted_mail_tbl->thread_id = mail_data->mail_id;
1099 converted_mail_tbl->thread_item_count = thread_item_count = 1;
1102 converted_mail_tbl->thread_id = thread_id;
1103 thread_item_count++;
1107 thread_item_count = 2;
1110 mail_data->thread_id = converted_mail_tbl->thread_id;
1111 converted_mail_tbl->user_name = EM_SAFE_STRDUP(account_tbl_item->user_name);
1112 emstorage_begin_transaction(multi_user_name, NULL, NULL, NULL);
1114 /* insert mail to mail table */
1115 if (!emstorage_add_mail(multi_user_name, converted_mail_tbl, 0, false, &err)) {
1116 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
1117 /* ROLLBACK TRANSACTION; */
1118 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1122 /* Update thread information */
1123 EM_DEBUG_LOG("thread_item_count [%d]", thread_item_count);
1125 if (thread_item_count > 1) {
1126 if (!emstorage_update_latest_thread_mail(multi_user_name, mail_data->account_id, mail_data->mailbox_id, converted_mail_tbl->thread_id, &updated_thread_id, 0, 0, NOTI_THREAD_ID_CHANGED_BY_ADD, false, &err)) {
1127 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
1128 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1132 if (updated_thread_id > 0)
1133 input_mail_data->thread_id = updated_thread_id;
1136 /* Insert attachment information to DB */
1138 for (i = 0; i < attachment_count; i++) {
1139 memset(name_buf, 0x00, sizeof(name_buf));
1140 memset(path_buf, 0x00, sizeof(path_buf));
1142 /* set attachment size */
1143 memset(real_file_path, 0x00, sizeof(real_file_path));
1144 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment_data_list[i].attachment_path);
1146 if (attachment_data_list[i].attachment_size == 0) {
1147 if(attachment_data_list[i].attachment_path && stat(real_file_path, &st_buf) < 0)
1148 attachment_data_list[i].attachment_size = st_buf.st_size;
1151 if (!attachment_data_list[i].inline_content_status) {
1152 if (!emstorage_get_new_attachment_no(multi_user_name, &attachment_id, &err)) {
1153 EM_DEBUG_EXCEPTION("emstorage_get_new_attachment_no failed [%d]", err);
1154 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1159 if (!emstorage_create_dir(multi_user_name, mail_data->account_id, mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0 : attachment_id, &err)) {
1160 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1161 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1165 if (!emstorage_get_save_name(multi_user_name,
1166 mail_data->account_id,
1167 mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0 : attachment_id,
1168 attachment_data_list[i].attachment_name,
1173 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1174 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1177 /* if (input_from_eas == 0 || attachment_data_list[i].save_status) { */
1178 if (attachment_data_list[i].save_status) {
1179 if (attachment_data_list[i].attachment_mime_type && strcasestr(attachment_data_list[i].attachment_mime_type, "PKCS7")) {
1180 if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
1181 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1182 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1186 if (!emstorage_copy_file(real_file_path, name_buf, input_from_eas, &err)) {
1187 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
1188 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1194 memset(&attachment_tbl, 0, sizeof(emstorage_attachment_tbl_t));
1195 attachment_tbl.attachment_name = attachment_data_list[i].attachment_name;
1196 attachment_tbl.attachment_path = path_buf;
1197 attachment_tbl.attachment_size = attachment_data_list[i].attachment_size;
1198 attachment_tbl.mail_id = mail_data->mail_id;
1199 attachment_tbl.account_id = mail_data->account_id;
1200 attachment_tbl.mailbox_id = mail_data->mailbox_id;
1201 attachment_tbl.attachment_save_status = attachment_data_list[i].save_status;
1202 attachment_tbl.attachment_drm_type = attachment_data_list[i].drm_status;
1203 attachment_tbl.attachment_inline_content_status = attachment_data_list[i].inline_content_status;
1204 attachment_tbl.attachment_mime_type = attachment_data_list[i].attachment_mime_type;
1205 attachment_tbl.content_id = attachment_data_list[i].content_id;
1207 if (!emstorage_add_attachment(multi_user_name, &attachment_tbl, 0, false, &err)) {
1208 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
1209 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1213 if (!mail_smime_flag) {
1214 attachment_data_list[i].attachment_id = attachment_tbl.attachment_id;
1218 #ifdef __FEATURE_BODY_SEARCH__
1219 /* Insert mail_text to DB */
1220 if (!emcore_strip_mail_body_from_file(multi_user_name, converted_mail_tbl, &stripped_text, &err) || stripped_text == NULL) {
1221 EM_DEBUG_EXCEPTION("emcore_strip_mail_body_from_file failed [%d]", err);
1224 if (!emcore_add_mail_text(multi_user_name, mailbox_tbl, converted_mail_tbl, stripped_text, &err)) {
1225 EM_DEBUG_EXCEPTION("emcore_add_mail_text failed [%d]", err);
1226 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1231 /* Insert Meeting request to DB */
1232 if (mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1233 || mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1234 || mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1235 EM_DEBUG_LOG("This mail has the meeting request");
1236 input_meeting_request->mail_id = mail_data->mail_id;
1238 if (!emstorage_add_meeting_request(multi_user_name, mail_data->account_id, mailbox_tbl->mailbox_id, input_meeting_request, false, &err)) {
1239 EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
1240 emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
1245 emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL);
1247 if (emstorage_get_thread_id_of_thread_mails(multi_user_name, converted_mail_tbl, &thread_id, &latest_mail_id_in_thread, &thread_item_count) != EMAIL_ERROR_NONE)
1248 EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed.");
1250 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl->mailbox_id);
1251 if (!emcore_notify_storage_event(NOTI_MAIL_ADD, converted_mail_tbl->account_id, converted_mail_tbl->mail_id, mailbox_id_param_string, thread_id))
1252 EM_DEBUG_LOG("emcore_notify_storage_event [NOTI_MAIL_ADD] failed.");
1254 if (account_tbl_item->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC && !move_flag) {
1255 if (!emcore_remove_overflowed_mails(multi_user_name, mailbox_tbl, &err)) {
1256 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
1257 err = EMAIL_ERROR_NONE;
1259 EM_DEBUG_LOG("emcore_remove_overflowed_mails failed [%d]", err);
1263 if ( input_from_eas && (mail_data->flags_seen_field == 0)) {
1264 // && mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_TRASH
1265 // && mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX) {
1266 if ((err = emcore_update_sync_status_of_account(multi_user_name, mail_data->account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING | SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1267 EM_DEBUG_LOG("emcore_update_sync_status_of_account failed [%d]", err);
1274 EM_SAFE_FREE(body_text_file_name);
1275 EM_SAFE_FREE(prefix_path);
1277 #ifdef __FEATURE_BODY_SEARCH__
1278 EM_SAFE_FREE(stripped_text);
1281 if (mail_smime_flag)
1282 emcore_free_attachment_data(&attachment_data_list, attachment_count, NULL);
1284 if (account_tbl_item)
1285 emstorage_free_account(&account_tbl_item, 1, NULL);
1288 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1290 if (converted_mail_tbl)
1291 emstorage_free_mail(&converted_mail_tbl, 1, NULL);
1293 EM_DEBUG_FUNC_END();
1297 INTERNAL_FUNC int emcore_add_read_receipt(char *multi_user_name, int input_read_mail_id, int *output_receipt_mail_id)
1299 EM_DEBUG_FUNC_BEGIN("input_read_mail_id [%d], output_receipt_mail_id [%p]", input_read_mail_id, output_receipt_mail_id);
1300 int err = EMAIL_ERROR_NONE;
1301 int attachment_count = 0;
1302 ENVELOPE *envelope = NULL;
1303 email_mail_data_t *read_mail_data = NULL;
1304 email_mail_data_t *receipt_mail_data = NULL;
1305 emstorage_mail_tbl_t *receipt_mail_tbl_data = NULL;
1306 email_attachment_data_t *attachment_data = NULL;
1307 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1308 BODY *root_body = NULL;
1310 if( (err = emcore_get_mail_data(multi_user_name, input_read_mail_id, &read_mail_data)) != EMAIL_ERROR_NONE) {
1311 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
1315 receipt_mail_data = em_malloc(sizeof(email_mail_data_t));
1317 if (!receipt_mail_data) {
1318 EM_DEBUG_EXCEPTION("em_malloc failed...");
1319 err = EMAIL_ERROR_OUT_OF_MEMORY;
1323 memcpy(receipt_mail_data, read_mail_data, sizeof(email_mail_data_t));
1325 receipt_mail_data->full_address_to = EM_SAFE_STRDUP(read_mail_data->full_address_from);
1326 receipt_mail_data->message_id = EM_SAFE_STRDUP(read_mail_data->message_id);
1328 if (read_mail_data->subject) {
1329 receipt_mail_data->subject = em_malloc(EM_SAFE_STRLEN(read_mail_data->subject) + 7);
1330 if (!(receipt_mail_data->subject)) {
1331 EM_DEBUG_EXCEPTION("em_malloc failed...");
1332 err = EMAIL_ERROR_OUT_OF_MEMORY;
1336 SNPRINTF(receipt_mail_data->subject, EM_SAFE_STRLEN(read_mail_data->subject) + 7, "Read: %s", read_mail_data->subject);
1339 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, receipt_mail_data->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox_tbl, true, &err)) {
1340 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
1344 receipt_mail_data->mailbox_id = mailbox_tbl->mailbox_id;
1345 receipt_mail_data->mailbox_type = EMAIL_MAILBOX_TYPE_OUTBOX;
1346 receipt_mail_data->file_path_html = NULL;
1347 receipt_mail_data->flags_draft_field = 1;
1348 receipt_mail_data->body_download_status = 1;
1349 receipt_mail_data->save_status = (unsigned char)EMAIL_MAIL_STATUS_SENDING;
1350 receipt_mail_data->report_status = (unsigned char)EMAIL_MAIL_REPORT_MDN;
1352 if (!em_convert_mail_data_to_mail_tbl(receipt_mail_data, 1, &receipt_mail_tbl_data, &err)) {
1353 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed [%d]", err);
1357 if ( (err = emcore_make_envelope_from_mail(multi_user_name, receipt_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
1358 EM_DEBUG_EXCEPTION("emcore_make_envelope_from_mail failed [%d]", err);
1362 envelope->references = EM_SAFE_STRDUP(read_mail_data->message_id);
1364 if (!emcore_get_report_mail_body(multi_user_name, envelope, &root_body, &err)) {
1365 EM_DEBUG_EXCEPTION("emcore_get_report_mail_body failed [%d]", err);
1369 receipt_mail_data->file_path_plain = EM_SAFE_STRDUP(root_body->nested.part->body.sparep);
1371 /* Report attachment */
1372 /* Final-Recipient : rfc822;digipop@gmail.com
1373 Original-Message-ID: <r97a77ag0jdhkvvxke58u9i5.1345611508570@email.android.com>
1374 Disposition : manual-action/MDN-sent-manually; displayed */
1377 receipt_mail_data->attachment_count = 1;
1378 attachment_count = 1;
1380 attachment_data = em_malloc(sizeof(email_attachment_data_t));
1381 if (!attachment_data) {
1382 EM_DEBUG_EXCEPTION("em_malloc failed...");
1383 err = EMAIL_ERROR_OUT_OF_MEMORY;
1387 attachment_data->save_status = 1;
1388 attachment_data->attachment_path = EM_SAFE_STRDUP(root_body->nested.part->next->body.sparep);
1390 if (!emcore_get_file_name(attachment_data->attachment_path, &p, &err)) {
1391 EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", err);
1395 attachment_data->attachment_name = cpystr(p);
1398 if ( (err = emcore_add_mail(multi_user_name, receipt_mail_data, attachment_data, attachment_count, NULL, 0, false)) != EMAIL_ERROR_NONE) {
1399 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
1403 *output_receipt_mail_id = receipt_mail_data->mail_id;
1406 if(receipt_mail_data) {
1407 EM_SAFE_FREE(receipt_mail_data->full_address_to);
1408 EM_SAFE_FREE(receipt_mail_data->message_id);
1409 EM_SAFE_FREE(receipt_mail_data->subject);
1410 EM_SAFE_FREE(receipt_mail_data);
1414 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1417 emcore_free_mail_data_list(&read_mail_data, 1);
1420 emcore_free_attachment_data(&attachment_data, 1, NULL);
1422 if(receipt_mail_tbl_data)
1423 emstorage_free_mail(&receipt_mail_tbl_data, 1, NULL);
1426 mail_free_body(&root_body);
1428 EM_DEBUG_FUNC_END("err [%d]", err);
1432 INTERNAL_FUNC int emcore_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code)
1434 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], err_code[%p]", account_id, input_mailbox_id, meeting_req, err_code);
1436 int err = EMAIL_ERROR_NONE;
1438 if (!meeting_req || meeting_req->mail_id <= 0) {
1440 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
1442 err = EMAIL_ERROR_INVALID_PARAM;
1446 if (!emstorage_add_meeting_request(multi_user_name, account_id, input_mailbox_id, meeting_req, 1, &err)) {
1447 EM_DEBUG_EXCEPTION(" emstorage_add_meeting_request failed [%d]", err);
1458 EM_DEBUG_FUNC_END();
1462 INTERNAL_FUNC int emcore_query_mail_size_limit(char *multi_user_name, int account_id, int handle, int *err_code)
1464 EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
1466 int err = EMAIL_ERROR_NONE;
1467 SENDSTREAM *stream = NULL;
1468 void *tmp_stream = NULL;
1469 int mail_size_limit = -1;
1470 email_account_t *ref_account = NULL;
1471 sslstart_t stls = NULL;
1472 MAILSTREAM *mail_stream = NULL;
1474 if (account_id <= 0) {
1475 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1476 err = EMAIL_ERROR_INVALID_PARAM;
1480 if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id, false))) {
1481 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1482 err = EMAIL_ERROR_INVALID_ACCOUNT;
1486 /* if there is no security option, unset security. */
1487 if (!ref_account->outgoing_server_secure_connection) {
1488 stls = (sslstart_t)mail_parameters(NULL, GET_SSLSTART, NULL);
1489 mail_parameters(NULL, SET_SSLSTART, NULL);
1492 if (ref_account->pop_before_smtp != FALSE) {
1493 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&mail_stream, &err)) {
1494 EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
1499 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err)) {
1500 EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
1504 stream = (SENDSTREAM *)tmp_stream;
1506 if (stream && stream->protocol.esmtp.ok) {
1507 if (stream->protocol.esmtp.size.ok && stream->protocol.esmtp.size.limit > 0) {
1508 EM_DEBUG_LOG("Server size limit : %ld", stream->protocol.esmtp.size.limit);
1509 mail_size_limit = stream->protocol.esmtp.size.limit;
1518 emcore_free_account(ref_account);
1519 EM_SAFE_FREE(ref_account);
1522 #ifndef __FEATURE_KEEP_CONNECTION__
1525 #endif /* __FEATURE_KEEP_CONNECTION__ */
1528 mail_stream = mail_close (mail_stream);
1531 mail_parameters(NULL, SET_SSLSTART, (void *)stls);
1534 if (!emcore_notify_network_event(NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FINISH, account_id, NULL, mail_size_limit, handle))
1535 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FINISH] Failed");
1537 if (!emcore_notify_network_event(NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FAIL, account_id, NULL, handle, err))
1538 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FAIL] Failed");
1541 if (err_code != NULL)
1544 EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
1548 /* thread local variable for reuse */
1549 __thread GList* g_send_stream_list = NULL;
1552 SENDSTREAM **send_stream;
1553 } email_send_stream_list_t;
1556 stmp stream should be closed when threads exit, otherwise memory leaks
1558 INTERNAL_FUNC void emcore_close_smtp_stream_list ()
1560 EM_DEBUG_FUNC_BEGIN();
1561 GList* cur = g_send_stream_list;
1562 email_send_stream_list_t* data = NULL;
1566 if (data) *(data->send_stream) = smtp_close (*(data->send_stream));
1567 g_send_stream_list = g_list_delete_link (g_send_stream_list, cur);
1569 cur = g_send_stream_list;
1572 EM_DEBUG_FUNC_END();
1576 if threads exit after calling the function, emcore_close_smtp_stream_list should be called.
1577 Otherwise, memory leaks
1579 INTERNAL_FUNC SENDSTREAM** emcore_get_smtp_stream (char *multi_user_name, int account_id, int *error)
1581 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
1582 GList* cur = g_send_stream_list;
1583 email_send_stream_list_t* data = NULL;
1584 SENDSTREAM** ret = NULL;
1585 int err = EMAIL_ERROR_NONE;
1587 for ( ; cur ; cur = g_list_next(cur) ) {
1589 if (data->account_id == account_id) {
1590 if (data->send_stream == NULL || *(data->send_stream) == NULL) {
1591 EM_DEBUG_LOG ("smtp_stream was closed before");
1592 g_send_stream_list = g_list_delete_link (g_send_stream_list, cur);
1597 int reply = smtp_send ( *(data->send_stream), "NOOP", NULL);
1598 if (reply/100 == 2) { /* 2xx means a success */
1599 EM_DEBUG_LOG ("reusable smtp_stream found");
1600 return data->send_stream;
1603 EM_DEBUG_LOG ("smtp_stream is not reusable");
1604 *(data->send_stream) = smtp_close (*(data->send_stream));
1605 g_send_stream_list = g_list_delete_link (g_send_stream_list, cur);
1606 g_free (data->send_stream);
1612 ret = em_malloc (sizeof(SENDSTREAM*));
1614 EM_DEBUG_EXCEPTION("em_malloc error");
1618 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)ret, &err)) {
1619 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1625 email_send_stream_list_t *node = em_malloc (sizeof(email_send_stream_list_t));
1627 EM_DEBUG_EXCEPTION ("em_malloc error");
1628 *ret = smtp_close (*ret);
1633 node->account_id = account_id;
1634 node->send_stream = ret;
1636 g_send_stream_list = g_list_prepend (g_send_stream_list, node);
1643 EM_DEBUG_FUNC_END();
1651 3 threads call this function :
1652 worker_send_event_queue
1653 mainloop (by alarm),
1654 thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL,
1657 INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_code)
1659 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1660 EM_PROFILE_BEGIN(profile_emcore_send_mail);
1662 int err = EMAIL_ERROR_NONE, err2 = EMAIL_ERROR_NONE;
1663 int attachment_tbl_count = 0;
1665 SENDSTREAM** send_stream = NULL;
1666 ENVELOPE *envelope = NULL;
1667 sslstart_t stls = NULL;
1668 emstorage_mail_tbl_t *mail_tbl_data = NULL;
1669 emstorage_attachment_tbl_t *attachment_tbl_data = NULL;
1670 email_account_t *ref_account = NULL;
1671 email_option_t *opt = NULL;
1673 emstorage_mailbox_tbl_t* local_mailbox = NULL;
1674 int dst_mailbox_id = 0;
1675 int total_mail_size = 0;
1677 MAILSTREAM *mail_stream = NULL;
1680 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1681 err = EMAIL_ERROR_INVALID_PARAM;
1685 /* get mail to send */
1686 if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_tbl_data, false, &err) || err != EMAIL_ERROR_NONE) {
1687 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1691 account_id = mail_tbl_data->account_id;
1693 if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id, false))) {
1694 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1695 err = EMAIL_ERROR_INVALID_ACCOUNT;
1699 if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
1700 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
1704 if (!emcore_check_send_mail_thread_status()) {
1705 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1706 err = EMAIL_ERROR_CANCELLED;
1710 if ((!mail_tbl_data->full_address_to) && (!mail_tbl_data->full_address_cc) && (!mail_tbl_data->full_address_bcc)) {
1711 err = EMAIL_ERROR_NO_RECIPIENT;
1712 EM_DEBUG_EXCEPTION("No Recipient information [%d]", err);
1716 if ((err = em_verify_email_address_of_mail_tbl(mail_tbl_data)) != EMAIL_ERROR_NONE) {
1717 err = EMAIL_ERROR_INVALID_ADDRESS;
1718 EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed [%d]", err);
1723 if (!emcore_check_send_mail_thread_status()) {
1724 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1725 err = EMAIL_ERROR_CANCELLED;
1729 opt = &(ref_account->options);
1731 #ifdef __FEATURE_SUPPORT_VALIDATION_SYSTEM__
1733 EM_VALIDATION_SYSTEM_LOG("INFO", mail_id, "Email Send Start, %s -> %s, success", mail_tbl_data->full_address_from, mail_tbl_data->full_address_to);
1734 for (i = 0; i < attachment_tbl_count; i++) {
1735 if(attachment_tbl_data)
1736 EM_VALIDATION_SYSTEM_LOG("FILE", mail_id, "[%s], %d", attachment_tbl_data[i].attachment_path, attachment_tbl_data[i].attachment_size);
1738 #endif /* __FEATURE_SUPPORT_VALIDATION_SYSTEM__ */
1740 /*Update status flag to DB*/
1742 /* get rfc822 data */
1743 if (!emcore_make_rfc822_file_from_mail(multi_user_name, mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err)) {
1744 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
1748 if (!envelope || (!envelope->to && !envelope->cc && !envelope->bcc)) {
1749 EM_DEBUG_EXCEPTION(" no recipients found...");
1750 err = EMAIL_ERROR_NO_RECIPIENT;
1754 /* if there is no security option, unset security. */
1755 if (!ref_account->outgoing_server_secure_connection) {
1756 stls = (sslstart_t)mail_parameters(NULL, GET_SSLSTART, NULL);
1757 mail_parameters(NULL, SET_SSLSTART, NULL);
1760 if (!emcore_check_send_mail_thread_status()) {
1761 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1762 err = EMAIL_ERROR_CANCELLED;
1766 if (ref_account->pop_before_smtp != FALSE) {
1767 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&mail_stream, &err)) {
1768 EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
1769 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1770 err = EMAIL_ERROR_CANCELLED;
1775 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_DRAFT, &local_mailbox, false, &err)) {
1776 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1779 dst_mailbox_id = local_mailbox->mailbox_id;
1780 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1783 send_stream = emcore_get_smtp_stream (multi_user_name, account_id, &err);
1785 EM_DEBUG_EXCEPTION(" emcore_get_smtp_stream failed [%d]", err);
1786 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1787 err = EMAIL_ERROR_CANCELLED;
1792 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err)) {
1793 EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
1795 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1796 err = EMAIL_ERROR_CANCELLED;
1800 stream = (SENDSTREAM *)tmp_stream;
1803 if (!emcore_check_send_mail_thread_status()) {
1804 EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
1805 err = EMAIL_ERROR_CANCELLED;
1809 if (*send_stream && (*send_stream)->protocol.esmtp.ok) {
1810 if ((*send_stream)->protocol.esmtp.size.ok && (*send_stream)->protocol.esmtp.size.limit > 0) {
1811 EM_DEBUG_LOG("Server size limit : %ld", (*send_stream)->protocol.esmtp.size.limit);
1812 emcore_get_file_size(fpath, &total_mail_size, NULL);
1813 EM_DEBUG_LOG("mail size : %d", total_mail_size);
1814 if (total_mail_size > (*send_stream)->protocol.esmtp.size.limit) {
1815 err = EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE;
1821 /* set request of delivery status. */
1822 EM_DEBUG_LOG("opt->req_delivery_receipt [%d]", opt->req_delivery_receipt);
1823 EM_DEBUG_LOG("mail_tbl_data->report_status [%d]", mail_tbl_data->report_status);
1825 if (opt->req_delivery_receipt == EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON || (mail_tbl_data->report_status & EMAIL_MAIL_REQUEST_DSN)) {
1826 EM_DEBUG_LOG("DSN is required.");
1827 (*send_stream)->protocol.esmtp.dsn.want = 1;
1828 (*send_stream)->protocol.esmtp.dsn.full = 0;
1829 (*send_stream)->protocol.esmtp.dsn.notify.failure = 1;
1830 (*send_stream)->protocol.esmtp.dsn.notify.success = 1;
1833 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
1834 emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SENDING_MAIL, EMAIL_ERROR_NONE);
1836 /*Update status save_status to DB*/
1837 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
1838 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
1840 /* send mail to server. */
1841 if (!emcore_send_mail_smtp(multi_user_name, *send_stream, envelope, fpath, account_id, mail_id, &err)) {
1842 EM_DEBUG_EXCEPTION(" emcore_send_mail_smtp failed [%d]", err);
1843 if (err == SMTP_RESPONSE_EXCEED_SIZE_LIMIT)
1844 err = EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE;
1846 #ifndef __FEATURE_MOVE_TO_OUTBOX_FIRST__
1847 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, false, &err)) {
1848 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1851 dst_mailbox_id = local_mailbox->mailbox_id;
1852 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1854 /* unsent mail is moved to 'OUTBOX'. */
1855 if (!emcore_move_mail(multi_user_name, &mail_id, 1, dst_mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, NULL))
1856 EM_DEBUG_EXCEPTION(" emcore_mail_move falied...");
1859 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1863 emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, err);
1866 /* sent mail is moved to 'SENT' box or deleted. */
1867 if (opt->keep_local_copy) {
1868 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err)) {
1869 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1872 dst_mailbox_id = local_mailbox->mailbox_id;
1874 if (!emcore_move_mail(multi_user_name, &mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
1875 EM_DEBUG_EXCEPTION(" emcore_mail_move falied [%d]", err);
1876 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1877 #ifdef __FEATURE_LOCAL_ACTIVITY__
1878 else if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) /* To be synced to Sent box only IMAP not for POP */ {
1879 emstorage_activity_tbl_t new_activity;
1882 if (false == emcore_get_next_activity_id(&activityid, &err)) {
1883 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed [%d] ", err);
1886 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
1887 new_activity.activity_id = activityid;
1888 new_activity.server_mailid = NULL;
1889 new_activity.account_id = account_id;
1890 new_activity.mail_id = mail_id;
1891 new_activity.activity_type = ACTIVITY_SAVEMAIL;
1892 new_activity.dest_mbox = NULL;
1893 new_activity.src_mbox = NULL;
1895 if (!emcore_add_activity(&new_activity, &err)) {
1896 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed [%d] ", err);
1899 if (!emcore_move_mail_on_server(multi_user_name, dest_mbox.account_id, dst_mailbox_id, &mail_id, 1, dest_mbox.name, &err)) {
1900 EM_DEBUG_EXCEPTION(" emcore_move_mail_on_server falied [%d]", err);
1903 /* Remove ACTIVITY_SAVEMAIL activity */
1904 new_activity.activity_id = activityid;
1905 new_activity.activity_type = ACTIVITY_SAVEMAIL;
1906 new_activity.account_id = account_id;
1907 new_activity.mail_id = mail_id;
1908 new_activity.dest_mbox = NULL;
1909 new_activity.server_mailid = NULL;
1910 new_activity.src_mbox = NULL;
1912 if (!emcore_delete_activity(&new_activity, &err)) {
1913 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_SAVEMAIL] [%d] ", err);
1920 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1921 emstorage_mailbox_tbl_t* src_mailbox = NULL;
1922 // emstorage_mail_tbl_t *temp_mail = NULL;
1924 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &src_mailbox, true, &err)) {
1925 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1929 EM_DEBUG_LOG("local_yn:[%d]", src_mailbox->local_yn);
1930 if (src_mailbox->local_yn) {
1931 /* This is syncing operation in sent box
1932 but it slowed operation */
1934 void *local_stream = NULL;
1935 if (!emcore_sync_header (multi_user_name, local_mailbox, &local_stream, NULL, NULL, NULL, NULL, NULL, 0, -1, &err)) {
1936 EM_DEBUG_EXCEPTION("emcore_sync_header failed");
1938 mail_close (local_stream);
1939 if (!emstorage_get_maildata_by_servermailid(multi_user_name, local_mailbox->mailbox_id, "0", &temp_mail, false, &err)) {
1940 if (err != EMAIL_ERROR_MAIL_NOT_FOUND) {
1941 EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid failed : [%d]", err);
1947 emcore_sync_mail_from_client_to_server(multi_user_name, mail_id);
1948 emstorage_free_mail(&temp_mail, 1, NULL);
1952 /* sent box exception list : gmail (After the mail sent, moved the sent box) */
1953 if (ref_account->outgoing_server_address) {
1954 if (!strcasestr(ref_account->outgoing_server_address, "gmail"))
1955 emcore_sync_mail_from_client_to_server(multi_user_name, mail_id);
1959 if (!emcore_move_mail_on_server(multi_user_name, account_id, src_mailbox->mailbox_id, &mail_id, 1, local_mailbox->mailbox_name, &err)) {
1960 EM_DEBUG_EXCEPTION(" emcore_move_mail_on_server falied [%d]", err);
1964 emstorage_free_mailbox(&src_mailbox, 1, NULL);
1967 /* On Successful Mail sent remove the Draft flag */
1968 mail_tbl_data->flags_draft_field = 0;
1970 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "flags_draft_field", mail_tbl_data->flags_draft_field, true, &err))
1971 EM_DEBUG_EXCEPTION("Failed to modify extra flag [%d]", err);
1974 if (!emcore_delete_mail(multi_user_name, account_id, &mail_id, 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
1975 EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
1978 /* Set the phone log */
1979 if ((err = emcore_set_sent_contacts_log(multi_user_name, mail_tbl_data)) != EMAIL_ERROR_NONE) {
1980 EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
1983 /*Update save_status */
1984 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
1985 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
1986 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
1988 if (!emcore_delete_transaction_info_by_mailId(mail_id))
1989 EM_DEBUG_EXCEPTION(" emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
1994 if (ret == false && sent_flag == 0) {
1995 emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, err);
1998 if (ret == false && err != EMAIL_ERROR_INVALID_PARAM && mail_tbl_data) {
1999 if (err != EMAIL_ERROR_CANCELLED) {
2000 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
2001 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
2002 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
2005 if (EMAIL_MAIL_STATUS_SEND_CANCELED == mail_tbl_data->save_status)
2006 EM_DEBUG_LOG("EMAIL_MAIL_STATUS_SEND_CANCELED Already set for ");
2008 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
2009 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
2010 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
2016 emcore_free_account(ref_account);
2017 EM_SAFE_FREE(ref_account);
2021 #ifndef __FEATURE_KEEP_CONNECTION__
2024 #endif /* __FEATURE_KEEP_CONNECTION__ */
2028 mail_stream = mail_close (mail_stream);
2031 mail_parameters(NULL, SET_SSLSTART, (void *)stls);
2033 if (attachment_tbl_data)
2034 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
2037 mail_free_envelope(&envelope);
2040 EM_DEBUG_LOG_SEC("REMOVE TEMP FILE : %s", fpath);
2042 EM_SAFE_FREE (fpath);
2046 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2049 if (!emcore_notify_network_event(NOTI_SEND_FINISH, account_id, NULL, mail_id, 0))
2050 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEND_FINISH] Failed");
2051 #ifdef __FEATURE_SUPPORT_VALIDATION_SYSTEM__
2053 EM_VALIDATION_SYSTEM_LOG("INFO", mail_id, "Email Send End, %s -> %s, success", mail_tbl_data->full_address_from, mail_tbl_data->full_address_to);
2057 if (!emcore_notify_network_event(NOTI_SEND_FAIL, account_id, NULL, mail_id, err))
2058 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEND_FAIL] Failed");
2059 #ifdef __FEATURE_SUPPORT_VALIDATION_SYSTEM__
2061 EM_VALIDATION_SYSTEM_LOG("INFO", mail_id, "Email Send End, %s -> %s, failed", mail_tbl_data->full_address_from, mail_tbl_data->full_address_to);
2064 if (err != EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE) {
2065 /* Add alarm for next sending mails */
2066 if( (err2 = emcore_create_alarm_for_auto_resend(multi_user_name, AUTO_RESEND_INTERVAL)) != EMAIL_ERROR_NONE) {
2067 if (err2 != EMAIL_ERROR_MAIL_NOT_FOUND)
2068 EM_DEBUG_EXCEPTION("emcore_create_alarm_for_auto_resend failed [%d]", err2);
2074 emstorage_free_mail(&mail_tbl_data, 1, NULL);
2076 if (err_code != NULL)
2078 EM_PROFILE_END(profile_emcore_send_mail);
2079 EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
2083 /* send a saved all mails */
2084 INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id, char *input_mailbox_name, int *err_code)
2086 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_name[%p], err_code[%p]", account_id, input_mailbox_name, err_code);
2089 int err = EMAIL_ERROR_NONE;
2090 int err2 = EMAIL_ERROR_NONE;
2091 int status = EMAIL_SEND_FAIL;
2092 int *mail_ids = NULL;
2096 int attachment_tbl_count = 0;
2098 SENDSTREAM *stream = NULL;
2099 ENVELOPE *envelope = NULL;
2100 email_account_t *ref_account = NULL;
2101 emstorage_mail_tbl_t *searched_mail_tbl_data = NULL;
2102 emstorage_attachment_tbl_t *attachment_tbl_data = NULL;
2103 email_option_t *opt = NULL;
2104 sslstart_t stls = NULL;
2105 void *tmp_stream = NULL;
2106 emstorage_mailbox_tbl_t* local_mailbox = NULL;
2107 int dst_mailbox_id = 0;
2109 if (!account_id || !input_mailbox_name) {
2110 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2111 err = EMAIL_ERROR_INVALID_PARAM;
2116 if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id, false))) {
2117 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2118 err = EMAIL_ERROR_INVALID_ACCOUNT;
2122 /*don't delete the comment. several threads including event thread call this func */
2123 /* FINISH_OFF_IF_CANCELED; */
2125 opt = &(ref_account->options);
2127 if (!emstorage_get_mailbox_by_name(multi_user_name, account_id, -1, input_mailbox_name, &local_mailbox, false, &err)) {
2128 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed : [%d]", err);
2133 if (!emstorage_mail_search_start(multi_user_name, NULL, account_id, local_mailbox->mailbox_id, 0, &handle, &total, true, &err)) {
2134 EM_DEBUG_EXCEPTION("emstorage_mail_search_start failed [%d]", err);
2138 mail_ids = em_malloc(sizeof(int) * total);
2139 if (mail_ids == NULL) {
2140 EM_DEBUG_EXCEPTION("malloc failed...");
2141 err = EMAIL_ERROR_OUT_OF_MEMORY;
2145 for (i = 0; i < total ; i++) {
2146 if (!emstorage_mail_search_result(handle, RETRIEVE_ID, (void **)&mail_ids[i], true, &err)) {
2147 EM_DEBUG_EXCEPTION("emstorage_mail_search_result failed [%d]", err);
2152 if (!emstorage_mail_search_end(handle, true, &err)) {
2153 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
2159 mail_send_notify(EMAIL_SEND_PREPARE, 0, 0, account_id, mail_ids[total], err);
2162 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2164 for (i = 0; i < total; i++) {
2165 /*don't delete the comment. several threads including event thread call this func */
2166 /* FINISH_OFF_IF_CANCELED;*/
2168 if (!emstorage_get_mail_by_id(multi_user_name, mail_ids[i], &searched_mail_tbl_data, false, &err)) {
2169 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2173 /* Skip the mail canceled */
2174 if (searched_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED) {
2175 EM_DEBUG_EXCEPTION("The mail was canceled. [%d]", mail_ids[i]);
2176 emstorage_free_mail(&searched_mail_tbl_data, 1, &err);
2177 searched_mail_tbl_data = NULL;
2181 if ( (err = emstorage_get_attachment_list(multi_user_name, mail_ids[i], false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
2182 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2186 /* check for email_address validation */
2187 if ( (err = em_verify_email_address_of_mail_tbl(searched_mail_tbl_data)) != EMAIL_ERROR_NONE ) {
2188 err = EMAIL_ERROR_INVALID_ADDRESS;
2189 EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed [%d]", err);
2193 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
2195 if (!emcore_make_rfc822_file_from_mail(multi_user_name, searched_mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err)) {
2196 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail falied [%d]", err);
2200 /*don't delete the comment. several threads including event thread call this func */
2201 /* FINISH_OFF_IF_CANCELED;*/
2203 /* connect mail server. */
2205 /* if there no security option, unset security. */
2206 if (!ref_account->outgoing_server_secure_connection) {
2207 stls = (sslstart_t)mail_parameters(NULL, GET_SSLSTART, NULL);
2208 mail_parameters(NULL, SET_SSLSTART, NULL);
2212 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, &tmp_stream, &err) || !tmp_stream) {
2213 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2215 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
2216 err = EMAIL_ERROR_CANCELLED;
2218 status = EMAIL_SEND_CONNECTION_FAIL;
2222 stream = (SENDSTREAM *)tmp_stream;
2224 /*don't delete the comment. several threads including event thread call this func */
2225 /* FINISH_OFF_IF_CANCELED;*/
2227 mail_send_notify(EMAIL_SEND_CONNECTION_SUCCEED, 0, 0, account_id, mail_ids[i], err);
2229 /* reqest of delivery status. */
2230 if (opt && opt->req_delivery_receipt == EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON) {
2231 stream->protocol.esmtp.dsn.want = 1;
2232 stream->protocol.esmtp.dsn.full = 0;
2233 stream->protocol.esmtp.dsn.notify.failure = 1;
2234 stream->protocol.esmtp.dsn.notify.success = 1;
2237 mail_send_notify(EMAIL_SEND_START, 0, 0, account_id, mail_ids[i], err);
2240 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
2242 /* update mail status to sending. */
2243 if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err)) {
2244 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
2249 if (!emcore_send_mail_smtp(multi_user_name, stream, envelope, fpath, account_id, mail_ids[i], &err)) {
2250 EM_DEBUG_EXCEPTION("emcore_send_mail_smtp failed [%d]", err);
2251 if (err == SMTP_RESPONSE_EXCEED_SIZE_LIMIT) err = EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE;
2253 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
2255 /* update mail status to failure. */
2256 if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err2))
2257 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
2259 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err)) {
2260 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
2263 dst_mailbox_id = local_mailbox->mailbox_id;
2265 emcore_move_mail(multi_user_name, &mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, NULL);
2268 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2273 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
2275 /* update mail status to sent mail. */
2276 if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err)) {
2277 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
2281 /* sent mail is moved to 'SENT' box or deleted. */
2282 if (opt->keep_local_copy) {
2283 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err)) {
2284 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
2287 dst_mailbox_id = local_mailbox->mailbox_id;
2289 if (!emcore_move_mail(multi_user_name, &mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
2290 EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
2293 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2296 if (!emcore_delete_mail(multi_user_name, account_id, &mail_ids[i], 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
2297 EM_DEBUG_EXCEPTION("emcore_delete_mail falied [%d]", err);
2300 /* Set the phone log */
2301 if ((err = emcore_set_sent_contacts_log(multi_user_name, searched_mail_tbl_data)) != EMAIL_ERROR_NONE) {
2302 EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
2305 if(searched_mail_tbl_data) {
2306 emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
2307 searched_mail_tbl_data = NULL;
2310 if(attachment_tbl_data)
2311 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
2313 mail_free_envelope(&envelope); envelope = NULL;
2317 EM_SAFE_FREE(fpath);
2327 emcore_free_account(ref_account);
2328 EM_SAFE_FREE(ref_account);
2335 mail_parameters(NIL, SET_SSLSTART, (void *)stls);
2338 mail_free_envelope(&envelope);
2341 if (!emstorage_mail_search_end(handle, true, &err2))
2342 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err2);
2345 if (searched_mail_tbl_data)
2346 emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
2348 if(attachment_tbl_data)
2349 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
2353 EM_SAFE_FREE(fpath);
2357 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2360 mail_send_notify(EMAIL_SEND_FINISH, 0, 0, account_id, mail_ids[total], err);
2361 emcore_show_user_message(multi_user_name, account_id, EMAIL_ACTION_SEND_MAIL, err);
2363 if(mail_ids) /* prevent 34385 */
2364 mail_send_notify(status, 0, 0, account_id, mail_ids[total], err);
2365 emcore_show_user_message(multi_user_name, account_id, EMAIL_ACTION_SEND_MAIL, err);
2368 EM_SAFE_FREE(mail_ids);
2370 if (err_code != NULL)
2376 static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code)
2378 EM_DEBUG_FUNC_BEGIN_SEC("stream[%p], env[%p], data_file[%s], account_id[%d], mail_id[%d], err_code[%p]", stream, env, data_file, account_id, mail_id, err_code);
2379 EM_PROFILE_BEGIN(profile_emcore_send_mail_smtp);
2382 int err = EMAIL_ERROR_NONE;
2384 long total = 0, sent = 0, send_ret = 0, send_err = 0, sent_percent = 0, last_sent_percent = 0;
2385 char buf[2048] = { 0, };
2386 email_account_t *ref_account = NULL;
2389 if (!env || !env->from || (!env->to && !env->cc && !env->bcc)) {
2391 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2392 err = EMAIL_ERROR_INVALID_PARAM;
2396 if (!env->from->mailbox || !env->from->host) {
2397 EM_DEBUG_EXCEPTION("env->from->mailbox[%p], env->from->host[%p]", env->from->mailbox, env->from->host);
2398 err = EMAIL_ERROR_INVALID_PARAM;
2402 if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id, false))) {
2403 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2404 err = EMAIL_ERROR_INVALID_ACCOUNT;
2408 EM_DEBUG_LOG("Modifying - MAIL FROM ");
2409 if (ref_account->user_email_address == NULL) {
2410 EM_DEBUG_LOG("ref_account->user_email_address is null!!");
2411 SNPRINTF(buf, sizeof(buf), "FROM:<%s@%s>", env->from->mailbox, env->from->host);
2414 SNPRINTF(buf, sizeof(buf), "FROM:<%s>", ref_account->user_email_address);
2416 /* set DSN for ESMTP */
2417 if (stream->protocol.esmtp.ok) {
2418 if (stream->protocol.esmtp.eightbit.ok && stream->protocol.esmtp.eightbit.want)
2419 strncat (buf, " BODY=8BITMIME", sizeof(buf)-(EM_SAFE_STRLEN(buf)+1));
2421 EM_DEBUG_LOG("stream->protocol.esmtp.dsn.ok [%d]", stream->protocol.esmtp.dsn.ok);
2423 if (stream->protocol.esmtp.dsn.ok && stream->protocol.esmtp.dsn.want) {
2424 EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is required");
2425 strncat (buf, stream->protocol.esmtp.dsn.full ? " RET=FULL" : " RET=HDRS", sizeof(buf)-EM_SAFE_STRLEN(buf)-1);
2426 if (stream->protocol.esmtp.dsn.envid)
2427 SNPRINTF (buf + EM_SAFE_STRLEN (buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)), " ENVID=%.100s", stream->protocol.esmtp.dsn.envid);
2430 EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is not required or DSN is not supported");
2433 EM_PROFILE_BEGIN(profile_prepare_and_head);
2434 send_ret = smtp_send(stream, "RSET", 0);
2435 EM_DEBUG_LOG("[SMTP] RSET --------> %s", stream->reply);
2437 if (send_ret != SMTP_RESPONSE_OK) {
2442 send_ret = smtp_send(stream, "MAIL", buf);
2443 EM_DEBUG_LOG("[SMTP] MAIL %s --------> %s", buf, stream->reply);
2446 case SMTP_RESPONSE_OK:
2449 case SMTP_RESPONSE_WANT_AUTH :
2450 case SMTP_RESPONSE_WANT_AUTH2:
2451 EM_DEBUG_EXCEPTION("SMTP error : authentication required...");
2452 err = EMAIL_ERROR_AUTH_REQUIRED;
2455 case SMTP_RESPONSE_UNAVAIL:
2456 EM_DEBUG_EXCEPTION("SMTP error : sending unavailable...");
2457 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2459 case SMTP_RESPONSE_CONNECTION_BROKEN:
2460 EM_DEBUG_EXCEPTION("SMTP error : SMTP connection broken...");
2461 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2464 EM_DEBUG_EXCEPTION("SMTP error : sending unavailable...");
2465 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2470 send_ret = smtp_rcpt(stream, env->to, &send_err);
2471 EM_DEBUG_LOG_SEC("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->to->mailbox, env->to->host, env->to->error ? env->to->error : stream->reply);
2473 err = stream->replycode;
2482 send_ret = smtp_rcpt(stream, env->cc, &send_err);
2483 EM_DEBUG_LOG_SEC("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->cc->mailbox, env->cc->host, env->cc->error ? env->cc->error : stream->reply);
2485 err = stream->replycode;
2494 send_ret = smtp_rcpt(stream, env->bcc, &send_err);
2495 EM_DEBUG_LOG_SEC("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->bcc->mailbox, env->bcc->host, env->bcc->error ? env->bcc->error : stream->reply);
2497 err = stream->replycode;
2507 EM_DEBUG_EXCEPTION("One or more recipients failed...");
2508 err = EMAIL_ERROR_INVALID_ADDRESS;
2512 EM_DEBUG_EXCEPTION("No valid recipients...");
2514 switch (stream->replycode) {
2515 case SMTP_RESPONSE_UNAVAIL:
2516 case SMTP_RESPONSE_WANT_AUTH :
2517 case SMTP_RESPONSE_WANT_AUTH2:
2518 err = EMAIL_ERROR_AUTH_REQUIRED;
2522 err = EMAIL_ERROR_INVALID_ADDRESS;
2528 send_ret = smtp_send(stream, "DATA", 0);
2529 EM_DEBUG_LOG_SEC("[SMTP] DATA --------> %s", stream->reply);
2530 EM_PROFILE_END(profile_prepare_and_head);
2532 if (send_ret != SMTP_RESPONSE_READY) {
2538 EM_PROFILE_BEGIN(profile_open_file);
2540 err = em_fopen(data_file, "r+", &fp);
2541 if (err != EMAIL_ERROR_NONE) {
2542 EM_DEBUG_EXCEPTION("em_fopen(\"%s\") failed..., error:[%d]", data_file, err);
2545 EM_PROFILE_END(profile_open_file);
2548 #ifdef __FEATURE_SEND_OPTMIZATION__
2551 int read_size, allocSize, dataSize, gMaxAllocSize = 40960; /* 40KB */
2552 int total_fixed = 0;
2553 fseek(fp, 0, SEEK_END);
2555 total_fixed = total;
2556 fseek(fp, 0, SEEK_SET);
2557 EM_DEBUG_LOG("total size [%d]", total);
2559 if (total < gMaxAllocSize)
2560 allocSize = total + 1;
2562 allocSize = gMaxAllocSize;
2564 EM_PROFILE_BEGIN(profile_allocation);
2565 /* Allocate a buffer of max 2MB to read from file */
2566 data = (char *)em_malloc(allocSize);
2568 EM_PROFILE_END(profile_allocation);
2571 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2577 /* Cancel the sending event */
2578 if (!emcore_check_send_mail_thread_status()) {
2579 EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
2580 err = EMAIL_ERROR_CANCELLED;
2584 if (total < allocSize)
2587 dataSize = allocSize;
2589 memset(data, 0x0, dataSize+1);
2590 read_size = fread(data, sizeof (char), dataSize, fp);
2592 if (read_size != dataSize) {
2595 EM_DEBUG_EXCEPTION("Read from file failed");
2596 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2601 if (!(send_ret = smtp_soutr_test(stream->netstream, data))) {
2603 EM_DEBUG_EXCEPTION("Failed to send the data ");
2604 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2608 sent_percent = (int) ((double)sent / (double)total_fixed * 100.0);
2609 if (last_sent_percent + 5 <= sent_percent) {
2610 double progress = (double)sent / (double)total_fixed;
2611 if (!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, sent_percent))
2612 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEND_START] Failed >>>>");
2613 last_sent_percent = sent_percent;
2615 emcore_update_notification_for_send(account_id, mail_id, progress);
2617 EM_DEBUG_LOG("Sent data Successfully. sent[%d] total[%d]", sent, total);
2625 fseek(fp, 0, SEEK_END);
2627 fseek(fp, 0, SEEK_SET);
2629 while (fgets(buf, 1024, fp)) {
2630 #ifdef FEATURE_SEND_DATA_DEBUG
2631 EM_DEBUG_LOG("%s", buf);
2633 sent += EM_SAFE_STRLEN(buf);
2635 if (!(send_ret = smtp_soutr(stream->netstream, buf)))
2637 /* Sending Progress Notification */
2638 sent_percent = (int) ((double)sent / (double)total * 100.0);
2639 if (last_sent_percent + 5 <= sent_percent) {
2640 /* Disabled Temporary
2641 if (!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, sent_percent))
2642 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_SEND_START] Failed >>>>");
2644 last_sent_percent = sent_percent;
2650 EM_DEBUG_EXCEPTION("smtp_soutr failed - %ld", send_ret);
2651 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2656 send_ret = smtp_send(stream, ".", 0);
2657 EM_DEBUG_LOG("[SMTP] . --------> %s", stream->reply);
2659 if (send_ret != SMTP_RESPONSE_OK) {
2668 smtp_send(stream, "RSET", 0);
2671 emcore_free_account(ref_account);
2672 EM_SAFE_FREE(ref_account);
2680 EM_PROFILE_END(profile_emcore_send_mail_smtp);
2681 EM_DEBUG_FUNC_END("ret [%d]", ret);
2685 /* ------ rfc822 handle --------------------------------------------------- */
2686 #define RANDOM_NUMBER_LENGTH 35
2688 char *emcore_generate_content_id_string(const char *hostname, int *err)
2690 EM_DEBUG_FUNC_BEGIN("hostname[%p]", hostname);
2693 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2695 *err = EMAIL_ERROR_INVALID_PARAM;
2699 int cid_length = RANDOM_NUMBER_LENGTH + EM_SAFE_STRLEN(hostname) + 2, random_number_1, random_number_2, random_number_3, random_number_4;
2700 char *cid_string = NULL;
2702 cid_string = malloc(cid_length);
2706 *err = EMAIL_ERROR_OUT_OF_MEMORY;
2710 memset(cid_string, 0, cid_length);
2712 srand(time(NULL) + rand());
2713 random_number_1 = rand() * rand();
2714 random_number_2 = rand() * rand();
2715 random_number_3 = rand() * rand();
2716 random_number_4 = rand() * rand();
2718 SNPRINTF(cid_string, cid_length, "<%08x%08x%08x%08x@%s>", random_number_1, random_number_2, random_number_3, random_number_4, hostname);
2721 *err = EMAIL_ERROR_NONE;
2723 EM_DEBUG_FUNC_END("cid_string [%s]", cid_string);
2728 /* ------ attach_part ----------------------------------------------------- */
2729 /* data : if filename NULL, content data. */
2730 /* else absolute path of file to be attached. */
2731 /* data_len : length of data. if filename not NULL, ignored. */
2732 /* file_name : attahcment name. */
2733 static int attach_part (BODY *body, const unsigned char *data, int data_len,
2734 char *filename, char *content_sub_type, int is_inline, int *err_code)
2736 EM_DEBUG_FUNC_BEGIN_SEC("body[%p], data[%s], data_len[%d], filename[%s], content_sub_type[%s], err_code[%p]", body, data, data_len, filename, content_sub_type, err_code);
2739 int error = EMAIL_ERROR_NONE;
2740 int has_special_character = 0;
2741 int base64_file_name_length = 0;
2743 int mail_type = EMAIL_SMIME_NONE;
2745 gsize bytes_written;
2746 char *encoded_file_name = NULL;
2747 char *extension = NULL;
2748 char *base64_file_name = NULL;
2749 char *result_file_name = NULL;
2750 char content_disposition[100] = { 0, };
2751 PARAMETER *last_param = NULL;
2752 PARAMETER *param = NULL;
2753 PART *last_part = NULL;
2755 SIZEDTEXT source_text;
2756 GError *glib_error = NULL;
2757 CHARSET *result_charset = NULL;
2760 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2761 error = EMAIL_ERROR_INVALID_PARAM;
2765 if (body->nested.part) {
2766 last_part = body->nested.part;
2768 if (last_part != NULL) {
2769 while (last_part->next)
2770 last_part = last_part->next;
2775 part = mail_newbody_part();
2777 EM_DEBUG_EXCEPTION("mail_newbody_part failed...");
2778 error = EMAIL_ERROR_OUT_OF_MEMORY;
2782 /* add it to the last */
2785 last_part->next = part;
2787 body->nested.part = part;
2792 /* content_data = (unsigned char *)fs_get(data_len + 1); */
2793 /* memcpy(content_data, data, data_len); */
2794 /* content_data[data_len] = 0; */
2796 /* part->body.contents.text.data = content_data; */
2797 /* part->body.contents.text.size = data_len; */
2799 if (filename) { /* attachment */
2800 source_text.data = (unsigned char*)filename;
2801 source_text.size = EM_SAFE_STRLEN(filename);
2803 result_charset = (CHARSET*)utf8_infercharset(&source_text);
2805 if(result_charset) {
2806 EM_DEBUG_LOG_SEC("return_charset->name [%s]", result_charset->name);
2807 encoded_file_name = (char*)g_convert (filename, -1, "UTF-8", result_charset->name, &bytes_read, &bytes_written, &glib_error);
2811 while(filename[i]) {
2812 if(filename[i++] & 0x80) {
2813 has_special_character = 1;
2817 EM_DEBUG_LOG("has_special_character [%d]", has_special_character);
2818 if(has_special_character)
2819 encoded_file_name = (char*)g_convert (filename, -1, "UTF-8", "EUC-KR", &bytes_read, &bytes_written, &glib_error);
2822 EM_DEBUG_LOG_SEC("encoded_file_name [%s]", encoded_file_name);
2824 if(encoded_file_name == NULL)
2825 encoded_file_name = strdup(filename);
2827 if(!em_encode_base64(encoded_file_name, EM_SAFE_STRLEN(encoded_file_name), &base64_file_name, (unsigned long*)&base64_file_name_length, &error)) {
2828 EM_DEBUG_EXCEPTION("em_encode_base64 failed. error [%d]", error);
2832 result_file_name = em_replace_all_string(base64_file_name, "\015\012", "");
2834 EM_DEBUG_LOG("base64_file_name_length [%d]", base64_file_name_length);
2836 if(result_file_name) {
2837 EM_SAFE_FREE(encoded_file_name);
2838 encoded_file_name = em_malloc(EM_SAFE_STRLEN(result_file_name) + 15);
2839 if(!encoded_file_name) {
2840 EM_DEBUG_EXCEPTION("em_malloc failed.");
2843 snprintf(encoded_file_name, EM_SAFE_STRLEN(result_file_name) + 15, "=?UTF-8?B?%s?=", result_file_name);
2844 EM_DEBUG_LOG_SEC("encoded_file_name [%s]", encoded_file_name);
2847 extension = em_get_extension_from_file_path(filename, NULL);
2849 part->body.type = em_get_content_type_from_extension_string(extension, NULL);
2850 if(part->body.type == TYPEIMAGE) {
2851 part->body.subtype = strdup(extension);
2852 part->body.encoding = ENCBINARY;
2853 } else if (part->body.type == TYPEPKCS7_SIGN) {
2854 part->body.subtype = strdup("pkcs7-signature");
2855 part->body.type = TYPEAPPLICATION;
2856 part->body.encoding = ENCBINARY;
2857 } else if (part->body.type == TYPEPKCS7_MIME) {
2858 part->body.subtype = strdup("pkcs7-mime");
2859 part->body.type = TYPEAPPLICATION;
2860 part->body.encoding = ENCBINARY;
2861 } else if (part->body.type == TYPEPGP) {
2862 part->body.type = TYPEAPPLICATION;
2863 part->body.subtype = EM_SAFE_STRDUP(content_sub_type);
2864 part->body.encoding = ENC7BIT;
2866 part->body.subtype = strdup("octet-stream");
2867 part->body.encoding = ENCBINARY;
2870 if (!extension && content_sub_type) {
2873 if (strcasecmp(content_sub_type, "IMAGE") == 0) {
2874 part->body.type = TYPEIMAGE;
2875 if ((p = strstr(content_sub_type, "/"))) {
2876 EM_SAFE_FREE(part->body.subtype);
2877 part->body.subtype = EM_SAFE_STRDUP(p+1);
2882 part->body.size.bytes = data_len;
2885 part->body.sparep = EM_SAFE_STRDUP((char *)data); /* file path */
2887 part->body.sparep = NULL;
2889 SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "attachment");
2891 part->body.disposition.type = cpystr(content_disposition);
2893 /* BODY PARAMETER */
2894 /* another parameter or get parameter-list from this function-parameter */
2895 param = mail_newbody_parameter();
2896 if (param == NULL) {
2897 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2898 error = EMAIL_ERROR_OUT_OF_MEMORY;
2902 param->attribute = cpystr("name");
2903 param->value = cpystr(encoded_file_name);
2906 last_part->body.parameter = last_param;
2910 part->body.id = emcore_generate_content_id_string("org.tizen.email", &error);
2911 part->body.type = TYPEIMAGE;
2912 /* EM_SAFE_FREE(part->body.subtype); */
2913 /* part->body.subtype = EM_SAFE_STRDUP(content_sub_type); */
2916 /* DISPOSITION PARAMETER */
2917 param = mail_newbody_parameter();
2918 if (param == NULL) {
2919 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2920 error = EMAIL_ERROR_OUT_OF_MEMORY;
2924 param->attribute = cpystr("filename");
2925 param->value = cpystr(encoded_file_name);
2928 last_part->body.disposition.parameter = last_param;
2931 last_part->body.disposition.type = strdup("inline");
2934 if (content_sub_type && !strcasecmp(content_sub_type, "pgp-encrypted"))
2935 mail_type = EMAIL_PGP_ENCRYPTED;
2937 if (mail_type != EMAIL_PGP_ENCRYPTED) {
2938 /* text body (plain/html) */
2939 part->body.type = TYPETEXT;
2940 part->body.size.bytes = data_len;
2943 part->body.sparep = EM_SAFE_STRDUP((char *)data); /* file path */
2945 part->body.sparep = NULL;
2948 if (!content_sub_type) {
2949 /* Plain text body */
2950 part->body.encoding = ENC8BIT;
2951 part->body.subtype = cpystr("plain");
2952 last_param = part->body.parameter;
2954 if (last_param != NULL) {
2955 while (last_param->next)
2956 last_param = last_param->next;
2959 param = mail_newbody_parameter();
2961 if (param == NULL) {
2962 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2963 error = EMAIL_ERROR_OUT_OF_MEMORY;
2967 param->attribute = cpystr("CHARSET");
2970 gchar *extract_charset_plain = g_path_get_basename((const gchar *)data);
2971 if (extract_charset_plain != NULL && extract_charset_plain[0] != '\0')
2972 param->value = cpystr(extract_charset_plain);
2973 g_free(extract_charset_plain);
2976 param->value = cpystr("UTF-8");
2979 param->value = cpystr("UTF-8");
2983 if (last_param != NULL)
2984 last_param->next = param;
2986 part->body.parameter = param;
2989 /* HTML text body */
2990 part->body.encoding = ENC8BIT;
2991 part->body.subtype = cpystr(content_sub_type);
2993 last_param = part->body.parameter;
2995 if (last_param != NULL) {
2996 while (last_param->next)
2997 last_param = last_param->next;
3000 param = mail_newbody_parameter();
3002 if (param == NULL) {
3003 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
3004 error = EMAIL_ERROR_OUT_OF_MEMORY;
3008 param->attribute = cpystr("CHARSET");
3012 gchar *extract_charset = g_path_get_basename((const gchar *)data);
3013 if (extract_charset != NULL) {
3014 if ((pHtml = strstr(extract_charset, ".htm")) != NULL) {
3015 extract_charset[pHtml-extract_charset] = '\0';
3016 param->value = cpystr(extract_charset);
3021 param->value = cpystr("UTF-8");
3023 EM_SAFE_FREE(extract_charset);
3026 param->value = cpystr("UTF-8");
3029 if (last_param != NULL)
3030 last_param->next = param;
3032 part->body.parameter = param;
3035 /* NOTE : need to require this code. */
3036 /* sprintf(content_disposition, "%s\0", "inline"); */
3038 SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "inline");
3039 part->body.disposition.type = cpystr(content_disposition);
3042 part->body.type = TYPEAPPLICATION;
3043 part->body.subtype = strdup(content_sub_type);
3044 part->body.size.bytes = data_len;
3045 part->body.encoding = ENC7BIT;
3048 part->body.sparep = EM_SAFE_STRDUP((char *)data); /* file path */
3050 part->body.contents.text.data = (unsigned char *)strdup("Version: 1");
3051 part->body.contents.text.size = strlen("Version: 1");
3059 EM_SAFE_FREE(encoded_file_name);
3060 EM_SAFE_FREE(result_file_name); /*prevent 26242*/
3061 EM_SAFE_FREE(base64_file_name);
3062 if (err_code != NULL)
3064 EM_DEBUG_FUNC_END();
3068 static PART *attach_multipart_with_sub_type(BODY *parent_body, char *sub_type, int *err_code)
3070 EM_DEBUG_FUNC_BEGIN("parent_body[%p], sub_type [%s], err_code[%p]", parent_body, sub_type, err_code);
3072 int error = EMAIL_ERROR_NONE;
3074 PART *tail_part_cur = NULL;
3075 PART *new_part = NULL;
3077 if (!parent_body || !sub_type) {
3078 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3079 error = EMAIL_ERROR_INVALID_PARAM;
3083 if (parent_body->nested.part) {
3084 tail_part_cur = parent_body->nested.part;
3086 if (tail_part_cur != NULL) {
3087 while (tail_part_cur->next)
3088 tail_part_cur = tail_part_cur->next;
3092 new_part = mail_newbody_part();
3094 if (new_part == NULL) {
3095 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
3096 error = EMAIL_ERROR_OUT_OF_MEMORY;
3101 new_part->next = NULL;
3102 new_part->body.type = TYPEMULTIPART;
3103 new_part->body.subtype = EM_SAFE_STRDUP(sub_type);
3106 tail_part_cur->next = new_part;
3108 parent_body->nested.part = new_part;
3115 EM_DEBUG_FUNC_END();
3119 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
3120 static int attach_attachment_to_body(char *multi_user_name, BODY **multipart_body, BODY *text_body, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_tbl_count, int *err_code)
3122 EM_DEBUG_FUNC_BEGIN("multipart_body[%p], text_body[%p], input_attachment_tbl[%p], input_attachment_tbl_count [%d], err_code[%p]", multipart_body, text_body, input_attachment_tbl, input_attachment_tbl_count, err_code);
3126 int error = EMAIL_ERROR_NONE;
3127 BODY *frame_body = NULL;
3128 char *prefix_path = NULL;
3129 char real_file_path[MAX_PATH] = {0};
3131 /* make multipart body(multipart frame_body..) .. that has not content.. */
3133 if (!multipart_body || !text_body || !input_attachment_tbl) {
3134 EM_DEBUG_EXCEPTION(" multipart_body[%p], text_body[%p], input_attachment_tbl[%p]", multipart_body, text_body, input_attachment_tbl);
3135 error = EMAIL_ERROR_INVALID_PARAM;
3139 frame_body = mail_newbody();
3140 if (frame_body == NULL) {
3141 EM_DEBUG_EXCEPTION("mail_newbody failed...");
3142 error = EMAIL_ERROR_OUT_OF_MEMORY;
3146 frame_body->type = TYPEMULTIPART;
3147 frame_body->contents.text.data = NULL;
3148 frame_body->contents.text.size = 0;
3149 frame_body->size.bytes = 0;
3151 /* insert original text_body to frame_body.. */
3152 if (!attach_part(frame_body, text_body->sparep, 0, NULL, NULL, false, &error)) {
3153 EM_DEBUG_EXCEPTION(" attach_part failed [%d]", error);
3157 /* insert files.. */
3158 emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
3162 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
3163 error = emcore_get_container_path(multi_user_name, &prefix_path);
3164 if (error != EMAIL_ERROR_NONE) {
3165 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
3169 prefix_path = strdup("");
3172 for(i = 0; i < input_attachment_tbl_count; i++) {
3173 temp_attachment_tbl = input_attachment_tbl + i;
3175 EM_DEBUG_LOG("insert files - attachment id[%d]", temp_attachment_tbl->attachment_id);
3177 memset(real_file_path, 0x00, sizeof(real_file_path));
3178 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, temp_attachment_tbl->attachment_path);
3180 if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0) {
3181 if (!temp_attachment_tbl->attachment_name) {
3182 if (!emcore_get_file_name(real_file_path, &name, &error)) {
3183 EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
3188 name = temp_attachment_tbl->attachment_name;
3190 if (!attach_part(frame_body, (unsigned char *)real_file_path, 0, name, NULL, false, &error)) {
3191 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3201 EM_SAFE_FREE(prefix_path);
3204 *multipart_body = frame_body;
3205 else if (frame_body != NULL)
3206 mail_free_body(&frame_body);
3208 if (err_code != NULL)
3210 EM_DEBUG_FUNC_END();
3214 static char *emcore_encode_rfc2047_text(char *utf8_text, int *err_code)
3216 EM_DEBUG_FUNC_BEGIN("utf8_text[%s], err_code[%p]", utf8_text, err_code);
3218 if (utf8_text == NULL) {
3219 if (err_code != NULL)
3220 *err_code = EMAIL_ERROR_INVALID_PARAM;
3224 gsize len = EM_SAFE_STRLEN(utf8_text);
3226 EM_DEBUG_FUNC_END();
3230 return emcore_gmime_get_encoding_to_utf8(utf8_text);
3231 // return g_strdup(utf8_text); /* emoji handle */
3237 static void emcore_encode_rfc2047_address(ADDRESS *address, int *err_code)
3239 EM_DEBUG_FUNC_BEGIN("address[%p], err_code[%p]", address, err_code);
3242 if (address->personal) {
3243 char *rfc2047_personal = emcore_encode_rfc2047_text(address->personal, err_code);
3244 EM_SAFE_FREE(address->personal);
3245 address->personal = rfc2047_personal;
3247 address = address->next;
3249 EM_DEBUG_FUNC_END();
3252 #define DATE_STR_LENGTH 100
3254 static int emcore_make_envelope_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope)
3256 EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], output_envelope[%p]", input_mail_tbl_data, output_envelope);
3259 int error = EMAIL_ERROR_NONE;
3260 int is_incomplete = 0;
3262 ENVELOPE *envelope = NULL;
3263 email_account_t *ref_account = NULL;
3265 if (!input_mail_tbl_data || !output_envelope) {
3266 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3267 error = EMAIL_ERROR_INVALID_PARAM;
3268 return error; /* prevent 32729 */
3271 if ( (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0 && !input_mail_tbl_data->body_download_status) {
3272 EM_DEBUG_EXCEPTION("input_mail_tbl_data->body_download_status[%p]", input_mail_tbl_data->body_download_status);
3273 error = EMAIL_ERROR_INVALID_PARAM;
3277 if (!(envelope = mail_newenvelope())) {
3278 EM_DEBUG_EXCEPTION("mail_newenvelope failed...");
3279 error = EMAIL_ERROR_OUT_OF_MEMORY;
3283 is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
3285 if (is_incomplete && (input_mail_tbl_data->account_id > 0)) {
3286 ref_account = emcore_get_account_reference(multi_user_name, input_mail_tbl_data->account_id, false);
3288 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
3289 error = EMAIL_ERROR_INVALID_ACCOUNT;
3293 if (ref_account->user_email_address && ref_account->user_email_address[0] != '\0') {
3294 char *p = cpystr(ref_account->user_email_address);
3297 EM_DEBUG_EXCEPTION("cpystr failed...");
3298 error = EMAIL_ERROR_OUT_OF_MEMORY;
3302 EM_DEBUG_LOG("Assign envelope->from");
3304 if (input_mail_tbl_data->full_address_from) {
3305 char *temp_address_string = NULL ;
3306 em_skip_whitespace_without_alias(input_mail_tbl_data->full_address_from , &temp_address_string);
3307 EM_DEBUG_LOG_SEC("address[temp_address_string][%s]", temp_address_string);
3308 rfc822_parse_adrlist(&envelope->from, temp_address_string, NULL);
3309 EM_SAFE_FREE(temp_address_string);
3310 temp_address_string = NULL ;
3313 envelope->from = rfc822_parse_mailbox(&p, NULL);
3316 if (!envelope->from) {
3317 EM_DEBUG_EXCEPTION("rfc822_parse_mailbox failed...");
3318 error = EMAIL_ERROR_INVALID_ADDRESS;
3322 if (envelope->from->personal == NULL) {
3323 if (ref_account->options.display_name_from && ref_account->options.display_name_from[0] != '\0')
3324 envelope->from->personal = cpystr(ref_account->options.display_name_from);
3326 envelope->from->personal =
3327 (ref_account->user_display_name && ref_account->user_display_name[0] != '\0') ?
3328 cpystr(ref_account->user_display_name) : NULL;
3333 if (ref_account->return_address && ref_account->return_address[0] != '\0') {
3334 char *p = cpystr(ref_account->return_address);
3337 EM_DEBUG_EXCEPTION("cpystr failed...");
3338 error = EMAIL_ERROR_OUT_OF_MEMORY;
3341 envelope->return_path = rfc822_parse_mailbox(&p, NULL);
3347 if (input_mail_tbl_data->full_address_from == NULL) {
3348 EM_DEBUG_EXCEPTION("input_mail_tbl_data->full_address_from[%p]", input_mail_tbl_data->full_address_from);
3349 error = EMAIL_ERROR_INVALID_MAIL;
3355 if (input_mail_tbl_data->full_address_from) {
3356 for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_from); i < j; i++) {
3357 if (input_mail_tbl_data->full_address_from[i] == ';')
3358 input_mail_tbl_data->full_address_from[i] = ',';
3362 if (input_mail_tbl_data->full_address_return) {
3363 for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_return); i < j; i++) {
3364 if (input_mail_tbl_data->full_address_return[i] == ';')
3365 input_mail_tbl_data->full_address_return[i] = ',';
3368 em_skip_whitespace_without_alias(input_mail_tbl_data->full_address_from , &pAdd);
3369 EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
3371 rfc822_parse_adrlist(&envelope->from, pAdd, NULL);
3375 em_skip_whitespace(input_mail_tbl_data->full_address_return , &pAdd);
3376 EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
3378 rfc822_parse_adrlist(&envelope->return_path, pAdd, NULL);
3386 if (input_mail_tbl_data->full_address_to) {
3387 for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_to); i < j; i++) {
3388 if (input_mail_tbl_data->full_address_to[i] == ';')
3389 input_mail_tbl_data->full_address_to[i] = ',';
3393 if (input_mail_tbl_data->full_address_cc) {
3394 for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_cc); i < j; i++) {
3395 if (input_mail_tbl_data->full_address_cc[i] == ';')
3396 input_mail_tbl_data->full_address_cc[i] = ',';
3400 if (input_mail_tbl_data->full_address_bcc) {
3401 for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_bcc); i < j; i++) {
3402 if (input_mail_tbl_data->full_address_bcc[i] == ';')
3403 input_mail_tbl_data->full_address_bcc[i] = ',';
3408 envelope->message_id = EM_SAFE_STRDUP(input_mail_tbl_data->message_id);
3409 EM_DEBUG_LOG_SEC("message_id[%s]", envelope->message_id);
3411 em_skip_whitespace(input_mail_tbl_data->full_address_to , &pAdd);
3412 EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
3414 rfc822_parse_adrlist(&envelope->to, pAdd, NULL);
3418 EM_DEBUG_LOG_SEC("address[input_mail_tbl_data->full_address_cc][%s]", input_mail_tbl_data->full_address_cc);
3419 em_skip_whitespace(input_mail_tbl_data->full_address_cc , &pAdd);
3420 EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
3422 rfc822_parse_adrlist(&envelope->cc, pAdd, NULL);
3426 em_skip_whitespace(input_mail_tbl_data->full_address_bcc , &pAdd);
3427 rfc822_parse_adrlist(&envelope->bcc, pAdd, NULL);
3431 emcore_encode_rfc2047_address(envelope->return_path, &error);
3432 emcore_encode_rfc2047_address(envelope->from, &error);
3433 emcore_encode_rfc2047_address(envelope->sender, &error);
3434 emcore_encode_rfc2047_address(envelope->reply_to, &error);
3435 emcore_encode_rfc2047_address(envelope->to, &error);
3436 emcore_encode_rfc2047_address(envelope->cc, &error);
3437 emcore_encode_rfc2047_address(envelope->bcc, &error);
3439 if (input_mail_tbl_data->subject)
3440 envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
3442 char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
3443 rfc822_date(rfc822_date_string);
3445 EM_DEBUG_LOG("rfc822_date : [%s]", rfc822_date_string);
3447 if (!is_incomplete) {
3448 char localtime_string[DATE_STR_LENGTH] = { 0, };
3449 time_t tn = time(0);
3450 struct tm *t = gmtime(&tn);
3452 EM_DEBUG_EXCEPTION("gmtime failed");
3453 error = EMAIL_ERROR_SYSTEM_FAILURE;
3457 int zone = t->tm_hour * 60 + t->tm_min;
3458 int julian = t->tm_yday;
3460 t = localtime(&input_mail_tbl_data->date_time);
3462 EM_DEBUG_EXCEPTION("localtime failed");
3463 error = EMAIL_ERROR_SYSTEM_FAILURE;
3467 zone = t->tm_hour * 60 + t->tm_min - zone;
3469 if ((julian = t->tm_yday - julian))
3470 zone += ((julian < 0) == (abs(julian) == 1)) ? -24 * 60 : 24*60;
3472 SNPRINTF(localtime_string, DATE_STR_LENGTH, "%s, %d %s %d %02d:%02d:%02d "
3482 EM_DEBUG_LOG("localtime string : [%s]", localtime_string);
3483 /* append last 5byes("+0900") */
3484 g_strlcat(localtime_string, strchr(rfc822_date_string, '+'), DATE_STR_LENGTH);
3485 envelope->date = (unsigned char *)cpystr((const char *)localtime_string);
3488 envelope->date = (unsigned char *)cpystr((const char *)rfc822_date_string);
3496 *output_envelope = envelope;
3498 mail_free_envelope(&envelope);
3499 *output_envelope = NULL;
3503 emcore_free_account(ref_account);
3504 EM_SAFE_FREE(ref_account);
3507 EM_DEBUG_FUNC_END("error [%d]", error);
3511 static char *emcore_get_digest_string(int digest_type, int mime_type)
3513 EM_DEBUG_FUNC_BEGIN();
3514 char *digest_string = NULL;
3515 char p_digest_string[100] = {0, };
3517 switch (mime_type) {
3518 case EMAIL_SMIME_SIGNED :
3519 case EMAIL_SMIME_ENCRYPTED :
3520 case EMAIL_SMIME_SIGNED_AND_ENCRYPTED :
3521 memset(p_digest_string, 0x00, sizeof(p_digest_string));
3523 case EMAIL_PGP_SIGNED :
3524 case EMAIL_PGP_ENCRYPTED :
3525 case EMAIL_PGP_SIGNED_AND_ENCRYPTED :
3526 memset(p_digest_string, 0x00, sizeof(p_digest_string));
3527 strcpy(p_digest_string, "pgp-");
3531 switch (digest_type) {
3532 case DIGEST_TYPE_SHA1 :
3533 strcat(p_digest_string, "sha1");
3535 case DIGEST_TYPE_MD5 :
3536 strcat(p_digest_string, "md5");
3538 case DIGEST_TYPE_RIPEMD160 :
3539 strcat(p_digest_string, "ripemd160");
3541 case DIGEST_TYPE_MD2 :
3542 strcat(p_digest_string, "md2");
3544 case DIGEST_TYPE_TIGER192 :
3545 strcat(p_digest_string, "tiger192");
3547 case DIGEST_TYPE_HAVAL5160 :
3548 strcat(p_digest_string, "haval5160");
3550 case DIGEST_TYPE_SHA256 :
3551 strcat(p_digest_string, "sha256");
3553 case DIGEST_TYPE_SHA384 :
3554 strcat(p_digest_string, "sha384");
3556 case DIGEST_TYPE_SHA512 :
3557 strcat(p_digest_string, "sha512");
3559 case DIGEST_TYPE_SHA224 :
3560 strcat(p_digest_string, "sha224");
3562 case DIGEST_TYPE_MD4 :
3563 strcat(p_digest_string, "md4");
3567 digest_string = EM_SAFE_STRDUP(p_digest_string);
3569 EM_DEBUG_FUNC_END();
3570 return digest_string;
3574 /* Description : Make RFC822 text file from mail_tbl data */
3576 /* input_mail_tbl_data : */
3577 /* is_draft : this mail is draft mail. */
3578 /* file_path : path of file that rfc822 data will be written to. */
3579 INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_count, ENVELOPE **env, char **file_path, email_option_t *sending_option, int *err_code)
3581 EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], env[%p], file_path[%p], sending_option[%p], err_code[%p]", input_mail_tbl_data, env, file_path, sending_option, err_code);
3584 int error = EMAIL_ERROR_NONE;
3586 char *digest_string = NULL;
3588 ENVELOPE *envelope = NULL;
3589 BODY *root_body = NULL;
3590 BODY *text_body = NULL;
3591 BODY *html_body = NULL;
3592 PART *part_for_alternative = NULL;
3593 PART *part_for_related = NULL;
3594 PARAMETER *param = NULL;
3596 char *prefix_path = NULL;
3597 char real_file_path[MAX_PATH] = {0};
3599 if (!input_mail_tbl_data) {
3600 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3601 error = EMAIL_ERROR_INVALID_PARAM;
3605 if ( (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0 && !input_mail_tbl_data->body_download_status) {
3606 EM_DEBUG_EXCEPTION("input_mail_tbl_data->body_download_status[%p]", input_mail_tbl_data->body_download_status);
3607 error = EMAIL_ERROR_INVALID_PARAM;
3611 if ( (error = emcore_make_envelope_from_mail(multi_user_name, input_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
3612 EM_DEBUG_EXCEPTION("emcore_make_envelope_from_mail failed [%d]", error);
3616 EM_DEBUG_LOG_SEC("input_mail_tbl_data->file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3617 EM_DEBUG_LOG_SEC("input_mail_tbl_data->file_path_html[%s]", input_mail_tbl_data->file_path_html);
3618 EM_DEBUG_LOG_SEC("input_mail_tbl_data->file_path_mime_entity[%s]", input_mail_tbl_data->file_path_mime_entity);
3619 EM_DEBUG_LOG("input_mail_tbl_data->body->attachment_num[%d]", input_mail_tbl_data->attachment_count);
3621 root_body = mail_newbody();
3622 if (root_body == NULL) {
3623 EM_DEBUG_EXCEPTION("mail_newbody failed...");
3624 error = EMAIL_ERROR_OUT_OF_MEMORY;
3628 param = mail_newbody_parameter();
3629 if (param == NULL) {
3630 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
3631 error = EMAIL_ERROR_OUT_OF_MEMORY;
3635 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
3636 error = emcore_get_container_path(multi_user_name, &prefix_path);
3637 if (error != EMAIL_ERROR_NONE) {
3638 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
3642 prefix_path = strdup("");
3645 if (input_attachment_count > 0) {
3646 /* handle the Multipart/mixed, Multipart/related and S/MIME */
3647 EM_DEBUG_LOG("input_attachment_num [%d]", input_attachment_count);
3648 EM_DEBUG_LOG("inline_attachment_num [%d]", input_mail_tbl_data->inline_content_count);
3649 EM_DEBUG_LOG("attachment_num [%d]", input_mail_tbl_data->attachment_count);
3651 if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE) {
3652 if (input_mail_tbl_data->attachment_count > 0) {
3653 root_body->type = TYPEMULTIPART;
3654 root_body->subtype = strdup("MIXED");
3656 root_body->type = TYPEMULTIPART;
3657 root_body->subtype = strdup("RELATED");
3660 mail_free_body_parameter(¶m);
3662 } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED) {
3663 PARAMETER *protocol_param = mail_newbody_parameter();
3665 root_body->type = TYPEMULTIPART;
3666 root_body->subtype = strdup("SIGNED");
3668 param->attribute = cpystr("micalg");
3670 digest_string = emcore_get_digest_string(input_mail_tbl_data->digest_type, input_mail_tbl_data->smime_type);
3671 param->value = cpystr(digest_string);
3673 protocol_param->attribute = cpystr("protocol");
3674 protocol_param->value = cpystr("application/pkcs7-signature");
3675 protocol_param->next = NULL;
3676 param->next = protocol_param;
3677 } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED || input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
3678 root_body->type = TYPEAPPLICATION;
3679 root_body->subtype = strdup("PKCS7-MIME");
3681 param->attribute = cpystr("name");
3682 param->value = cpystr("smime.p7m");
3684 } else if (input_mail_tbl_data->smime_type == EMAIL_PGP_SIGNED) {
3685 PARAMETER *protocol_param = mail_newbody_parameter();
3687 root_body->type = TYPEMULTIPART;
3688 root_body->subtype = strdup("SIGNED");
3690 param->attribute = cpystr("micalg");
3692 digest_string = emcore_get_digest_string(input_mail_tbl_data->digest_type, input_mail_tbl_data->smime_type);
3693 param->value = cpystr(digest_string);
3695 protocol_param->attribute = cpystr("protocol");
3696 protocol_param->value = cpystr("application/pgp-signature");
3697 protocol_param->next = NULL;
3698 param->next = protocol_param;
3700 root_body->type = TYPEMULTIPART;
3701 root_body->subtype = strdup("encrypted");
3703 param->attribute = cpystr("protocol");
3704 param->value = cpystr("application/pgp-encrypted");
3708 root_body->contents.text.data = NULL;
3709 root_body->contents.text.size = 0;
3710 root_body->size.bytes = 0;
3711 root_body->parameter = param;
3713 if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE &&
3714 (input_mail_tbl_data->file_path_plain && input_mail_tbl_data->file_path_html)) {
3715 /* Multipart/mixed -> multipart/related -> multipart/alternative : has inline content and */
3716 /* Multipart/mixed -> Multipart/alternative */
3718 if (input_mail_tbl_data->inline_content_count > 0 && (strcasecmp(root_body->subtype, "RELATED") != 0)) {
3719 part_for_related = attach_multipart_with_sub_type(root_body, "RELATED", &error);
3720 if (!part_for_related) {
3721 EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [related] failed [%d]", error);
3726 if (part_for_related)
3727 part_for_alternative = attach_multipart_with_sub_type(&(part_for_related->body), "ALTERNATIVE", &error);
3729 part_for_alternative = attach_multipart_with_sub_type(root_body, "ALTERNATIVE", &error);
3730 if (!part_for_alternative) {
3731 EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [alternative] failed [%d]", error);
3735 if (strlen(input_mail_tbl_data->file_path_plain) > 0) {
3736 EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3738 memset(real_file_path, 0x00, sizeof(real_file_path));
3739 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
3741 text_body = &(part_for_alternative->body);
3742 if (!attach_part(text_body, (unsigned char *)real_file_path, 0, NULL, "plain", false, &error)) {
3743 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3748 if (strlen(input_mail_tbl_data->file_path_html) > 0) {
3749 EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
3751 memset(real_file_path, 0x00, sizeof(real_file_path));
3752 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
3754 html_body = &(part_for_alternative->body);
3755 if (!attach_part (html_body, (unsigned char *)real_file_path, 0, NULL, "html", false, &error)) {
3756 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3761 else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE &&
3762 (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)) {
3763 if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0) {
3764 EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3766 memset(real_file_path, 0x00, sizeof(real_file_path));
3767 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
3769 if (!attach_part (root_body, (unsigned char *)real_file_path, 0, NULL, "plain", false, &error)) {
3770 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3775 if (input_mail_tbl_data->file_path_html && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_html) > 0) {
3776 EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
3777 if (input_mail_tbl_data->inline_content_count > 0 &&
3778 (root_body->subtype && (strcasecmp(root_body->subtype, "RELATED") != 0))) {
3779 part_for_related = attach_multipart_with_sub_type(root_body, "RELATED", &error);
3780 if (!part_for_related) {
3781 EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [related] failed [%d]", error);
3786 memset(real_file_path, 0x00, sizeof(real_file_path));
3787 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
3789 if (part_for_related) {
3790 if (!attach_part(&(part_for_related->body), (unsigned char *)real_file_path, 0,
3791 NULL, "html", false, &error)) {
3792 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3796 if (!attach_part (root_body, (unsigned char *)real_file_path, 0,
3797 NULL, "html", false, &error)) {
3798 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3803 } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED || input_mail_tbl_data->smime_type == EMAIL_PGP_SIGNED) {
3804 if (input_mail_tbl_data->file_path_mime_entity && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_mime_entity) > 0) {
3805 EM_DEBUG_LOG_SEC("file_path_mime_entity[%s]", input_mail_tbl_data->file_path_mime_entity);
3807 memset(real_file_path, 0x00, sizeof(real_file_path));
3808 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_mime_entity);
3810 root_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_mime_entity);
3812 } else if (input_mail_tbl_data->smime_type == EMAIL_PGP_ENCRYPTED || input_mail_tbl_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
3813 if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0) {
3814 EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3815 if (!attach_part (root_body, NULL, 0, NULL, "pgp-encrypted", false, &error)) {
3816 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3821 EM_DEBUG_LOG("S/MIME encrypted type");
3824 if (input_attachment_tbl && input_attachment_count) {
3825 emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
3827 BODY *body_to_attach = NULL;
3829 for(i = 0; i < input_attachment_count; i++) {
3830 temp_attachment_tbl = input_attachment_tbl + i;
3832 EM_DEBUG_LOG_SEC("attachment_name[%s], attachment_path[%s]", temp_attachment_tbl->attachment_name, temp_attachment_tbl->attachment_path);
3834 if (!temp_attachment_tbl->attachment_name) {
3835 if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error)) {
3836 EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
3841 name = temp_attachment_tbl->attachment_name;
3843 EM_DEBUG_LOG_SEC("name[%s]", name);
3845 if (temp_attachment_tbl->attachment_inline_content_status && part_for_related)
3846 body_to_attach = &(part_for_related->body);
3848 body_to_attach = root_body;
3850 memset(real_file_path, 0x00, sizeof(real_file_path));
3851 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, temp_attachment_tbl->attachment_path);
3853 if (!attach_part(body_to_attach,
3854 (unsigned char *)real_file_path,
3857 temp_attachment_tbl->attachment_mime_type,
3858 temp_attachment_tbl->attachment_inline_content_status,
3861 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3868 else if (input_mail_tbl_data->file_path_plain && input_mail_tbl_data->file_path_html) {
3869 /* Handle the Multipart/alternative */
3870 root_body->type = TYPEMULTIPART;
3871 root_body->subtype = strdup("ALTERNATIVE");
3873 mail_free_body_parameter(¶m);
3876 root_body->contents.text.data = NULL;
3877 root_body->contents.text.size = 0;
3878 root_body->size.bytes = 0;
3879 root_body->parameter = param;
3881 if (strlen(input_mail_tbl_data->file_path_plain) > 0) {
3882 EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3884 memset(real_file_path, 0x00, sizeof(real_file_path));
3885 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
3887 if (!attach_part(root_body, (unsigned char *)real_file_path, 0, NULL, "plain", false, &error)) {
3888 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3893 if (strlen(input_mail_tbl_data->file_path_html) > 0) {
3894 EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
3896 memset(real_file_path, 0x00, sizeof(real_file_path));
3897 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
3899 if (!attach_part (root_body, (unsigned char *)real_file_path, 0, NULL, "html", false, &error)) {
3900 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3906 root_body->type = TYPETEXT;
3907 root_body->encoding = ENC8BIT;
3908 if (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html) {
3909 if (input_mail_tbl_data->file_path_plain) {
3910 memset(real_file_path, 0x00, sizeof(real_file_path));
3911 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
3913 memset(real_file_path, 0x00, sizeof(real_file_path));
3914 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
3917 root_body->sparep = EM_SAFE_STRDUP(real_file_path);
3920 root_body->sparep = NULL;
3922 if (input_mail_tbl_data->file_path_html != NULL && input_mail_tbl_data->file_path_html[0] != '\0')
3923 root_body->subtype = strdup("html");
3924 if (root_body->sparep)
3925 root_body->size.bytes = EM_SAFE_STRLEN(root_body->sparep);
3927 root_body->size.bytes = 0;
3931 if (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) {
3933 EM_DEBUG_LOG("REPORT MAIL");
3934 envelope->references = cpystr(input_mail_tbl_data->message_id);
3938 EM_DEBUG_LOG("write rfc822 : file_path[%p]", file_path);
3940 if ((error = emcore_write_rfc822 (envelope, root_body, input_mail_tbl_data->priority,
3941 input_mail_tbl_data->report_status, file_path)) != EMAIL_ERROR_NONE) {
3942 EM_DEBUG_EXCEPTION("emcore_write_rfc822 failed [%d]", error);
3950 if ((ret == true) && (env != NULL))
3952 else if (envelope != NULL)
3953 mail_free_envelope(&envelope);
3955 if (text_body != NULL)
3956 mail_free_body(&text_body);
3958 if (root_body != NULL)
3959 mail_free_body(&root_body);
3961 EM_SAFE_FREE(digest_string);
3962 EM_SAFE_FREE(prefix_path);
3964 if (err_code != NULL)
3967 EM_DEBUG_FUNC_END("ret [%d]", ret);
3971 INTERNAL_FUNC int emcore_make_rfc822_file(char *multi_user_name, email_mail_data_t *input_mail_tbl_data, email_attachment_data_t *input_attachment_tbl, int input_attachment_count, int mime_entity_status, char **file_path, int *err_code)
3973 EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], file_path[%p], err_code[%p]", input_mail_tbl_data, file_path, err_code);
3975 int err = EMAIL_ERROR_NONE;
3978 int attachment_count = 0;
3979 int inline_content_count = 0;
3981 emstorage_mail_tbl_t *modified_mail_data = NULL;
3982 emstorage_attachment_tbl_t *modified_attachment_data = NULL;
3984 /* Convert from email_mail_data_t to emstorage_mail_tbl_t */
3985 if (!em_convert_mail_data_to_mail_tbl(input_mail_tbl_data, 1, &modified_mail_data, &err)) {
3986 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", err);
3990 if (mime_entity_status)
3991 modified_mail_data->smime_type = EMAIL_SMIME_NONE;
3993 /* Convert from email_attachment_data_t to emstorage_attachment_tbl_t */
3994 if (input_attachment_count > 0) {
3995 modified_attachment_data = em_malloc(sizeof(emstorage_attachment_tbl_t) * input_attachment_count);
3996 if (modified_attachment_data == NULL) {
3997 EM_DEBUG_EXCEPTION("em_malloc failed");
3998 err = EMAIL_ERROR_OUT_OF_MEMORY;
4003 for (i = 0; i < input_attachment_count; i++) {
4004 modified_attachment_data[i].attachment_id = input_attachment_tbl[i].attachment_id;
4005 modified_attachment_data[i].attachment_name = EM_SAFE_STRDUP(input_attachment_tbl[i].attachment_name);
4006 modified_attachment_data[i].attachment_path = EM_SAFE_STRDUP(input_attachment_tbl[i].attachment_path);
4007 modified_attachment_data[i].content_id = EM_SAFE_STRDUP(input_attachment_tbl[i].content_id);
4008 modified_attachment_data[i].attachment_id = input_attachment_tbl[i].attachment_size;
4009 modified_attachment_data[i].mail_id = input_attachment_tbl[i].mail_id;
4010 modified_attachment_data[i].account_id = input_attachment_tbl[i].account_id;
4011 modified_attachment_data[i].mailbox_id = input_attachment_tbl[i].mailbox_id;
4012 modified_attachment_data[i].attachment_save_status = input_attachment_tbl[i].save_status;
4013 modified_attachment_data[i].attachment_drm_type = input_attachment_tbl[i].drm_status;
4014 modified_attachment_data[i].attachment_inline_content_status = input_attachment_tbl[i].inline_content_status;
4015 modified_attachment_data[i].attachment_mime_type = EM_SAFE_STRDUP(input_attachment_tbl[i].attachment_mime_type);
4017 if (input_attachment_tbl[i].inline_content_status == INLINE_ATTACHMENT)
4018 inline_content_count += 1;
4020 attachment_count += 1;
4023 modified_mail_data->attachment_count = attachment_count;
4024 modified_mail_data->inline_content_count = inline_content_count;
4026 if (!emcore_make_rfc822_file_from_mail(multi_user_name, modified_mail_data, modified_attachment_data, input_attachment_count, NULL, file_path, NULL, &err)) {
4027 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
4035 if (modified_mail_data)
4036 emstorage_free_mail(&modified_mail_data, 1, NULL);
4038 if (modified_attachment_data)
4039 emstorage_free_attachment(&modified_attachment_data, input_attachment_count, NULL);
4044 EM_DEBUG_FUNC_END("ret [%d]", ret);
4048 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
4049 static int emcore_get_report_mail_body(char *multi_user_name, ENVELOPE *envelope, BODY **multipart_body, int *err_code)
4051 EM_DEBUG_FUNC_BEGIN("envelope[%p], mulitpart_body[%p], err_code[%p]", envelope, multipart_body, err_code);
4054 int err = EMAIL_ERROR_NONE;
4056 BODY *m_body = NULL;
4057 BODY *p_body = NULL;
4058 BODY *text_body = NULL;
4059 PARAMETER *param = NULL;
4060 emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
4063 char buf[512] = {0x00, };
4066 if (!envelope || !multipart_body) {
4067 EM_DEBUG_EXCEPTION(" envelope[%p], mulitpart_body[%p]", envelope, multipart_body);
4068 err = EMAIL_ERROR_INVALID_PARAM;
4072 if (!(text_body = mail_newbody())) {
4073 EM_DEBUG_EXCEPTION(" mail_newbody failed...");
4074 err = EMAIL_ERROR_OUT_OF_MEMORY;
4078 if (!emcore_get_temp_file_name(&fname, &err)) {
4079 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed [%d]", err);
4083 err = em_fopen(fname, "wb+", &fp);
4084 if (err != EMAIL_ERROR_NONE) {
4085 EM_DEBUG_EXCEPTION("em_fopen failed - %s, error : [%d]", fname, err);
4089 if (!envelope->from || !envelope->from->mailbox || !envelope->from->host) {
4090 if (!envelope->from)
4091 EM_DEBUG_EXCEPTION(" envelope->from[%p]", envelope->from);
4093 EM_DEBUG_LOG(" envelope->from->mailbox[%p], envelope->from->host[%p]", envelope->from->mailbox, envelope->from->host);
4095 err = EMAIL_ERROR_INVALID_PARAM;
4100 if (envelope->from->personal)
4101 SNPRINTF(buf, sizeof(buf), "%s <%s@%s>", envelope->from->personal, envelope->from->mailbox, envelope->from->host);
4104 SNPRINTF(buf, sizeof(buf), "%s@%s", envelope->from->mailbox, envelope->from->host);
4106 fprintf(fp, "Your message has been read by %s"CRLF_STRING, buf);
4107 fprintf(fp, "Date : %s", envelope->date);
4109 fclose(fp); fp = NULL;
4111 if (!emcore_get_file_size(fname, &sz, &err)) {
4112 EM_DEBUG_EXCEPTION(" emcore_get_file_size failed [%d]", err);
4116 text_body->type = TYPETEXT;
4117 text_body->encoding = ENC8BIT;
4118 text_body->sparep = EM_SAFE_STRDUP(fname);
4119 text_body->size.bytes = (unsigned long)sz;
4121 if (!emcore_get_temp_file_name(&fname, &err)) {
4122 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed [%d]", err);
4126 err = em_fopen(fname, "wb+", &fp);
4127 if (err != EMAIL_ERROR_NONE) {
4128 EM_DEBUG_EXCEPTION("em_fopen failed - %s, error : [%d]", fname, err);
4132 if (!envelope->references) {
4133 EM_DEBUG_EXCEPTION(" envelope->references[%p]", envelope->references);
4134 err = EMAIL_ERROR_INVALID_PARAM;
4138 fprintf(fp, "Final-Recipient : rfc822;%s@%s\r", envelope->from->mailbox, envelope->from->host);
4139 fprintf(fp, "Original-Message-ID: %s\r", envelope->references);
4140 fprintf(fp, "Disposition : manual-action/MDN-sent-manually; displayed");
4142 fclose(fp); fp = NULL;
4144 temp_attachment_tbl = (emstorage_attachment_tbl_t *)em_malloc(sizeof(emstorage_attachment_tbl_t));
4145 if (temp_attachment_tbl == NULL) {
4146 EM_DEBUG_EXCEPTION("em_malloc failed");
4147 err = EMAIL_ERROR_OUT_OF_MEMORY;
4151 temp_attachment_tbl->attachment_path = EM_SAFE_STRDUP(fname);
4153 if (!emcore_get_file_size(fname, &temp_attachment_tbl->attachment_size, &err)) {
4154 EM_DEBUG_EXCEPTION(" emcore_get_file_size failed [%d]", err);
4158 if (!attach_attachment_to_body(multi_user_name, &m_body, text_body, temp_attachment_tbl, 1, &err)) {
4159 EM_DEBUG_EXCEPTION(" attach_attachment_to_body failed [%d]", err);
4163 text_body->contents.text.data = NULL;
4165 /* change mail header */
4167 /* set content-type to multipart/report */
4168 m_body->subtype = strdup("report");
4170 /* set report-type parameter in content-type */
4171 param = em_malloc(sizeof(PARAMETER));
4173 EM_DEBUG_EXCEPTION(" malloc failed...");
4174 err = EMAIL_ERROR_OUT_OF_MEMORY;
4178 param->attribute = strdup("report-type");
4179 param->value = strdup("disposition-notification");
4180 param->next = m_body->parameter;
4182 m_body->parameter = param;
4184 /* change body-header */
4186 p_body = &m_body->nested.part->next->body;
4188 /* set content-type to message/disposition-notification */
4189 p_body->type = TYPEMESSAGE;
4190 p_body->encoding = ENC7BIT;
4192 EM_SAFE_FREE(p_body->subtype);
4194 p_body->subtype = strdup("disposition-notification");
4197 mail_free_body_parameter(&p_body->parameter);
4198 mail_free_body_parameter(&p_body->disposition.parameter);
4200 EM_SAFE_FREE(p_body->disposition.type);
4202 p_body->disposition.type = strdup("inline");
4207 if ((ret == true) && (multipart_body != NULL))
4208 *multipart_body = m_body;
4209 else if (m_body != NULL)
4210 mail_free_body(&m_body);
4212 if (text_body != NULL)
4213 mail_free_body(&text_body);
4218 EM_SAFE_FREE(fname);
4220 if (temp_attachment_tbl)
4221 emstorage_free_attachment(&temp_attachment_tbl, 1, NULL);
4223 if (err_code != NULL)
4226 EM_DEBUG_FUNC_END("err [%d]", err);
4231 INTERNAL_FUNC int emcore_get_body_buff(char *file_path, char **buff)
4233 EM_DEBUG_FUNC_BEGIN();
4237 char *read_buff = NULL;
4240 r_fp = fopen(file_path, "r");
4243 EM_DEBUG_EXCEPTION_SEC(" Filename %s failed to open", file_path);
4248 if (stat(file_path, &stbuf) == 0 && stbuf.st_size > 0) {
4249 EM_DEBUG_LOG(" File Size [ %d ] ", stbuf.st_size);
4250 read_buff = calloc((stbuf.st_size+ 1), sizeof(char));
4251 if (read_buff == NULL) {
4252 EM_DEBUG_EXCEPTION("calloc failed");
4255 read_buff[stbuf.st_size] = '\0';
4259 EM_DEBUG_EXCEPTION_SEC("file read failed - %s", file_path);
4260 EM_SAFE_FREE(read_buff);
4274 static int emcore_copy_attachment_from_original_mail(char *multi_user_name, int input_original_mail_id, int input_target_mail_id)
4276 EM_DEBUG_FUNC_BEGIN("input_original_mail_id[%d] input_target_mail_id[%d]", input_original_mail_id, input_target_mail_id);
4277 int err = EMAIL_ERROR_NONE;
4279 int original_mail_attachment_count = 0;
4280 int target_mail_attachment_count = 0;
4281 int attachment_id = 0;
4282 char output_file_name[MAX_PATH] = { 0, };
4283 char output_file_path[MAX_PATH] = { 0, };
4284 char virtual_file_path[MAX_PATH] = { 0, };
4285 emstorage_attachment_tbl_t *original_mail_attachment_array = NULL;
4286 emstorage_attachment_tbl_t *target_mail_attachment_array = NULL;
4287 emstorage_attachment_tbl_t *target_attach = NULL;
4289 if((err = emstorage_get_attachment_list(multi_user_name, input_original_mail_id, false, &original_mail_attachment_array, &original_mail_attachment_count)) != EMAIL_ERROR_NONE) {
4290 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4294 if((err = emstorage_get_attachment_list(multi_user_name, input_target_mail_id, false, &target_mail_attachment_array, &target_mail_attachment_count)) != EMAIL_ERROR_NONE) {
4295 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4299 for(i = 0; i < original_mail_attachment_count; i++) {
4300 for(j = 0; j < target_mail_attachment_count; j++) {
4301 if(strcmp(original_mail_attachment_array[i].attachment_name, target_mail_attachment_array[j].attachment_name) == 0 ) {
4302 target_attach = target_mail_attachment_array + j;
4304 /* If attachment is inline content, fild path should not include attachment id */
4305 if(target_attach->attachment_inline_content_status == 1)
4308 attachment_id = target_attach->attachment_id;
4310 EM_DEBUG_LOG("attachment_inline_content_status [%d] attachment_id[%d]", target_attach->attachment_inline_content_status, attachment_id);
4312 if(!emcore_save_mail_file(multi_user_name,
4313 target_attach->account_id,
4314 target_attach->mail_id,
4316 original_mail_attachment_array[i].attachment_path,
4317 original_mail_attachment_array[i].attachment_name,
4321 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4325 EM_SAFE_FREE(target_attach->attachment_path);
4326 target_attach->attachment_path = EM_SAFE_STRDUP(virtual_file_path);
4327 target_attach->attachment_save_status = 1;
4329 if(!emstorage_update_attachment(multi_user_name, target_attach, false, &err)) {
4330 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
4334 memset(output_file_path, 0, MAX_PATH);
4335 memset(output_file_name, 0, MAX_PATH);
4342 if(original_mail_attachment_array)
4343 emstorage_free_attachment(&original_mail_attachment_array, original_mail_attachment_count, NULL);
4344 if(target_mail_attachment_array)
4345 emstorage_free_attachment(&target_mail_attachment_array, target_mail_attachment_count, NULL);
4348 EM_DEBUG_FUNC_END("err[%d]", err);
4353 INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(char *multi_user_name, int input_mail_id)
4355 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d]", input_mail_id);
4356 int err = EMAIL_ERROR_NONE;
4358 int attachment_count = 0;
4359 email_mail_data_t *mail_to_be_sent = NULL;
4360 email_mail_data_t *original_mail = NULL;
4361 email_attachment_data_t *attachment_array = NULL;
4364 if((err = emcore_get_mail_data(multi_user_name, input_mail_id, &mail_to_be_sent)) != EMAIL_ERROR_NONE) {
4365 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
4369 if(mail_to_be_sent->reference_mail_id <= 0) {
4370 err = EMAIL_ERROR_INVALID_REFERENCE_MAIL;
4371 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_REFERENCE_MAIL");
4375 /* Get original mail data */
4376 if((err = emcore_get_mail_data(multi_user_name, mail_to_be_sent->reference_mail_id, &original_mail)) != EMAIL_ERROR_NONE) {
4377 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
4381 /* Check necessity of download */
4382 if((err = emcore_get_attachment_data_list(multi_user_name, original_mail->mail_id, &attachment_array, &attachment_count)) != EMAIL_ERROR_NONE) {
4383 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
4387 /* If need be, download attachments */
4388 for(i = 0; i < attachment_count; i++) {
4389 if(attachment_array[i].save_status != 1) {
4390 /* this function is not run by event thread,
4391 so cancellable should be set to 0*/
4392 if(!emcore_gmime_download_attachment(multi_user_name, original_mail->mail_id, i + 1, 0, -1, 0, &err)) {
4393 EM_DEBUG_EXCEPTION("emcore_gmime_download_attachment failed [%d]", err);
4399 /* Copy attachment to the mail to be sent */
4400 if((err = emcore_copy_attachment_from_original_mail(multi_user_name, original_mail->mail_id, mail_to_be_sent->mail_id)) != EMAIL_ERROR_NONE) {
4401 EM_DEBUG_EXCEPTION("emcore_copy_attachment_from_original failed [%d]", err);
4406 if(!emcore_send_mail(multi_user_name, mail_to_be_sent->mail_id, &err)) {
4407 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
4412 if(attachment_array)
4413 emcore_free_attachment_data(&attachment_array, attachment_count, NULL);
4415 if(mail_to_be_sent) {
4416 emcore_free_mail_data(mail_to_be_sent);
4417 EM_SAFE_FREE(mail_to_be_sent);
4421 emcore_free_mail_data(original_mail);
4422 EM_SAFE_FREE(original_mail);
4426 EM_DEBUG_FUNC_END("err [%d]", err);
4430 /* Scheduled sending ------------------------------------------------ */
4432 static int emcore_sending_alarm_cb(email_alarm_data_t *alarm_data, void *user_parameter)
4434 EM_DEBUG_FUNC_BEGIN("alarm_data [%p] user_parameter [%p]", alarm_data, user_parameter);
4435 int err = EMAIL_ERROR_NONE;
4437 char *multi_user_name = (char *)user_parameter;
4439 if (alarm_data == NULL) {
4440 EM_DEBUG_EXCEPTION("Invalid parameter");
4444 /* send mail here */
4445 if(!emcore_send_mail(multi_user_name, alarm_data->reference_id, &err)) {
4446 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", ret);
4452 EM_DEBUG_FUNC_END("err [%d]", err);
4457 INTERNAL_FUNC int emcore_schedule_sending_mail(char *multi_user_name, int input_mail_id, time_t input_time_to_send)
4459 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_time_to_send[%d]", input_mail_id, input_time_to_send);
4460 int err = EMAIL_ERROR_NONE;
4461 emstorage_mail_tbl_t *mail_data = NULL;
4464 if (!emstorage_get_mail_by_id(multi_user_name, input_mail_id, &mail_data, true, &err) || mail_data == NULL) {
4465 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4470 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, mail_data->account_id, &(mail_data->mail_id), 1, "save_status", EMAIL_MAIL_STATUS_SEND_SCHEDULED, true, &err)) {
4471 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
4476 if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, mail_data->account_id, &(mail_data->mail_id), 1, "scheduled_sending_time", input_time_to_send, true, &err)) {
4477 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
4482 if ((err = emcore_add_alarm(multi_user_name, input_time_to_send, EMAIL_ALARM_CLASS_SCHEDULED_SENDING, input_mail_id, emcore_sending_alarm_cb, NULL)) != EMAIL_ERROR_NONE) {
4483 EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]", err);
4490 emstorage_free_mail(&mail_data, 1, NULL);
4492 EM_DEBUG_FUNC_END("err [%d]", err);
4495 /* Scheduled sending ------------------------------------------------ */
4497 #ifdef __FEATURE_AUTO_RETRY_SEND__
4499 static int emcore_auto_resend_cb(email_alarm_data_t *alarm_data, void *user_parameter)
4501 EM_DEBUG_FUNC_BEGIN("alarm_data [%p] user_parameter [%p]", alarm_data, user_parameter);
4502 int err = EMAIL_ERROR_NONE;
4503 char *conditional_clause_string = NULL;
4504 char *attribute_field_name = NULL;
4505 email_list_filter_t filter_list[5];
4506 email_mail_list_item_t *result_mail_list = NULL;
4507 email_list_sorting_rule_t sorting_rule_list[2];
4508 int filter_rule_count = 5;
4509 int sorting_rule_count = 2;
4510 int result_mail_count = 0;
4512 char *multi_user_name = (char *)user_parameter;
4514 /* Get all mails have remaining resend counts in outbox with status 'EMAIL_MAIL_STATUS_SEND_FAILURE or EMAIL_MAIL_STATUS_SEND_WAIT' */
4515 attribute_field_name = emcore_get_mail_field_name_by_attribute_type(EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES);
4517 memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_rule_count);
4518 memset(sorting_rule_list, 0 , sizeof(email_list_sorting_rule_t) * sorting_rule_count);
4520 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
4521 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES;
4522 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_GREATER_THAN;
4523 filter_list[0].list_filter_item.rule.key_value.integer_type_value = 0;
4525 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
4526 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
4528 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
4529 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE;
4530 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
4531 filter_list[2].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAILBOX_TYPE_OUTBOX;
4533 filter_list[3].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
4534 filter_list[3].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
4536 filter_list[4].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
4537 filter_list[4].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
4538 filter_list[4].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
4539 filter_list[4].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAIL_STATUS_SEND_FAILURE;
4541 sorting_rule_list[0].target_attribute = EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID;
4542 sorting_rule_list[0].sort_order = EMAIL_SORT_ORDER_ASCEND;
4544 sorting_rule_list[1].target_attribute = EMAIL_MAIL_ATTRIBUTE_MAIL_ID;
4545 sorting_rule_list[1].sort_order = EMAIL_SORT_ORDER_ASCEND;
4547 if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_rule_count, sorting_rule_list, sorting_rule_count, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
4548 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
4552 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
4554 if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
4555 EM_DEBUG_LOG("There is no mails to be sent [%d]", err);
4559 /* Send mails in loop */
4561 for(i = 0; i < result_mail_count; i++) {
4562 if(!emcore_send_mail(multi_user_name, result_mail_list[i].mail_id, &err)) {
4563 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
4566 if(attribute_field_name) {
4567 if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, result_mail_list[i].account_id, &(result_mail_list[i].mail_id), 1, attribute_field_name, result_mail_list[i].remaining_resend_times - 1, true, &err)) {
4568 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
4574 EM_SAFE_FREE (conditional_clause_string); /* detected by valgrind */
4575 EM_SAFE_FREE(result_mail_list);
4577 EM_DEBUG_FUNC_END("err [%d]", err);
4581 INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(char *multi_user_name, int input_alarm_interval_in_second)
4583 EM_DEBUG_FUNC_BEGIN("input_alarm_interval_in_second[%d]", input_alarm_interval_in_second);
4584 int err = EMAIL_ERROR_NONE;
4585 time_t current_time;
4586 time_t trigger_at_time;
4587 char *conditional_clause_string = NULL;
4588 email_list_filter_t filter_list[5];
4589 email_mail_list_item_t *result_mail_list = NULL;
4590 email_list_sorting_rule_t sorting_rule_list[2];
4591 int filter_rule_count = 5;
4592 int sorting_rule_count = 2;
4593 int result_mail_count = 0;
4595 if(input_alarm_interval_in_second <= 0) {
4596 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4597 err = EMAIL_ERROR_INVALID_PARAM;
4601 /* Check whether the alarm is already existing */
4602 if(emcore_check_alarm_by_class_id(EMAIL_ALARM_CLASS_AUTO_RESEND) == EMAIL_ERROR_NONE) {
4604 err = EMAIL_ERROR_ALREADY_EXISTS;
4607 /* Get all mails have remaining resend counts in outbox with status 'EMAIL_MAIL_STATUS_SEND_FAILURE or EMAIL_MAIL_STATUS_SEND_WAIT' */
4609 memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_rule_count);
4610 memset(sorting_rule_list, 0 , sizeof(email_list_sorting_rule_t) * sorting_rule_count);
4612 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
4613 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES;
4614 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_GREATER_THAN;
4615 filter_list[0].list_filter_item.rule.key_value.integer_type_value = 0;
4617 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
4618 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
4620 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
4621 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE;
4622 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
4623 filter_list[2].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAILBOX_TYPE_OUTBOX;
4625 filter_list[3].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
4626 filter_list[3].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
4628 filter_list[4].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
4629 filter_list[4].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
4630 filter_list[4].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
4631 filter_list[4].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAIL_STATUS_SEND_FAILURE;
4633 sorting_rule_list[0].target_attribute = EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID;
4634 sorting_rule_list[0].sort_order = EMAIL_SORT_ORDER_ASCEND;
4636 sorting_rule_list[1].target_attribute = EMAIL_MAIL_ATTRIBUTE_MAIL_ID;
4637 sorting_rule_list[1].sort_order = EMAIL_SORT_ORDER_ASCEND;
4639 if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_rule_count, sorting_rule_list, sorting_rule_count, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
4640 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
4644 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
4646 if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
4647 if (err == EMAIL_ERROR_MAIL_NOT_FOUND)
4648 EM_DEBUG_LOG ("no mail found");
4650 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
4654 if(result_mail_count > 0) {
4656 time(¤t_time);
4658 trigger_at_time = current_time + input_alarm_interval_in_second;
4660 if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_AUTO_RESEND, 0, emcore_auto_resend_cb, (void *)multi_user_name)) != EMAIL_ERROR_NONE) {
4661 EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
4667 EM_SAFE_FREE(result_mail_list);
4668 EM_SAFE_FREE(conditional_clause_string);
4669 EM_DEBUG_FUNC_END("err[%d]", err);
4672 #endif /* __FEATURE_AUTO_RETRY_SEND__ */