Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-smtp.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <time.h>
29 #include "email-internal-types.h"
30 #include "c-client.h"
31 #include "email-core-global.h"
32 #include "email-core-utils.h"
33 #include "email-storage.h"
34 #include "email-core-api.h"
35 #include "email-core-smtp.h"
36 #include "email-core-event.h"
37 #include "email-core-mailbox.h"
38 #include "email-core-mail.h"
39 #include "email-core-mime.h"
40 #include "email-core-smime.h"
41 #include "email-core-account.h"
42 #include "email-core-imap-mailbox.h"
43 #include "email-core-mailbox-sync.h"
44 #include "email-core-signal.h"
45 #include "email-utilities.h"
46 #include "email-convert.h"
47
48
49 #include <unistd.h>
50 #include "email-debug-log.h"
51
52 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
53 static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body, int *err_code);
54 #endif
55 static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope);
56 static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id,  int *err_code);
57
58 /* Functions from uw-imap-toolkit */
59 /* extern void *fs_get(size_t size); */
60 extern void rfc822_date(char *date);
61 extern long smtp_send(SENDSTREAM *stream, char *command, char *args);
62 extern long smtp_rcpt(SENDSTREAM *stream, ADDRESS *adr, long* error);
63
64 #ifndef __FEATURE_SEND_OPTMIZATION__
65 extern long smtp_soutr(void *stream, char *s);
66 #endif
67
68 #ifdef __FEATURE_SEND_OPTMIZATION__
69 extern long smtp_soutr_test(void *stream, char *s);
70 #endif
71
72
73
74
75 void mail_send_notify(email_send_status_t status, int total, int sent, int account_id, int mail_id,  int err_code)
76 {
77         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);
78
79         switch (status) {
80                 case EMAIL_SEND_CONNECTION_FAIL:
81                 case EMAIL_SEND_FINISH:
82                 case EMAIL_SEND_FAIL:
83                         break;
84
85                 case EMAIL_SEND_PROGRESS:
86                 default:
87                         break;
88         }
89         emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, total, sent, status, account_id, mail_id, -1, err_code);
90         EM_DEBUG_FUNC_END();
91 }
92
93 /* ------ rfc822 handle ---------------------------------------------------*/
94 long buf_flush(void *stream, char *string)
95 {
96         EM_DEBUG_FUNC_BEGIN("stream[%p], string[%s]", stream, string);
97         EM_DEBUG_FUNC_END();
98         return 1;
99 }
100
101
102 #define RFC822_READ_BLOCK_SIZE    1024
103 #define RFC822_STRING_BUFFER_SIZE 1536
104
105 static char *emcore_find_img_tag(char *source_string)
106 {
107         EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
108
109         int cur = 0, string_length;
110         if (!source_string)
111                 return false;
112
113         string_length = EM_SAFE_STRLEN(source_string);
114
115         for (cur = 0; cur < string_length; cur++) {
116                 if (source_string[cur] == 'I' || source_string[cur] == 'i') {
117                         cur++;
118                         if (source_string[cur] == 'M' || source_string[cur] == 'm') {
119                                 cur++;
120                                 if (source_string[cur] == 'G' || source_string[cur] == 'g') {
121                                         EM_DEBUG_FUNC_END("%s", source_string + cur - 2);
122                                         return source_string + cur - 2;
123                                 }
124                         }
125                 }
126         }
127         EM_DEBUG_FUNC_END("Can't find");
128         return NULL;
129 }
130
131 #define CONTENT_ID_BUFFER_SIZE 512
132 static char *emcore_replace_inline_image_path_with_content_id(char *source_string, BODY *html_body, int *err_code)
133 {
134         EM_DEBUG_FUNC_BEGIN("source_string[%p], html_body[%p], err_code[%p]", source_string, html_body, err_code);
135
136         int  err = EMAIL_ERROR_NONE;
137         char content_id_buffer[CONTENT_ID_BUFFER_SIZE], file_name_buffer[512], new_string[512], *result_string = NULL, *input_string = NULL;
138         BODY *cur_body = NULL;
139         PART *cur_part = NULL;
140
141         if (!source_string || !html_body) {
142                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
143                 err = EMAIL_ERROR_INVALID_PARAM;
144                 goto FINISH_OFF;
145         }
146
147         input_string = EM_SAFE_STRDUP(source_string);
148
149         cur_part = html_body->nested.part;
150
151         while (cur_part) {
152                 cur_body = &(cur_part->body);
153                 result_string = NULL; /*prevent 37865*/
154                 if (cur_body) {
155                         EM_DEBUG_LOG("Has body. id[%s]", cur_body->disposition.type);
156                         if (cur_body->disposition.type && cur_body->disposition.type[0] == 'i') {   /*  Is inline content? */
157                                 EM_DEBUG_LOG("Has inline content");
158                                 memset(content_id_buffer, 0, CONTENT_ID_BUFFER_SIZE);
159                                 if (cur_body->id) {
160                                         EM_SAFE_STRNCPY(content_id_buffer, cur_body->id + 1, CONTENT_ID_BUFFER_SIZE - 1); /*  Removing <, > */
161                                         /* prevent 34413 */
162                                         char *last_bracket = rindex(content_id_buffer, '>');
163                                         *last_bracket = NULL_CHAR;
164
165                                         /* if (emcore_get_attribute_value_of_body_part(cur_body->parameter, "name", file_name_buffer, CONTENT_ID_BUFFER_SIZE, false, &err)) { */
166                                         if (emcore_get_attribute_value_of_body_part(cur_body->parameter, "name", file_name_buffer, CONTENT_ID_BUFFER_SIZE, true, &err)) {
167                                                 EM_DEBUG_LOG("Content-ID[%s], filename[%s]", content_id_buffer, file_name_buffer);
168                                                 SNPRINTF(new_string, CONTENT_ID_BUFFER_SIZE, "cid:%s", content_id_buffer);
169                                                 result_string = em_replace_string(input_string, file_name_buffer, new_string);
170                                         }
171                                 }
172                         }
173                 }
174                 if (result_string) {
175                         EM_SAFE_FREE(input_string);
176                         input_string = result_string;
177                 }
178                 cur_part = cur_part->next;
179         }
180
181         if (input_string)
182                 result_string = EM_SAFE_STRDUP(input_string);
183 FINISH_OFF:
184
185         EM_SAFE_FREE(input_string);
186         if (err_code)
187                 *err_code = err;
188
189         EM_DEBUG_FUNC_END("ret[%s]", result_string);
190         return result_string;
191 }
192
193 static int emcore_write_body(BODY *body, BODY *html_body, FILE *fp, int *err_code)
194 {
195         EM_DEBUG_FUNC_BEGIN("fp[%d]", fp);
196         char *file_path = NULL;
197         char buf[RFC822_STRING_BUFFER_SIZE + 1];
198         char *img_tag_pos = NULL;
199         char *p = NULL;
200         char *replaced_string = NULL;
201         int fd, nread, nwrite, error = EMAIL_ERROR_NONE;
202         unsigned long len;
203
204         file_path = body->sparep;
205
206         if (!file_path || EM_SAFE_STRLEN(file_path) == 0)  {
207                 EM_DEBUG_LOG("There is no file path");
208                 switch (body->encoding)  {
209                         case 0:
210                                 break;
211                         default:
212                                 p = cpystr((const char *)body->contents.text.data);
213                                 len = body->contents.text.size;
214                                 break;
215                 }
216
217                 if (p)  {
218                         EM_DEBUG_LOG("p[%s]", p);
219                         fprintf(fp, "%s"CRLF_STRING CRLF_STRING, p);
220                         EM_SAFE_FREE(p);
221                 }
222
223                 EM_SAFE_FREE(body->sparep);
224                 EM_DEBUG_FUNC_END();
225                 return true;
226         }
227
228         EM_DEBUG_LOG("Opening a file[%s]", file_path);
229         fd = open(file_path, O_RDONLY);
230
231         if (fd < 0) {
232                 EM_DEBUG_EXCEPTION("open(\"%s\") failed...", file_path);
233                 return false;
234         }
235
236         while (1) {
237                 memset(&buf, 0x00, RFC822_STRING_BUFFER_SIZE + 1);
238                 nread = read(fd, buf, (body->encoding == ENCBASE64 ? 57 : RFC822_READ_BLOCK_SIZE - 2));
239
240                 if (nread <= 0)  {
241                         EM_DEBUG_LOG("Can't read anymore : nread[%d]", nread);
242                         break;
243                 }
244
245                 p = NULL;
246                 len = nread;
247
248                 /* EM_DEBUG_LOG("body->type[%d], body->subtype[%c]", body->type, body->subtype[0]); */
249
250                 if (body->type == TYPETEXT && (body->subtype && (body->subtype[0] == 'H' || body->subtype[0] == 'h'))) {
251                         EM_DEBUG_LOG("HTML Part");
252                         img_tag_pos = emcore_find_img_tag(buf);
253
254                         if (img_tag_pos) {
255                                 replaced_string = emcore_replace_inline_image_path_with_content_id(buf, html_body, &error);
256                                 if (replaced_string) {
257                                         EM_DEBUG_LOG("emcore_replace_inline_image_path_with_content_id succeeded");
258                                         strcpy(buf, replaced_string);
259                                         nread = len = EM_SAFE_STRLEN(buf);
260                                         EM_DEBUG_LOG("buf[%s], nread[%d], len[%d]", buf, nread, len);
261                                 }
262                                 else
263                                         EM_DEBUG_LOG("emcore_replace_inline_image_path_with_content_id failed[%d]", error);
264                         }
265                 }
266                 /* EM_DEBUG_LOG("body->encoding[%d]", body->encoding); */
267 //              if (body->subtype[0] != 'S' || body->subtype[0] != 's') {
268                 switch (body->encoding)  {
269                         case ENCQUOTEDPRINTABLE:
270                                 p = (char *)rfc822_8bit((unsigned char *)buf, (unsigned long)nread, (unsigned long *)&len);
271                                 break;
272                         case ENCBASE64:
273                                 p = (char *)rfc822_binary((void *)buf, (unsigned long)nread, (unsigned long *)&len);
274                                 break;
275                         default:
276                                 buf[len] = '\0';
277                                 break;
278                 }
279 //              }
280
281                 nwrite = fprintf(fp, "%s", (p ? p : buf));
282                 if (nwrite != len)  {
283                         fclose(fp);
284                         close(fd);
285                         EM_SAFE_FREE(p);
286                         EM_DEBUG_EXCEPTION("fprintf failed nwrite[%d], len[%d]", nwrite, len);
287                         return false;
288                 }
289                 EM_SAFE_FREE(p);
290         }
291
292         if (body->encoding == ENCQUOTEDPRINTABLE || body->encoding == ENCBASE64)
293                 fprintf(fp, CRLF_STRING);
294
295         fprintf(fp, CRLF_STRING);
296
297         if (body->sparep)  {
298                 free(body->sparep);
299                 body->sparep = NULL;
300         }
301
302         close(fd);
303
304         EM_DEBUG_FUNC_END();
305         return true;
306 }
307
308 static int emcore_write_rfc822_body(BODY *body, BODY *html_body, FILE *fp, int *err_code)
309 {
310         EM_DEBUG_FUNC_BEGIN("body[%p], html_body[%p], fp[%p], err_code[%p]", body, html_body, fp, err_code);
311
312         PARAMETER *param = NULL;
313         PART *part = NULL;
314         char *p = NULL, *bndry = NULL, buf[1025];
315         int error = EMAIL_ERROR_NONE;
316
317         switch (body->type)  {
318                 case TYPEMULTIPART:
319                         EM_DEBUG_LOG("body->type = TYPEMULTIPART");
320                         part = body->nested.part;
321
322                         for (param = body->parameter; param; param = param->next)  {
323                                 if (strcasecmp(param->attribute, "BOUNDARY") == 0) {
324                                         bndry = param->value;
325                                         break;
326                                 }
327                         }
328
329                         do  {
330                                 p = buf; p[0] = '\0';
331
332                                 rfc822_write_body_header(&p, &part->body);
333
334                                 fprintf(fp, "--%s"CRLF_STRING, bndry);
335                                 if (body->subtype[0] == 'S' || body->subtype[0] == 's') {
336                                         if (!emcore_write_body(body, html_body, fp, &error)) {
337                                                 EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
338                                                 return false;
339                                         }
340                                         fprintf(fp, "--%s"CRLF_STRING, bndry);
341                                 }
342
343                                 fprintf(fp, "%s"CRLF_STRING, buf);
344
345                                 emcore_write_rfc822_body(&part->body, html_body, fp, err_code);
346                         } while ((part = part->next));
347
348                         fprintf(fp, "--%s--"CRLF_STRING, bndry);
349                         break;
350
351                 default:  {
352                         EM_DEBUG_LOG("body->type is not TYPEMULTIPART");
353
354                         if (!emcore_write_body(body, html_body, fp, &error)) {
355                                 EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
356                                 return false;
357                         }
358
359                         break;
360                 }       /*  default: */
361         }
362         EM_DEBUG_FUNC_END();
363         return true;
364 }
365
366 static int emcore_write_rfc822(ENVELOPE *env, BODY *body, BODY *html_body, email_mail_priority_t input_priority, email_mail_report_t input_report_flag, char **data, int *err_code)
367 {
368         EM_DEBUG_FUNC_BEGIN("env[%p], body[%p], data[%p], err_code[%p]", env, body, data, err_code);
369
370         int ret = false;
371         int error = EMAIL_ERROR_NONE;
372
373         FILE *fp = NULL;
374         char *fname = NULL;
375         char *p = NULL;
376         size_t p_len = 0;
377
378         if (!env || !data)  {
379                 EM_DEBUG_EXCEPTION("Invalid Parameters");
380                 error = EMAIL_ERROR_INVALID_PARAM;
381                 goto FINISH_OFF;
382         }
383
384         srand(time(NULL));
385
386         rfc822_encode_body_7bit(env, body); /*  if contents.text.data isn't NULL, the data will be encoded. */
387
388         /*  FIXME : create memory map for this file */
389         p_len = (env->subject ? EM_SAFE_STRLEN(env->subject) : 0) + 8192;
390
391         if (!(p = em_malloc(p_len)))  {         /* (env->subject ? EM_SAFE_STRLEN(env->subject) : 0) + 8192))) */
392                 EM_DEBUG_EXCEPTION(" malloc failed...");
393                 error = EMAIL_ERROR_OUT_OF_MEMORY;
394                 goto FINISH_OFF;
395         }
396
397
398         RFC822BUFFER buf;
399
400         /* write at start of buffer */
401         buf.end = (buf.beg = buf.cur = p) + p_len - 1;
402         /* buf.f = NIL; */
403         buf.f = buf_flush;
404         buf.s = NIL;
405
406         /*  rfc822_output_header(&buf, env, body, NIL, T); */           /*  including BCC  */
407         rfc822_output_header(&buf, env, body, NIL, NIL);                /*  Excluding BCC */
408
409         *buf.cur = '\0';                /* tie off buffer */
410         {
411                 gchar **tokens = g_strsplit(p, "CHARSET=X-UNKNOWN", 2);
412
413                 if (g_strv_length(tokens) > 1)  {
414                         gchar *charset;
415
416                         if (body->sparep) {
417                                 charset = g_path_get_basename(body->sparep);
418                                 char *pHtml = NULL;
419                                 if (charset != NULL) {
420                                         if ((pHtml = strstr(charset, ".htm")) != NULL)
421                                                 charset[pHtml-charset] = '\0';
422                                 }
423
424                                 SNPRINTF(p, p_len, "%sCHARSET=%s%s", tokens[0], charset, tokens[1]);
425                                 g_free(charset);
426                         }
427                         else
428                                 EM_DEBUG_EXCEPTION("body->sparep is NULL");
429                 }
430
431                 g_strfreev(tokens);
432         } {
433                 gchar **tokens = g_strsplit(p, "To: undisclosed recipients: ;\015\012", 2);
434                 if (g_strv_length(tokens) > 1)
435                         SNPRINTF(p, p_len, "%s%s", tokens[0], tokens[1]);
436                 g_strfreev(tokens);
437         }
438
439
440         EM_DEBUG_LOG(" =============================================================================== "
441                 LF_STRING"%s"LF_STRING
442                 " =============================================================================== ", p);
443
444         if (EM_SAFE_STRLEN(p) > 2)
445                 *(p + EM_SAFE_STRLEN(p) - 2) = '\0';
446
447
448         if (input_report_flag)  {
449                 char buf[512] = {0x00, };
450
451                 if(input_report_flag & EMAIL_MAIL_REPORT_DSN) {
452                         /*  DSN (delivery status) */
453                         /*  change content-type */
454                         /*  Content-Type: multipart/report; */
455                         /*              report-type= delivery-status; */
456                         /*              boundary="----=_NextPart_000_004F_01C76EFF.54275C50" */
457                 }
458
459                 if(input_report_flag & EMAIL_MAIL_REPORT_MDN) {
460                         /*  MDN (read receipt) */
461                         /*  Content-Type: multipart/report; */
462                         /*              report-type= disposition-notification; */
463                         /*              boundary="----=_NextPart_000_004F_01C76EFF.54275C50" */
464                 }
465
466                 if(input_report_flag & EMAIL_MAIL_REQUEST_MDN) {
467                         /*  require read status */
468                         rfc822_address(buf, env->from);
469                         if (EM_SAFE_STRLEN(buf))
470                                 SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "Disposition-Notification-To: %s"CRLF_STRING, buf);
471                 }
472         }
473
474         if (input_priority)  {          /*  priority (1:high 3:normal 5:low) */
475                 SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-Priority: %d"CRLF_STRING, input_priority);
476
477                 switch (input_priority)  {
478                         case EMAIL_MAIL_PRIORITY_HIGH:
479                                 SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-MSMail-Priority: HIgh"CRLF_STRING);
480                                 break;
481                         case EMAIL_MAIL_PRIORITY_NORMAL:
482                                 SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-MSMail-Priority: Normal"CRLF_STRING);
483                                 break;
484                         case EMAIL_MAIL_PRIORITY_LOW:
485                                 SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-MSMail-Priority: Low"CRLF_STRING);
486                                 break;
487                 }
488         }
489
490         SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), CRLF_STRING);
491
492         if (!emcore_get_temp_file_name(&fname, &error))  {
493                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed[%d]", error);
494                 goto FINISH_OFF;
495         }
496
497         if (!(fp = fopen(fname, "w+")))  {
498                 EM_DEBUG_EXCEPTION("fopen failed[%s]", fname);
499                 error = EMAIL_ERROR_SYSTEM_FAILURE;
500                 goto FINISH_OFF;
501         }
502
503         fprintf(fp, "%s", p);
504
505         if (body)  {
506                 if (!emcore_write_rfc822_body(body, html_body, fp, &error))  {
507                         EM_DEBUG_EXCEPTION("emcore_write_rfc822_body failed[%d]", error);
508                         goto FINISH_OFF;
509                 }
510         }
511
512         ret = true;
513
514
515 FINISH_OFF:
516         if (fp != NULL)
517                 fclose(fp);
518
519 #ifdef USE_SYNC_LOG_FILE
520         emstorage_copy_file(fname, "/tmp/phone2pc.eml", false, NULL);
521 #endif
522
523         if (ret == true)
524                 *data = fname;
525         else if (fname != NULL)  {
526                 remove(fname);
527                 EM_SAFE_FREE(fname);
528         }
529
530         EM_SAFE_FREE(p);
531
532         if (err_code != NULL)
533                 *err_code = error;
534         EM_DEBUG_FUNC_END();
535         return ret;
536 }
537
538 INTERNAL_FUNC int emcore_add_mail(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)
539 {
540         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);
541
542         int err = EMAIL_ERROR_NONE;
543         int attachment_id = 0, thread_id = -1, thread_item_count = 0, latest_mail_id_in_thread = -1;
544         int i = 0, rule_len, rule_matched = -1, local_attachment_count = 0, local_inline_content_count = 0;
545         int mailbox_id_spam = 0, mailbox_id_target = 0;
546         char *ext = NULL;
547         char name_buf[MAX_PATH] = {0x00, };
548         char *body_text_file_name = NULL;
549
550         int attachment_count = 0;
551         email_mail_data_t *mail_data = NULL;
552         email_attachment_data_t *attachment_data_list = NULL;
553         emstorage_mail_tbl_t    *converted_mail_tbl = NULL;
554         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
555         emstorage_attachment_tbl_t attachment_tbl = { 0 };
556         emstorage_account_tbl_t *account_tbl_item = NULL;
557         emstorage_rule_tbl_t *rule = NULL;
558         struct stat st_buf = { 0 };
559         char mailbox_id_param_string[10] = {0,};
560
561         /* Validating parameters */
562         if (!input_mail_data || !(input_mail_data->account_id) || !(input_mail_data->mailbox_id))  {
563                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
564                 err = EMAIL_ERROR_INVALID_PARAM;
565                 goto FINISH_OFF;
566         }
567
568         if (emcore_is_storage_full(&err) == true) {
569                 EM_DEBUG_EXCEPTION("Storage is full");
570                 goto FINISH_OFF;
571         }
572
573         if (!emstorage_get_account_by_id(input_mail_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_item, true, &err)) {
574                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed. account_id[%d] err[%d]", input_mail_data->account_id, err);
575                 err = EMAIL_ERROR_INVALID_ACCOUNT;
576                 goto FINISH_OFF;
577         }
578
579         if (input_from_eas == 0 && input_mail_data->smime_type && input_mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_DRAFT) {
580                 if (!emcore_convert_mail_data_to_smime_data(account_tbl_item, input_mail_data, input_attachment_data_list, input_attachment_count, &mail_data, &attachment_data_list, &attachment_count)) {
581                         EM_DEBUG_EXCEPTION("S/MIME failed");
582                         goto FINISH_OFF;
583                 }
584         } else {
585                 mail_data = input_mail_data;
586                 attachment_data_list = input_attachment_data_list;
587                 attachment_count = input_attachment_count;
588         }
589
590         mailbox_id_target = mail_data->mailbox_id;
591
592         if (input_from_eas == 0 &&
593                         !(input_mail_data->message_class & EMAIL_MESSAGE_CLASS_SMART_REPLY) &&
594                         !(input_mail_data->message_class & EMAIL_MESSAGE_CLASS_SMART_FORWARD) ) {
595                 if (mail_data->file_path_plain)  {
596                         if (stat(mail_data->file_path_plain, &st_buf) < 0)  {
597                                 EM_DEBUG_EXCEPTION("mail_data->file_path_plain, stat(\"%s\") failed...", mail_data->file_path_plain);
598                                 err = EMAIL_ERROR_INVALID_MAIL;
599                                 goto FINISH_OFF;
600                         }
601                 }
602
603                 if (mail_data->file_path_html)  {
604                         if (stat(mail_data->file_path_html, &st_buf) < 0)  {
605                                 EM_DEBUG_EXCEPTION("mail_data->file_path_html, stat(\"%s\") failed...", mail_data->file_path_html);
606                                 err = EMAIL_ERROR_INVALID_MAIL;
607                                 goto FINISH_OFF;
608                         }
609                 }
610
611                 if (attachment_count && attachment_data_list)  {
612                         for (i = 0; i < attachment_count; i++)  {
613                                 if (attachment_data_list[i].save_status) {
614                                         if (!attachment_data_list[i].attachment_path || stat(attachment_data_list[i].attachment_path, &st_buf) < 0)  {
615                                                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", attachment_data_list[i].attachment_path);
616                                                 err = EMAIL_ERROR_INVALID_ATTACHMENT;
617                                                 goto FINISH_OFF;
618                                         }
619                                 }
620                         }
621                 }
622
623                 if (!input_mail_data->full_address_from)
624                         input_mail_data->full_address_from = EM_SAFE_STRDUP(account_tbl_item->user_email_address);
625
626                 /* check for email_address validation */
627                 if (!em_verify_email_address_of_mail_data(mail_data, false, &err)) {
628                         EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_data failed [%d]", err);
629                         goto FINISH_OFF;
630                 }
631
632                 if (mail_data->report_status & EMAIL_MAIL_REPORT_MDN)  {
633                         /* check read-report mail */
634                         if(!mail_data->full_address_to) { /* A report mail should have 'to' address */
635                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
636                                 err = EMAIL_ERROR_INVALID_PARAM;
637                                 goto FINISH_OFF;
638                         }
639                         /* Create report mail body */
640                         /*
641                         if (!emcore_make_rfc822_file_from_mail(mail_src, NULL, NULL, NULL, &err))  {
642                                 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
643                                 goto FINISH_OFF;
644                         }
645                         */
646                 }
647         }
648         else {  /*  For Spam handling */
649                 email_option_t *opt = &account_tbl_item->options;
650                 EM_DEBUG_LOG("block_address [%d], block_subject [%d]", opt->block_address, opt->block_subject);
651
652                 if (opt->block_address || opt->block_subject)  {
653                         int is_completed = false;
654                         int type = 0;
655
656                         if (!opt->block_address)
657                                 type = EMAIL_FILTER_SUBJECT;
658                         else if (!opt->block_subject)
659                                 type = EMAIL_FILTER_FROM;
660
661                         if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_len, &is_completed, &rule, true, &err) || !rule)
662                                 EM_DEBUG_LOG("No proper rules. emstorage_get_rule returns [%d]", err);
663                 }
664
665                 if (rule) {
666                         if (!emstorage_get_mailbox_id_by_mailbox_type(mail_data->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_id_spam, false, &err))  {
667                                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
668                                 mailbox_id_spam = 0;
669                         }
670
671                         if (mailbox_id_spam && !emcore_check_rule(mail_data->full_address_from, mail_data->subject, rule, rule_len, &rule_matched, &err))  {
672                                 EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
673                                 goto FINISH_OFF;
674                         }
675                 }
676
677                 if (rule_matched >= 0 && mailbox_id_spam)
678                         mailbox_id_target = mailbox_id_spam;
679         }
680
681         if ((err = emstorage_get_mailbox_by_id(mailbox_id_target, (emstorage_mailbox_tbl_t**)&mailbox_tbl)) != EMAIL_ERROR_NONE) {
682                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
683                 goto FINISH_OFF;
684         }
685
686         if (!emstorage_increase_mail_id(&mail_data->mail_id, true, &err)) {
687                 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
688                 goto FINISH_OFF;
689         }
690
691         EM_DEBUG_LOG("mail_data->mail_size [%d]", mail_data->mail_size);
692
693         if(mail_data->mail_size == 0)
694                 emcore_calc_mail_size(mail_data, attachment_data_list, attachment_count, &(mail_data->mail_size)); /*  Getting file size before file moved.  */
695
696         EM_DEBUG_LOG("input_from_eas [%d] mail_data->body_download_status [%d]", input_from_eas, mail_data->body_download_status);
697
698         if (input_from_eas == 0|| mail_data->body_download_status) {
699                 if (!emstorage_create_dir(mail_data->account_id, mail_data->mail_id, 0, &err))  {
700                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
701                         goto FINISH_OFF;
702                 }
703
704                 if (mail_data->file_path_plain) {
705                         EM_DEBUG_LOG("mail_data->file_path_plain [%s]", mail_data->file_path_plain);
706                         /* EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH); */
707
708                         if ( (err = em_get_file_name_from_file_path(mail_data->file_path_plain, &body_text_file_name)) != EMAIL_ERROR_NONE) {
709                                 EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
710                                 err = EMAIL_ERROR_INVALID_FILE_PATH;
711                                 goto FINISH_OFF;
712                         }
713
714                         /*
715                         if (input_from_eas)
716                                 EM_SAFE_STRNCPY(body_text_file_name, UNKNOWN_CHARSET_PLAIN_TEXT_FILE, MAX_PATH);
717                         else
718                                 EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH);
719                         */
720
721                         if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, 0, body_text_file_name, name_buf, &err))  {
722                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
723                                 goto FINISH_OFF;
724                         }
725
726                         if (!emstorage_move_file(mail_data->file_path_plain, name_buf, input_from_eas, &err)) {
727                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
728                                 goto FINISH_OFF;
729                         }
730                         if (mail_data->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_NONE)
731                                 mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
732
733                         EM_SAFE_FREE(mail_data->file_path_plain);
734                         mail_data->file_path_plain = EM_SAFE_STRDUP(name_buf);
735                 }
736
737                 if (mail_data->file_path_html) {
738                         EM_DEBUG_LOG("mail_data->file_path_html [%s]", mail_data->file_path_html);
739                         /* EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH); */
740
741                         EM_SAFE_FREE(body_text_file_name);
742
743                         if ( (err = em_get_file_name_from_file_path(mail_data->file_path_html, &body_text_file_name)) != EMAIL_ERROR_NONE) {
744                                 EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
745                                 err = EMAIL_ERROR_INVALID_FILE_PATH;
746                                 goto FINISH_OFF;
747                         }
748                         /*
749                         if (input_from_eas)
750                                 EM_SAFE_STRNCPY(body_text_file_name, UNKNOWN_CHARSET_HTML_TEXT_FILE, MAX_PATH);
751                         else
752                                 EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH);
753                         */
754
755                         if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, 0, body_text_file_name, name_buf, &err))  {
756                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
757                                 goto FINISH_OFF;
758                         }
759
760                         if (!emstorage_move_file(mail_data->file_path_html, name_buf, input_from_eas, &err))  {
761                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
762                                 goto FINISH_OFF;
763                         }
764
765                         if (mail_data->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_NONE)
766                                 mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
767
768                         EM_SAFE_FREE(mail_data->file_path_html);
769                         mail_data->file_path_html = EM_SAFE_STRDUP(name_buf);
770                 }
771         }
772
773         if (mail_data->file_path_mime_entity) {
774                 EM_DEBUG_LOG("mail_data->file_path_mime_entity [%s]", mail_data->file_path_mime_entity);
775
776                 if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, 0, "mime_entity", name_buf, &err)) {
777                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
778                         goto FINISH_OFF;
779                 }
780
781                 if (!emstorage_move_file(mail_data->file_path_mime_entity, name_buf, input_from_eas, &err)) {
782                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
783                         goto FINISH_OFF;
784                 }
785
786                 EM_SAFE_FREE(mail_data->file_path_mime_entity);
787                 mail_data->file_path_mime_entity = EM_SAFE_STRDUP(name_buf);
788         }
789
790         if (!mail_data->date_time)  {
791                 /* time isn't set */
792                 mail_data->date_time = time(NULL);
793         }
794
795
796         mail_data->mailbox_id           = mailbox_id_target;
797         mail_data->mailbox_type         = mailbox_tbl->mailbox_type;
798         mail_data->server_mail_status   = !input_from_eas;
799         mail_data->save_status          = EMAIL_MAIL_STATUS_SAVED;
800
801         /*  Getting attachment count */
802         for (i = 0; i < attachment_count; i++) {
803                 if (attachment_data_list[i].inline_content_status== 1)
804                         local_inline_content_count++;
805                 local_attachment_count++;
806         }
807
808         mail_data->inline_content_count = local_inline_content_count;
809         mail_data->attachment_count     = local_attachment_count;
810
811         EM_DEBUG_LOG("inline_content_count   [%d]", local_inline_content_count);
812         EM_DEBUG_LOG("attachment_count [%d]", local_attachment_count);
813
814         EM_DEBUG_LOG("preview_text[%p]", mail_data->preview_text);
815         if (mail_data->preview_text == NULL) {
816                 if ( (err = emcore_get_preview_text_from_file(mail_data->file_path_plain, mail_data->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail_data->preview_text))) != EMAIL_ERROR_NONE) {
817                         EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
818
819                         if (err != EMAIL_ERROR_EMPTY_FILE)
820                                 goto FINISH_OFF;
821                 }
822         }
823
824         if (!em_convert_mail_data_to_mail_tbl(mail_data, 1, &converted_mail_tbl, &err)) {
825                 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed [%d]", err);
826                 goto FINISH_OFF;
827         }
828
829         converted_mail_tbl->mailbox_id        = mailbox_tbl->mailbox_id;
830         converted_mail_tbl->mailbox_name      = EM_SAFE_STRDUP(mailbox_tbl->mailbox_name);
831
832         /* Fill address information */
833         emcore_fill_address_information_of_mail_tbl(converted_mail_tbl);
834
835         /* Fill thread id */
836         if(mail_data->thread_id == 0) {
837                 if (emstorage_get_thread_id_of_thread_mails(converted_mail_tbl, &thread_id, &latest_mail_id_in_thread, &thread_item_count) != EMAIL_ERROR_NONE)
838                         EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed");
839
840                 if (thread_id == -1) {
841                         converted_mail_tbl->thread_id         = mail_data->mail_id;
842                         converted_mail_tbl->thread_item_count = thread_item_count = 1;
843                 }
844                 else  {
845                         converted_mail_tbl->thread_id         = thread_id;
846                         thread_item_count++;
847                 }
848         }
849         else {
850                 thread_item_count                         = 2;
851         }
852
853         mail_data->thread_id = converted_mail_tbl->thread_id;
854
855         emstorage_begin_transaction(NULL, NULL, NULL);
856
857         /*  insert mail to mail table */
858         if (!emstorage_add_mail(converted_mail_tbl, 0, false, &err))  {
859                 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
860                 /*  ROLLBACK TRANSACTION; */
861                 emstorage_rollback_transaction(NULL, NULL, NULL);
862
863                 goto FINISH_OFF;
864         }
865
866         /* Update thread information */
867         EM_DEBUG_LOG("thread_item_count [%d]", thread_item_count);
868
869         if (thread_item_count > 1) {
870                 if (!emstorage_update_latest_thread_mail(mail_data->account_id, converted_mail_tbl->thread_id, 0, 0, false, &err)) {
871                         EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
872                         emstorage_rollback_transaction(NULL, NULL, NULL);
873
874                         goto FINISH_OFF;
875                 }
876         }
877
878         /*  Insert attachment information to DB */
879
880         for (i = 0; i < attachment_count; i++) {
881                 if (attachment_data_list[i].attachment_size == 0) {
882                         /* set attachment size */
883                         if(attachment_data_list[i].attachment_path && stat(attachment_data_list[i].attachment_path, &st_buf) < 0)
884                                 attachment_data_list[i].attachment_size = st_buf.st_size;
885                 }
886
887                 if (!attachment_data_list[i].inline_content_status) {
888                         if (!emstorage_get_new_attachment_no(&attachment_id, &err)) {
889                                 EM_DEBUG_EXCEPTION("emstorage_get_new_attachment_no failed [%d]", err);
890                                 emstorage_rollback_transaction(NULL, NULL, NULL);
891                                 goto FINISH_OFF;
892                         }
893                 }
894
895                 if (!emstorage_create_dir(mail_data->account_id, mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, &err))  {
896                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
897                         emstorage_rollback_transaction(NULL, NULL, NULL);
898                         goto FINISH_OFF;
899                 }
900
901                 if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, attachment_data_list[i].attachment_name, name_buf, &err))  {
902                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
903                         emstorage_rollback_transaction(NULL, NULL, NULL);
904                         goto FINISH_OFF;
905                 }
906                 /* if (input_from_eas == 0 || attachment_data_list[i].save_status) { */
907                 if (attachment_data_list[i].save_status) {
908                         if (!emstorage_copy_file(attachment_data_list[i].attachment_path, name_buf, input_from_eas, &err))  {
909                                 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
910                                 emstorage_rollback_transaction(NULL, NULL, NULL);
911                                 goto FINISH_OFF;
912                         }
913
914                         if ((ext = strrchr(attachment_data_list[i].attachment_name, '.'))) {
915                                 if (!strncmp(ext, ".vcs", strlen(".vcs")))
916                                         remove(attachment_data_list[i].attachment_path);
917                                 else if (!strncmp(ext, ".vcf", strlen(".vcf")))
918                                         remove(attachment_data_list[i].attachment_path);
919                                 else if (!strncmp(ext, ".vnt", strlen(".vnt")))
920                                         remove(attachment_data_list[i].attachment_path);
921                         }
922                 }
923
924                 memset(&attachment_tbl, 0, sizeof(emstorage_attachment_tbl_t));
925                 attachment_tbl.attachment_name                  = attachment_data_list[i].attachment_name;
926                 attachment_tbl.attachment_path                  = name_buf;
927                 attachment_tbl.attachment_size                  = attachment_data_list[i].attachment_size;
928                 attachment_tbl.mail_id                          = mail_data->mail_id;
929                 attachment_tbl.account_id                       = mail_data->account_id;
930                 attachment_tbl.mailbox_id                       = mail_data->mailbox_id;
931                 attachment_tbl.attachment_save_status           = attachment_data_list[i].save_status;
932                 attachment_tbl.attachment_drm_type              = attachment_data_list[i].drm_status;
933                 attachment_tbl.attachment_inline_content_status = attachment_data_list[i].inline_content_status;
934
935                 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
936                         EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
937                         emstorage_rollback_transaction(NULL, NULL, NULL);
938
939                         goto FINISH_OFF;
940                 }
941                 attachment_data_list[i].attachment_id = attachment_tbl.attachment_id;
942         }
943
944         /*  Insert Meeting request to DB */
945         if (mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
946                 || mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
947                 || mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
948                 EM_DEBUG_LOG("This mail has the meeting request");
949                 input_meeting_request->mail_id = mail_data->mail_id;
950                 if (!emstorage_add_meeting_request(mail_data->account_id, mailbox_tbl->mailbox_id, input_meeting_request, false, &err))  {
951                         EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
952
953                         goto FINISH_OFF;
954                 }
955         }
956
957         emstorage_commit_transaction(NULL, NULL, NULL);
958
959         SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl->mailbox_id);
960         if (!emcore_notify_storage_event(NOTI_MAIL_ADD, converted_mail_tbl->account_id, converted_mail_tbl->mail_id, mailbox_id_param_string, converted_mail_tbl->thread_id))
961                 EM_DEBUG_LOG("emcore_notify_storage_event [NOTI_MAIL_ADD] failed.");
962
963         if (account_tbl_item->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
964                 if (!emcore_remove_overflowed_mails(mailbox_tbl, &err)) {
965                         if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
966                                 err = EMAIL_ERROR_NONE;
967                         else
968                                 EM_DEBUG_LOG("emcore_remove_overflowed_mails failed [%d]", err);
969                 }
970         }
971
972         if ( input_from_eas && (mail_data->flags_seen_field == 0)
973                                 && mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_TRASH
974                                 && mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX) {
975                 if ((err = emcore_update_sync_status_of_account(mail_data->account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING | SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
976                                 EM_DEBUG_LOG("emcore_update_sync_status_of_account failed [%d]", err);
977 //              emcore_add_notification_for_unread_mail(converted_mail_tbl);
978                 emcore_display_unread_in_badge();
979         }
980
981 FINISH_OFF:
982
983         EM_SAFE_FREE(body_text_file_name);
984
985         if (account_tbl_item)
986                 emstorage_free_account(&account_tbl_item, 1, NULL);
987
988         if (mailbox_tbl)
989                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
990
991         if (converted_mail_tbl)
992                 emstorage_free_mail(&converted_mail_tbl, 1, NULL);
993
994         EM_DEBUG_FUNC_END();
995         return err;
996 }
997
998 INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id)
999 {
1000         EM_DEBUG_FUNC_BEGIN("input_read_mail_id [%d], output_receipt_mail_id [%p]", input_read_mail_id, output_receipt_mail_id);
1001         int                      err = EMAIL_ERROR_NONE;
1002         int                      attachment_count = 0;
1003         ENVELOPE                *envelope = NULL;
1004         email_mail_data_t       *read_mail_data = NULL;
1005         email_mail_data_t       *receipt_mail_data = NULL;
1006         emstorage_mail_tbl_t    *receipt_mail_tbl_data = NULL;
1007         email_attachment_data_t *attachment_data = NULL;
1008         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1009         BODY                    *root_body = NULL;
1010
1011         if( (err = emcore_get_mail_data(input_read_mail_id, &read_mail_data)) != EMAIL_ERROR_NONE) {
1012                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
1013                 goto FINISH_OFF;
1014         }
1015
1016         receipt_mail_data = em_malloc(sizeof(email_mail_data_t));
1017
1018         if (!receipt_mail_data)  {
1019                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1020                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1021                 goto FINISH_OFF;
1022         }
1023
1024         memcpy(receipt_mail_data, read_mail_data, sizeof(email_mail_data_t));
1025
1026         receipt_mail_data->full_address_to = EM_SAFE_STRDUP(read_mail_data->full_address_from);
1027         receipt_mail_data->message_id      = EM_SAFE_STRDUP(read_mail_data->message_id);
1028
1029         if (read_mail_data->subject)  {
1030                 receipt_mail_data->subject = em_malloc(EM_SAFE_STRLEN(read_mail_data->subject) + 7);
1031                 if (!(receipt_mail_data->subject))  {
1032                         EM_DEBUG_EXCEPTION("em_malloc failed...");
1033                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1034                         goto FINISH_OFF;
1035                 }
1036
1037                 SNPRINTF(receipt_mail_data->subject, EM_SAFE_STRLEN(read_mail_data->subject) + 7,  "Read: %s", read_mail_data->subject);
1038         }
1039
1040         if (!emstorage_get_mailbox_by_mailbox_type(receipt_mail_data->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox_tbl, true, &err))  {
1041                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
1042                 goto FINISH_OFF;
1043         }
1044
1045         receipt_mail_data->mailbox_id           = mailbox_tbl->mailbox_id;
1046         receipt_mail_data->mailbox_type         = EMAIL_MAILBOX_TYPE_OUTBOX;
1047         receipt_mail_data->file_path_html       = NULL;
1048         receipt_mail_data->flags_draft_field    = 1;
1049         receipt_mail_data->body_download_status = 1;
1050         receipt_mail_data->save_status          = (unsigned char)EMAIL_MAIL_STATUS_SENDING;
1051         receipt_mail_data->report_status        = (unsigned char)EMAIL_MAIL_REPORT_MDN;
1052
1053         if (!em_convert_mail_data_to_mail_tbl(receipt_mail_data, 1, &receipt_mail_tbl_data, &err)) {
1054                 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed [%d]", err);
1055                 goto FINISH_OFF;
1056         }
1057
1058         if ( (err = emcore_make_envelope_from_mail(receipt_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
1059                 EM_DEBUG_EXCEPTION("emcore_make_envelope_from_mail failed [%d]", err);
1060                 goto FINISH_OFF;
1061         }
1062
1063         envelope->references = EM_SAFE_STRDUP(read_mail_data->message_id);
1064
1065         if (!emcore_get_report_mail_body(envelope, &root_body, &err))  {
1066                 EM_DEBUG_EXCEPTION("emcore_get_report_mail_body failed [%d]", err);
1067                 goto FINISH_OFF;
1068         }
1069
1070         receipt_mail_data->file_path_plain  = EM_SAFE_STRDUP(root_body->nested.part->body.sparep);
1071
1072         /* Report attachment */
1073         /* Final-Recipient :  rfc822;digipop@gmail.com
1074            Original-Message-ID:  <r97a77ag0jdhkvvxke58u9i5.1345611508570@email.android.com>
1075            Disposition :  manual-action/MDN-sent-manually; displayed */
1076
1077         /*
1078         receipt_mail_data->attachment_count = 1;
1079         attachment_count                    = 1;
1080
1081         attachment_data = em_malloc(sizeof(email_attachment_data_t));
1082         if (!attachment_data)  {
1083                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1084                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1085                 goto FINISH_OFF;
1086         }
1087
1088         attachment_data->save_status     = 1;
1089         attachment_data->attachment_path = EM_SAFE_STRDUP(root_body->nested.part->next->body.sparep);
1090
1091         if (!emcore_get_file_name(attachment_data->attachment_path, &p, &err))  {
1092                 EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", err);
1093                 goto FINISH_OFF;
1094         }
1095
1096         attachment_data->attachment_name = cpystr(p);
1097         */
1098
1099         if ( (err = emcore_add_mail(receipt_mail_data, attachment_data, attachment_count, NULL, 0)) != EMAIL_ERROR_NONE) {
1100                 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
1101                 goto FINISH_OFF;
1102         }
1103
1104         *output_receipt_mail_id = receipt_mail_data->mail_id;
1105
1106 FINISH_OFF:
1107         if(receipt_mail_data) {
1108                 EM_SAFE_FREE(receipt_mail_data->full_address_to);
1109                 EM_SAFE_FREE(receipt_mail_data->message_id);
1110                 EM_SAFE_FREE(receipt_mail_data->subject);
1111                 EM_SAFE_FREE(receipt_mail_data);
1112         }
1113
1114         if(mailbox_tbl)
1115                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1116
1117         if(read_mail_data)
1118                 emcore_free_mail_data_list(&read_mail_data, 1);
1119
1120         if(attachment_data)
1121                 emcore_free_attachment_data(&attachment_data, 1, NULL);
1122
1123         if(receipt_mail_tbl_data)
1124                 emstorage_free_mail(&receipt_mail_tbl_data, 1, NULL);
1125
1126         EM_DEBUG_FUNC_END("err [%d]", err);
1127         return err;
1128 }
1129
1130 INTERNAL_FUNC int emcore_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code)
1131 {
1132         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);
1133         int ret = false;
1134         int err = EMAIL_ERROR_NONE;
1135
1136         if (!meeting_req || meeting_req->mail_id <= 0) {
1137                 if (meeting_req)
1138                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
1139
1140                 err = EMAIL_ERROR_INVALID_PARAM;
1141                 goto FINISH_OFF;
1142         }
1143
1144         if (!emstorage_add_meeting_request(account_id, input_mailbox_id, meeting_req, 1, &err))  {
1145                 EM_DEBUG_EXCEPTION(" emstorage_add_meeting_request failed [%d]", err);
1146
1147                 goto FINISH_OFF;
1148         }
1149
1150         ret = true;
1151
1152 FINISH_OFF:
1153
1154         if (err_code)
1155                 *err_code = err;
1156
1157         EM_DEBUG_FUNC_END();
1158         return ret;
1159 }
1160
1161 /*  send a mail */
1162 INTERNAL_FUNC int emcore_send_mail(int account_id, int input_mailbox_id, int mail_id, int *err_code)
1163 {
1164         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], mail_id[%d], err_code[%p]", account_id, input_mailbox_id, mail_id, err_code);
1165         EM_PROFILE_BEGIN(profile_emcore_send_mail);
1166         int ret = false;
1167         int err = EMAIL_ERROR_NONE, err2 = EMAIL_ERROR_NONE;
1168         int status = EMAIL_SEND_FAIL;
1169         int attachment_tbl_count = 0;
1170         int i = 0;
1171         SENDSTREAM *stream = NULL;
1172         ENVELOPE *envelope = NULL;
1173         sslstart_t stls = NULL;
1174         emstorage_mail_tbl_t       *mail_tbl_data = NULL;
1175         emstorage_attachment_tbl_t *attachment_tbl_data = NULL;
1176         email_account_t *ref_account = NULL;
1177         email_option_t *opt = NULL;
1178         void *tmp_stream = NULL;
1179         char *fpath = NULL;
1180         int sent_box = 0;
1181         emstorage_mailbox_tbl_t* local_mailbox = NULL;
1182         int dst_mailbox_id = 0;
1183
1184         if (!account_id || !mail_id)  {
1185                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1186                 err = EMAIL_ERROR_INVALID_PARAM;
1187                 goto FINISH_OFF;
1188         }
1189
1190         if (!(ref_account = emcore_get_account_reference(account_id))) {
1191                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1192                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1193                 goto FINISH_OFF;
1194         }
1195
1196         /*  get mail to send */
1197         if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err) || err != EMAIL_ERROR_NONE) {
1198                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1199                 goto FINISH_OFF;
1200         }
1201
1202         if ((err = emstorage_get_attachment_list(mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
1203                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
1204                 goto FINISH_OFF;
1205         }
1206
1207         if (!emcore_check_send_mail_thread_status()) {
1208                 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1209                 err = EMAIL_ERROR_CANCELLED;
1210                 goto FINISH_OFF;
1211         }
1212
1213         if ((!mail_tbl_data->full_address_to) && (!mail_tbl_data->full_address_cc) && (!mail_tbl_data->full_address_bcc)) {
1214                 err = EMAIL_ERROR_NO_RECIPIENT;
1215                 EM_DEBUG_EXCEPTION("No Recipient information [%d]", err);
1216                 goto FINISH_OFF;
1217         }
1218         else {
1219                 if ((err = em_verify_email_address_of_mail_tbl(mail_tbl_data, false)) != EMAIL_ERROR_NONE) {
1220                         err = EMAIL_ERROR_INVALID_ADDRESS;
1221                         EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed [%d]", err);
1222                         goto FINISH_OFF;
1223                 }
1224         }
1225
1226         if (!emcore_check_send_mail_thread_status()) {
1227                 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1228                 err = EMAIL_ERROR_CANCELLED;
1229                 goto FINISH_OFF;
1230         }
1231
1232         opt = &(ref_account->options);
1233
1234 #ifdef __FEATURE_SUPPORT_VALIDATION_SYSTEM__
1235         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);
1236         for (i = 0; i < attachment_tbl_count; i++) {
1237                 if(attachment_tbl_data)
1238                         EM_VALIDATION_SYSTEM_LOG("FILE", mail_id, "[%s], %d", attachment_tbl_data[i].attachment_path, attachment_tbl_data[i].attachment_size);
1239         }
1240 #endif /* __FEATURE_SUPPORT_VALIDATION_SYSTEM__ */
1241
1242         /*Update status flag to DB*/
1243
1244         /*  get rfc822 data */
1245         if (!emcore_make_rfc822_file_from_mail(mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err)) {
1246                 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
1247                 goto FINISH_OFF;
1248         }
1249
1250         if (!envelope || (!envelope->to && !envelope->cc && !envelope->bcc))  {
1251                 EM_DEBUG_EXCEPTION(" no recipients found...");
1252                 err = EMAIL_ERROR_NO_RECIPIENT;
1253                 goto FINISH_OFF;
1254         }
1255
1256         /*  if there is no security option, unset security. */
1257         if (!ref_account->outgoing_server_secure_connection)  {
1258                 stls = (sslstart_t)mail_parameters(NULL, GET_SSLSTART, NULL);
1259                 mail_parameters(NULL, SET_SSLSTART, NULL);
1260         }
1261
1262         if (!emcore_check_send_mail_thread_status()) {
1263                 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1264                 err = EMAIL_ERROR_CANCELLED;
1265                 goto FINISH_OFF;
1266         }
1267
1268         if (ref_account->pop_before_smtp != FALSE)  {
1269                 if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err))  {
1270                         EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
1271                         status = EMAIL_LIST_CONNECTION_FAIL;
1272                         if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1273                                 err = EMAIL_ERROR_CANCELLED;
1274                         goto FINISH_OFF;
1275                 }
1276         }
1277         if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_DRAFT, &local_mailbox, false, &err))  {
1278                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1279                 goto FINISH_OFF;
1280         }
1281         dst_mailbox_id = local_mailbox->mailbox_id;
1282
1283         if(local_mailbox)
1284                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1285
1286
1287         if (!emcore_connect_to_remote_mailbox(account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err))  {
1288                 EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
1289
1290                 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1291                         err = EMAIL_ERROR_CANCELLED;
1292                 status = EMAIL_SEND_CONNECTION_FAIL;
1293                 goto FINISH_OFF;
1294         }
1295
1296         stream = (SENDSTREAM *)tmp_stream;
1297
1298         if (!emcore_check_send_mail_thread_status()) {
1299                 EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
1300                 err = EMAIL_ERROR_CANCELLED;
1301                 goto FINISH_OFF;
1302         }
1303
1304         /*  set request of delivery status. */
1305         EM_DEBUG_LOG("opt->req_delivery_receipt [%d]", opt->req_delivery_receipt);
1306         EM_DEBUG_LOG("mail_tbl_data->report_status [%d]", mail_tbl_data->report_status);
1307
1308         if (opt->req_delivery_receipt == EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON || (mail_tbl_data->report_status & EMAIL_MAIL_REQUEST_DSN))  {
1309                 EM_DEBUG_LOG("DSN is required.");
1310                 stream->protocol.esmtp.dsn.want = 1;
1311                 stream->protocol.esmtp.dsn.full = 0;
1312                 stream->protocol.esmtp.dsn.notify.failure = 1;
1313                 stream->protocol.esmtp.dsn.notify.success = 1;
1314         }
1315
1316         mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
1317
1318         /*Update status save_status to DB*/
1319         if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, false, &err))
1320                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
1321
1322         /*  send mail to server. */
1323         if (!emcore_send_mail_smtp(stream, envelope, fpath, account_id, mail_id, &err)) {
1324                 EM_DEBUG_EXCEPTION(" emcore_send_mail_smtp failed [%d]", err);
1325 #ifndef __FEATURE_MOVE_TO_OUTBOX_FIRST__
1326                 if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, false, &err))  {
1327                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1328                         goto FINISH_OFF;
1329                 }
1330                 dst_mailbox_id = local_mailbox->mailbox_id;
1331
1332                 if(local_mailbox)
1333                         emstorage_free_mailbox(&local_mailbox, 1, NULL);
1334
1335                 /*  unsent mail is moved to 'OUTBOX'. */
1336                 if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, NULL))
1337                         EM_DEBUG_EXCEPTION(" emcore_mail_move falied...");
1338 #endif
1339                 goto FINISH_OFF;
1340         }
1341
1342         /*  sent mail is moved to 'SENT' box or deleted. */
1343         if (opt->keep_local_copy)  {
1344                 if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
1345                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1346                         goto FINISH_OFF;
1347                 }
1348                 dst_mailbox_id = local_mailbox->mailbox_id;
1349
1350                 if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
1351                         EM_DEBUG_EXCEPTION(" emcore_mail_move falied [%d]", err);
1352 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1353 #ifdef __FEATURE_LOCAL_ACTIVITY__
1354                 else if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) /* To be synced to Sent box only IMAP not for POP */ {
1355
1356                         emstorage_activity_tbl_t new_activity;
1357                         int activityid = 0;
1358
1359                         if (false == emcore_get_next_activity_id(&activityid, &err)) {
1360                                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed [%d] ", err);
1361                         }
1362
1363                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
1364                         new_activity.activity_id  =  activityid;
1365                         new_activity.server_mailid = NULL;
1366                         new_activity.account_id = account_id;
1367                         new_activity.mail_id    = mail_id;
1368                         new_activity.activity_type = ACTIVITY_SAVEMAIL;
1369                         new_activity.dest_mbox  = NULL;
1370                         new_activity.src_mbox   = NULL;
1371
1372                         if (!emcore_add_activity(&new_activity, &err)) {
1373                                 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed [%d] ", err);
1374                         }
1375
1376                         if (!emcore_move_mail_on_server(dest_mbox.account_id, dst_mailbox_id, &mail_id, 1, dest_mbox.name, &err)) {
1377                                 EM_DEBUG_EXCEPTION(" emcore_move_mail_on_server falied [%d]", err);
1378                         }
1379                         else {
1380                                 /* Remove ACTIVITY_SAVEMAIL activity */
1381                                 new_activity.activity_id  =  activityid;
1382                                 new_activity.activity_type = ACTIVITY_SAVEMAIL;
1383                                 new_activity.account_id = account_id;
1384                                 new_activity.mail_id    = mail_id;
1385                                 new_activity.dest_mbox  = NULL;
1386                                 new_activity.server_mailid = NULL;
1387                                 new_activity.src_mbox   = NULL;
1388
1389                                 if (!emcore_delete_activity(&new_activity, &err)) {
1390                                         EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_SAVEMAIL] [%d] ", err);
1391                                 }
1392                         }
1393                         sent_box = 1;
1394                 }
1395 #endif
1396 #endif
1397
1398                 /* On Successful Mail sent remove the Draft flag */
1399                 mail_tbl_data->flags_draft_field = 0;
1400
1401                 if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "flags_draft_field", mail_tbl_data->flags_draft_field, false, &err))
1402                         EM_DEBUG_EXCEPTION("Failed to modify extra flag [%d]", err);
1403
1404                 sent_box = 1;
1405         }
1406         else  {
1407                 if (!emcore_delete_mail(account_id, &mail_id, 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
1408                         EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
1409         }
1410
1411         /* Set the phone log */
1412         if ((err = emcore_set_sent_contacts_log(mail_tbl_data)) != EMAIL_ERROR_NONE) {
1413                 EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
1414         }
1415
1416         /*Update status save_status to DB*/
1417         mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
1418         if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, false, &err))
1419                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
1420
1421         if (!emcore_delete_transaction_info_by_mailId(mail_id))
1422                 EM_DEBUG_EXCEPTION(" emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
1423
1424         ret = true;
1425
1426 FINISH_OFF:
1427         if (ret == false && err != EMAIL_ERROR_INVALID_PARAM && mail_tbl_data)  {
1428                 if (err != EMAIL_ERROR_CANCELLED) {
1429                         mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
1430                         if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, false, &err2))
1431                                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
1432                 }
1433                 else {
1434                         if (EMAIL_MAIL_STATUS_SEND_CANCELED == mail_tbl_data->save_status)
1435                                 EM_DEBUG_LOG("EMAIL_MAIL_STATUS_SEND_CANCELED Already set for ");
1436                         else {
1437                                 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
1438                                 if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, false, &err2))
1439                                         EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
1440                         }
1441                 }
1442         }
1443
1444         if (ref_account) {
1445                 emcore_free_account(ref_account);
1446                 EM_SAFE_FREE(ref_account);
1447         }
1448
1449 #ifndef __FEATURE_KEEP_CONNECTION__
1450         if (stream)
1451                 smtp_close(stream);
1452 #endif /* __FEATURE_KEEP_CONNECTION__ */
1453         if (stls)
1454                 mail_parameters(NULL, SET_SSLSTART, (void  *)stls);
1455
1456         if (attachment_tbl_data)
1457                 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
1458
1459         if (envelope)
1460                 mail_free_envelope(&envelope);
1461
1462         if (fpath) {
1463                 EM_DEBUG_LOG("REMOVE TEMP FILE  :  %s", fpath);
1464                 remove(fpath);
1465                 free(fpath);
1466         }
1467
1468         if(local_mailbox)
1469                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1470
1471         if (ret == true) {
1472                 if (!emcore_notify_network_event(NOTI_SEND_FINISH, account_id, NULL, mail_id, 0))
1473                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEND_FINISH] Failed");
1474 #ifdef __FEATURE_SUPPORT_VALIDATION_SYSTEM__
1475                 if(mail_tbl_data)
1476                         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);
1477 #endif
1478         }
1479         else {
1480                 if (!emcore_notify_network_event(NOTI_SEND_FAIL, account_id, NULL, mail_id, err))
1481                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEND_FAIL] Failed");
1482 #ifdef __FEATURE_SUPPORT_VALIDATION_SYSTEM__
1483                 if(mail_tbl_data)
1484                 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);
1485 #endif
1486                 emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, err);
1487         }
1488
1489         if (mail_tbl_data)
1490                 emstorage_free_mail(&mail_tbl_data, 1, NULL);
1491
1492         if (err_code != NULL)
1493                 *err_code = err;
1494         EM_PROFILE_END(profile_emcore_send_mail);
1495         EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
1496         return ret;
1497 }
1498
1499 /*  send a saved all mails */
1500 INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_name, int *err_code)
1501 {
1502         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_name[%p], err_code[%p]", account_id, input_mailbox_name, err_code);
1503
1504         int ret = false;
1505         int err = EMAIL_ERROR_NONE;
1506         int status = EMAIL_SEND_FAIL;
1507         int *mail_ids = NULL;
1508         int handle = 0;
1509         int i = 0;
1510         int total = 0;
1511         int attachment_tbl_count = 0;
1512         char *fpath = NULL;
1513         SENDSTREAM *stream = NULL;
1514         ENVELOPE *envelope = NULL;
1515         email_account_t *ref_account = NULL;
1516         emstorage_mail_tbl_t       *searched_mail_tbl_data = NULL;
1517         emstorage_attachment_tbl_t *attachment_tbl_data    = NULL;
1518         email_option_t *opt = NULL;
1519         sslstart_t      stls = NULL;
1520         void *tmp_stream = NULL;
1521         emstorage_mailbox_tbl_t* local_mailbox = NULL;
1522         int dst_mailbox_id = 0;
1523
1524         if (!account_id || !input_mailbox_name)  {
1525                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1526                 err = EMAIL_ERROR_INVALID_PARAM;
1527                 goto FINISH_OFF;
1528         }
1529
1530
1531         if (!(ref_account = emcore_get_account_reference(account_id)))  {
1532                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1533                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1534                 goto FINISH_OFF;
1535         }
1536
1537         FINISH_OFF_IF_CANCELED;
1538
1539         opt = &(ref_account->options);
1540
1541         /*  search mail. */
1542         if (!emstorage_mail_search_start(NULL, account_id, input_mailbox_name, 0, &handle, &total, true, &err))  {
1543                 EM_DEBUG_EXCEPTION("emstorage_mail_search_start failed [%d]", err);
1544                 goto FINISH_OFF;
1545         }
1546
1547         mail_ids = em_malloc(sizeof(int) * total);
1548         if (mail_ids == NULL) {
1549                 EM_DEBUG_EXCEPTION("malloc failed...");
1550                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1551                 goto FINISH_OFF;
1552         }
1553
1554         for (i = 0; i < total ; i++) {
1555                 if (!emstorage_mail_search_result(handle, RETRIEVE_ID, (void **)&mail_ids[i], true, &err))  {
1556                         EM_DEBUG_EXCEPTION("emstorage_mail_search_result failed [%d]", err);
1557                         goto FINISH_OFF;
1558                 }
1559         }
1560
1561         if (!emstorage_mail_search_end(handle, true, &err)) {
1562                 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
1563                 goto FINISH_OFF;
1564         }
1565
1566         handle = 0;
1567
1568         mail_send_notify(EMAIL_SEND_PREPARE, 0, 0, account_id, mail_ids[total], err);
1569
1570         for (i = 0; i < total; i++) {
1571                 FINISH_OFF_IF_CANCELED;
1572
1573                 if (!emstorage_get_mail_by_id(mail_ids[i], &searched_mail_tbl_data, false, &err)) {
1574                         EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1575                         goto FINISH_OFF;
1576                 }
1577
1578                 /* Skip the mail canceled */
1579                 if (searched_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED)  {
1580                         EM_DEBUG_EXCEPTION("The mail was canceled. [%d]", mail_ids[i]);
1581                         emstorage_free_mail(&searched_mail_tbl_data, 1, &err);
1582                         searched_mail_tbl_data = NULL;
1583                         continue;
1584                 }
1585
1586                 if ( (err = emstorage_get_attachment_list(mail_ids[i], false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
1587                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
1588                         goto FINISH_OFF;
1589                 }
1590
1591                 /* check for email_address validation */
1592                 if ( (err = em_verify_email_address_of_mail_tbl(searched_mail_tbl_data, false)) != EMAIL_ERROR_NONE ) {
1593                         err = EMAIL_ERROR_INVALID_ADDRESS;
1594                         EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed [%d]", err);
1595                         goto FINISH_OFF;
1596                 }
1597
1598                 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
1599
1600                 if (!emcore_make_rfc822_file_from_mail(searched_mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err))  {
1601                         EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail falied [%d]", err);
1602                         goto FINISH_OFF;
1603                 }
1604
1605                 FINISH_OFF_IF_CANCELED;
1606
1607                 /*  connect mail server. */
1608                 if (!stream) {
1609                         /*  if there no security option, unset security. */
1610                         if (!ref_account->outgoing_server_secure_connection) {
1611                                 stls = (sslstart_t)mail_parameters(NULL, GET_SSLSTART, NULL);
1612                                 mail_parameters(NULL, SET_SSLSTART, NULL);
1613                         }
1614
1615                         stream = NULL;
1616                         if (!emcore_connect_to_remote_mailbox(account_id, EMAIL_CONNECT_FOR_SENDING, &tmp_stream, &err) || !tmp_stream)  {
1617                                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1618
1619                                 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1620                                         err = EMAIL_ERROR_CANCELLED;
1621
1622                                 status = EMAIL_SEND_CONNECTION_FAIL;
1623                                 goto FINISH_OFF;
1624                         }
1625
1626                         stream = (SENDSTREAM *)tmp_stream;
1627
1628                         FINISH_OFF_IF_CANCELED;
1629
1630                         mail_send_notify(EMAIL_SEND_CONNECTION_SUCCEED, 0, 0, account_id, mail_ids[i], err);
1631
1632                         /*  reqest of delivery status. */
1633                         if (opt && opt->req_delivery_receipt == EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON)  {
1634                                 stream->protocol.esmtp.dsn.want = 1;
1635                                 stream->protocol.esmtp.dsn.full = 0;
1636                                 stream->protocol.esmtp.dsn.notify.failure = 1;
1637                                 stream->protocol.esmtp.dsn.notify.success = 1;
1638                         }
1639
1640                         mail_send_notify(EMAIL_SEND_START, 0, 0, account_id, mail_ids[i], err);
1641                 }
1642
1643                 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
1644
1645                 /*  update mail status to sending. */
1646                 if (!emstorage_change_mail_field(mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
1647                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1648
1649                         goto FINISH_OFF;
1650                 }
1651
1652                 if (!emcore_send_mail_smtp(stream, envelope, fpath, account_id, mail_ids[i], &err))  {
1653                         EM_DEBUG_EXCEPTION("emcore_send_mail_smtp failed [%d]", err);
1654
1655                         searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
1656
1657                         /*  update mail status to failure. */
1658                         if (!emstorage_change_mail_field(mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))
1659                                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1660
1661                         if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
1662                                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1663                                 goto FINISH_OFF;
1664                         }
1665                         dst_mailbox_id = local_mailbox->mailbox_id;
1666
1667                         emcore_move_mail(&mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, NULL);
1668
1669                         if(local_mailbox)
1670                                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1671
1672                         goto FINISH_OFF;
1673                 }
1674
1675                 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
1676
1677                 /*  update mail status to sent mail. */
1678                 if (!emstorage_change_mail_field(mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
1679                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1680                         goto FINISH_OFF;
1681                 }
1682
1683                 /*  sent mail is moved to 'SENT' box or deleted. */
1684                 if (opt->keep_local_copy)  {
1685                         if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
1686                                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1687                                 goto FINISH_OFF;
1688                         }
1689                         dst_mailbox_id = local_mailbox->mailbox_id;
1690
1691                         if (!emcore_move_mail(&mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
1692                                 EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
1693
1694                         if(local_mailbox)
1695                                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1696                 }
1697                 else  {
1698                         if (!emcore_delete_mail(account_id, &mail_ids[i], 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
1699                                 EM_DEBUG_EXCEPTION("emcore_delete_mail falied [%d]", err);
1700                 }
1701
1702                 /* Set the phone log */
1703                 if ((err = emcore_set_sent_contacts_log(searched_mail_tbl_data)) != EMAIL_ERROR_NONE) {
1704                         EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
1705                 }
1706
1707                 if(searched_mail_tbl_data) {
1708                         emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
1709                         searched_mail_tbl_data = NULL;
1710                 }
1711
1712                 if(attachment_tbl_data)
1713                         emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
1714
1715                 mail_free_envelope(&envelope); envelope = NULL;
1716
1717                 if (fpath)  {
1718                         remove(fpath);
1719                         EM_SAFE_FREE(fpath);
1720                 }
1721         }
1722
1723
1724         ret = true;
1725
1726 FINISH_OFF:
1727
1728         if (ref_account) {
1729                 emcore_free_account(ref_account);
1730                 EM_SAFE_FREE(ref_account);
1731         }
1732
1733         if (stream)
1734                 smtp_close(stream);
1735
1736         if (stls)
1737                 mail_parameters(NIL, SET_SSLSTART, (void  *)stls);
1738
1739         if (envelope)
1740                 mail_free_envelope(&envelope);
1741
1742         if (handle)  {
1743                 if (!emstorage_mail_search_end(handle, true, &err))
1744                         EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
1745         }
1746
1747         if (searched_mail_tbl_data)
1748                 emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
1749
1750         if(attachment_tbl_data)
1751                 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
1752
1753         if (fpath)  {
1754                 remove(fpath);
1755                 EM_SAFE_FREE(fpath);
1756         }
1757
1758         if(local_mailbox)
1759                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1760
1761         if (ret == true)
1762                 mail_send_notify(EMAIL_SEND_FINISH, 0, 0, account_id, mail_ids[total], err);
1763         else {
1764                 if(mail_ids) /* prevent 34385 */
1765                         mail_send_notify(status, 0, 0, account_id, mail_ids[total], err);
1766                 emcore_show_user_message(account_id, EMAIL_ACTION_SEND_MAIL, err);
1767         }
1768
1769         EM_SAFE_FREE(mail_ids);
1770
1771         if (err_code != NULL)
1772                 *err_code = err;
1773
1774         return ret;
1775 }
1776
1777 static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code)
1778 {
1779         EM_DEBUG_FUNC_BEGIN("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);
1780         EM_PROFILE_BEGIN(profile_emcore_send_mail_smtp);
1781
1782         int ret = false;
1783         int err = EMAIL_ERROR_NONE;
1784         int recipients = 0;
1785         long total = 0, sent = 0, send_ret = 0, send_err = 0, sent_percent = 0, last_sent_percent = 0;
1786         char buf[2048] = { 0, };
1787         email_account_t *ref_account = NULL;
1788         FILE *fp = NULL;
1789
1790         if (!env || !env->from || (!env->to && !env->cc && !env->bcc)) {
1791                 if (env != NULL)
1792                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1793                 err = EMAIL_ERROR_INVALID_PARAM;
1794                 goto FINISH_OFF;
1795         }
1796
1797         if (!env->from->mailbox || !env->from->host) {
1798                 EM_DEBUG_EXCEPTION("env->from->mailbox[%p], env->from->host[%p]", env->from->mailbox, env->from->host);
1799                 err = EMAIL_ERROR_INVALID_PARAM;
1800                 goto FINISH_OFF;
1801         }
1802
1803         if (!(ref_account = emcore_get_account_reference(account_id))) {
1804                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1805                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1806                 goto FINISH_OFF;
1807         }
1808
1809         EM_DEBUG_LOG("Modifying - MAIL FROM ");
1810         if (ref_account->user_email_address == NULL) {
1811                 EM_DEBUG_LOG("ref_account->user_email_address is null!!");
1812                 SNPRINTF(buf, sizeof(buf), "FROM:<%s@%s>", env->from->mailbox, env->from->host);
1813         }
1814         else
1815                 SNPRINTF(buf, sizeof(buf), "FROM:<%s>", ref_account->user_email_address);
1816
1817         /*  set DSN for ESMTP */
1818         if (stream->protocol.esmtp.ok) {
1819                 if (stream->protocol.esmtp.eightbit.ok && stream->protocol.esmtp.eightbit.want)
1820                         strncat (buf, " BODY=8BITMIME", sizeof(buf)-(EM_SAFE_STRLEN(buf)+1));
1821
1822                 EM_DEBUG_LOG("stream->protocol.esmtp.dsn.ok [%d]", stream->protocol.esmtp.dsn.ok);
1823
1824                 if (stream->protocol.esmtp.dsn.ok && stream->protocol.esmtp.dsn.want) {
1825                         EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is required");
1826                         strncat (buf, stream->protocol.esmtp.dsn.full ? " RET=FULL" : " RET=HDRS", sizeof(buf)-EM_SAFE_STRLEN(buf)-1);
1827                         if (stream->protocol.esmtp.dsn.envid)
1828                                 SNPRINTF (buf + EM_SAFE_STRLEN (buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)), " ENVID=%.100s", stream->protocol.esmtp.dsn.envid);
1829                 }
1830                 else
1831                         EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is not required or DSN is not supported");
1832         }
1833
1834         EM_PROFILE_BEGIN(profile_prepare_and_head);
1835         send_ret = smtp_send(stream, "RSET", 0);
1836         EM_DEBUG_LOG("[SMTP] RSET --------> %s", stream->reply);
1837
1838         if (send_ret != SMTP_RESPONSE_OK) {
1839                 err = send_ret;
1840                 goto FINISH_OFF;
1841         }
1842
1843         send_ret = smtp_send(stream, "MAIL", buf);
1844         EM_DEBUG_LOG("[SMTP] MAIL %s --------> %s", buf, stream->reply);
1845
1846         switch (send_ret) {
1847                 case SMTP_RESPONSE_OK:
1848                         break;
1849
1850                 case SMTP_RESPONSE_WANT_AUTH  :
1851                 case SMTP_RESPONSE_WANT_AUTH2:
1852                         EM_DEBUG_EXCEPTION("SMTP error : authentication required...");
1853                         err = EMAIL_ERROR_AUTH_REQUIRED;
1854                         goto FINISH_OFF;
1855
1856                 case SMTP_RESPONSE_UNAVAIL:
1857                         EM_DEBUG_EXCEPTION("SMTP error : sending unavailable...");
1858                         err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1859                         goto FINISH_OFF;
1860                 case SMTP_RESPONSE_CONNECTION_BROKEN:
1861                         EM_DEBUG_EXCEPTION("SMTP error : SMTP connection broken...");
1862                         err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1863                         goto FINISH_OFF;
1864                 default:
1865                         EM_DEBUG_EXCEPTION("SMTP error : sending unavailable...");
1866                         err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1867                         goto FINISH_OFF;
1868         }
1869
1870         if (env->to) {
1871                 send_ret = smtp_rcpt(stream, env->to, &send_err);
1872                 EM_DEBUG_LOG("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->to->mailbox, env->to->host, env->to->error ? env->to->error  :  stream->reply);
1873                 if (send_ret) {
1874                         err = stream->replycode;
1875                         goto FINISH_OFF;
1876                 }
1877
1878                 if (!send_err)
1879                         recipients++;
1880         }
1881
1882         if (env->cc) {
1883                 send_ret = smtp_rcpt(stream, env->cc, &send_err);
1884                 EM_DEBUG_LOG("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->cc->mailbox, env->cc->host, env->cc->error ? env->cc->error  :  stream->reply);
1885                 if (send_ret) {
1886                         err = stream->replycode;
1887                         goto FINISH_OFF;
1888                 }
1889
1890                 if (!send_err)
1891                         recipients++;
1892         }
1893
1894         if (env->bcc) {
1895                 send_ret = smtp_rcpt(stream, env->bcc, &send_err);
1896                 EM_DEBUG_LOG("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->bcc->mailbox, env->bcc->host, env->bcc->error ? env->bcc->error  :  stream->reply);
1897                 if (send_ret) {
1898                         err = stream->replycode;
1899                         goto FINISH_OFF;
1900                 }
1901
1902                 if (!send_err)
1903                         recipients++;
1904         }
1905
1906
1907         if (send_err) {
1908                 EM_DEBUG_EXCEPTION("One or more recipients failed...");
1909                 err = EMAIL_ERROR_INVALID_ADDRESS;
1910         }
1911
1912         if (!recipients) {
1913                 EM_DEBUG_EXCEPTION("No valid recipients...");
1914
1915                 switch (stream->replycode) {
1916                         case SMTP_RESPONSE_UNAVAIL:
1917                         case SMTP_RESPONSE_WANT_AUTH  :
1918                         case SMTP_RESPONSE_WANT_AUTH2:
1919                                 err = EMAIL_ERROR_AUTH_REQUIRED;
1920                                 break;
1921
1922                         default:
1923                                 err = EMAIL_ERROR_INVALID_ADDRESS;
1924                                 break;
1925                 }
1926                 goto FINISH_OFF;
1927         }
1928
1929         send_ret = smtp_send(stream, "DATA", 0);
1930         EM_DEBUG_LOG("[SMTP] DATA --------> %s", stream->reply);
1931         EM_PROFILE_END(profile_prepare_and_head);
1932
1933         if (send_ret != SMTP_RESPONSE_READY) {
1934                 err = send_ret;
1935                 goto FINISH_OFF;
1936         }
1937
1938         if (data_file) {
1939                 EM_PROFILE_BEGIN(profile_open_file);
1940                 if (!(fp = fopen(data_file, "r+"))) {
1941                         EM_DEBUG_EXCEPTION("fopen(\"%s\") failed...", data_file);
1942                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1943                         goto FINISH_OFF;
1944                 }
1945                 EM_PROFILE_END(profile_open_file);
1946
1947
1948 #ifdef __FEATURE_SEND_OPTMIZATION__
1949         {
1950                 char *data = NULL;
1951                 int read_size, allocSize, dataSize, gMaxAllocSize = 40960; /*  40KB */
1952
1953                 fseek(fp, 0, SEEK_END);
1954                 total = ftell(fp);
1955                 fseek(fp, 0, SEEK_SET);
1956                 EM_DEBUG_LOG("total size [%d]", total);
1957
1958                 if (total < gMaxAllocSize)
1959                         allocSize = total + 1;
1960                 else
1961                         allocSize = gMaxAllocSize;
1962
1963                 EM_PROFILE_BEGIN(profile_allocation);
1964                 /* Allocate a buffer of max 2MB to read from file */
1965                 data = (char *)em_malloc(allocSize);
1966                 allocSize--;
1967                 EM_PROFILE_END(profile_allocation);
1968
1969                 if (NULL == data) {
1970                         err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1971                         goto FINISH_OFF;
1972                 }
1973
1974                 while (total) {
1975                         if (total  < allocSize)
1976                                 dataSize = total;
1977                         else
1978                                 dataSize = allocSize;
1979
1980                         memset(data, 0x0, dataSize+1);
1981                         read_size = fread(data, sizeof (char), dataSize, fp);
1982
1983                         if (read_size != dataSize) {
1984                                 /* read fail. */
1985                                 EM_SAFE_FREE(data);
1986                                 EM_DEBUG_EXCEPTION("Read from file failed");
1987                                 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1988                                 goto FINISH_OFF;
1989                         }
1990                         sent += read_size;
1991
1992                         EM_DEBUG_LOG("before smtp_soutr_test");
1993                         if (!(send_ret = smtp_soutr_test(stream->netstream, data))) {
1994                                 EM_SAFE_FREE(data);
1995                                 EM_DEBUG_EXCEPTION("Failed to send the data ");
1996                                 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1997                                 goto FINISH_OFF;
1998                         }
1999                         else {
2000                                 sent_percent = (int) ((double)sent / (double)total * 100.0);
2001                                 if (last_sent_percent + 5 <= sent_percent) {
2002                                         if (!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, sent_percent))
2003                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEND_START] Failed >>>>");
2004                                         last_sent_percent = sent_percent;
2005                                 }
2006                                 EM_DEBUG_LOG("Sent data Successfully. sent[%d] total[%d]", sent, total);
2007                         }
2008                         total -= dataSize;
2009                 }
2010
2011                 EM_SAFE_FREE(data);
2012         }
2013 #else
2014                 fseek(fp, 0, SEEK_END);
2015                 total = ftell(fp);
2016                 fseek(fp, 0, SEEK_SET);
2017
2018                 while (fgets(buf, 1024, fp)) {
2019 #ifdef FEATURE_SEND_DATA_DEBUG
2020                         EM_DEBUG_LOG("%s", buf);
2021 #endif
2022                         sent += EM_SAFE_STRLEN(buf);
2023
2024                         if (!(send_ret = smtp_soutr(stream->netstream, buf)))
2025                                 break;
2026                         /*  Sending Progress Notification */
2027                         sent_percent = (int) ((double)sent / (double)total * 100.0);
2028                         if (last_sent_percent + 5 <= sent_percent) {
2029                                 /* Disabled Temporary
2030                                 if (!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, sent_percent))
2031                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_SEND_START] Failed >>>>");
2032                                 */
2033                                 last_sent_percent = sent_percent;
2034                         }
2035                 }
2036
2037 #endif
2038                 if (!send_ret) {
2039                         EM_DEBUG_EXCEPTION("smtp_soutr failed - %ld", send_ret);
2040                         err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2041                         goto FINISH_OFF;
2042                 }
2043         }
2044
2045         send_ret = smtp_send(stream, ".", 0);
2046         EM_DEBUG_LOG("[SMTP] . --------> %s", stream->reply);
2047
2048         if (send_ret != SMTP_RESPONSE_OK) {
2049                 err = send_ret;
2050                 goto FINISH_OFF;
2051         }
2052
2053         ret = true;
2054
2055 FINISH_OFF:
2056         if (ret == false)
2057                 smtp_send(stream, "RSET", 0);
2058
2059         if (ref_account) {
2060                 emcore_free_account(ref_account);
2061                 EM_SAFE_FREE(ref_account);
2062         }
2063
2064         if (err_code)
2065                 *err_code = err;
2066
2067         if (fp)
2068                 fclose(fp);
2069         EM_PROFILE_END(profile_emcore_send_mail_smtp);
2070         EM_DEBUG_FUNC_END("ret [%d]", ret);
2071         return ret;
2072 }
2073
2074 /* ------ rfc822 handle --------------------------------------------------- */
2075 #define RANDOM_NUMBER_LENGTH 35
2076
2077 char *emcore_generate_content_id_string(const char *hostname, int *err)
2078 {
2079         EM_DEBUG_FUNC_BEGIN("hostname[%p]", hostname);
2080
2081         if (!hostname) {
2082                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2083                 if (err)
2084                         *err = EMAIL_ERROR_INVALID_PARAM;
2085                 return NULL;
2086         }
2087
2088         int cid_length = RANDOM_NUMBER_LENGTH + EM_SAFE_STRLEN(hostname) + 2, random_number_1, random_number_2, random_number_3, random_number_4;
2089         char *cid_string = NULL;
2090
2091         cid_string = malloc(cid_length);
2092
2093         if (!cid_string) {
2094                 if (err)
2095                         *err = EMAIL_ERROR_OUT_OF_MEMORY;
2096                 return NULL;
2097         }
2098
2099         memset(cid_string, 0, cid_length);
2100
2101         srand(time(NULL) + rand());
2102         random_number_1 = rand() * rand();
2103         random_number_2 = rand() * rand();
2104         random_number_3 = rand() * rand();
2105         random_number_4 = rand() * rand();
2106
2107         SNPRINTF(cid_string, cid_length, "<%08x%08x%08x%08x@%s>", random_number_1, random_number_2, random_number_3, random_number_4, hostname);
2108
2109         if (err)
2110                 *err = EMAIL_ERROR_NONE;
2111
2112         EM_DEBUG_FUNC_END("cid_string [%s]", cid_string);
2113         return cid_string;
2114 }
2115
2116
2117 /* ------ attach_part ----------------------------------------------------- */
2118 /*  data  :  if filename NULL, content data. */
2119 /*              else absolute path of file to be attached. */
2120 /*  data_len  :  length of data. if filename not NULL, ignored. */
2121 /*  file_name :  attahcment name. */
2122 static int attach_part(BODY *body, const unsigned char *data, int data_len, char *filename, char *content_sub_type, int is_inline, int *err_code)
2123 {
2124         EM_DEBUG_FUNC_BEGIN("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);
2125
2126         int        ret = false;
2127         int        error = EMAIL_ERROR_NONE;
2128         int        has_special_character = 0;
2129         int        base64_file_name_length = 0;
2130         int        i= 0;
2131         gsize      bytes_read;
2132         gsize      bytes_written;
2133         char      *encoded_file_name = NULL;
2134         char      *extension = NULL;
2135         char      *base64_file_name = NULL;
2136         char      *result_file_name = NULL;
2137         char       content_disposition[100] = { 0, };
2138         PARAMETER *last_param = NULL;
2139         PARAMETER *param = NULL;
2140         PART      *last_part = NULL;
2141         PART      *part = NULL;
2142         SIZEDTEXT  source_text;
2143         GError    *glib_error = NULL;
2144         CHARSET   *result_charset = NULL;
2145
2146         if (!body)  {
2147                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2148                 error = EMAIL_ERROR_INVALID_PARAM;
2149                 goto FINISH_OFF;
2150         }
2151
2152         if (body->nested.part)  {
2153                 last_part = body->nested.part;
2154
2155                 if (last_part != NULL)  {
2156                         while (last_part->next)
2157                                 last_part = last_part->next;
2158                 }
2159         }
2160
2161         /*  PART */
2162         part = mail_newbody_part();
2163         if (part == NULL)  {
2164                 EM_DEBUG_EXCEPTION("mail_newbody_part failed...");
2165                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2166                 goto FINISH_OFF;
2167         }
2168
2169         part->next = NULL;
2170
2171         if (last_part)
2172                 last_part->next = part;
2173         else
2174                 body->nested.part = part;
2175
2176         last_part = part;
2177
2178         /* set data..  */
2179         /* content_data = (unsigned char  *)fs_get(data_len + 1); */
2180         /* memcpy(content_data, data, data_len); */
2181         /* content_data[data_len] = 0; */
2182
2183         /* part->body.contents.text.data = content_data; */
2184         /* part->body.contents.text.size = data_len; */
2185
2186         if (filename)  {   /*  attachment */
2187                 source_text.data = (unsigned char*)filename;
2188                 source_text.size = EM_SAFE_STRLEN(filename);
2189
2190                 result_charset   = (CHARSET*)utf8_infercharset(&source_text);
2191
2192                 if(result_charset) {
2193                         EM_DEBUG_LOG("return_charset->name [%s]", result_charset->name);
2194                         encoded_file_name = (char*)g_convert (filename, -1, "UTF-8", result_charset->name, &bytes_read, &bytes_written, &glib_error);
2195                 }
2196                 else {
2197                         i = 0;
2198                         while(filename[i]) {
2199                                 if(filename[i++] & 0x80) {
2200                                         has_special_character = 1;
2201                                         break;
2202                                 }
2203                         }
2204                         EM_DEBUG_LOG("has_special_character [%d]", has_special_character);
2205                         if(has_special_character)
2206                                 encoded_file_name = (char*)g_convert (filename, -1, "UTF-8", "EUC-KR", &bytes_read, &bytes_written, &glib_error);
2207                 }
2208
2209                 EM_DEBUG_LOG("encoded_file_name [%s]", encoded_file_name);
2210
2211                 if(encoded_file_name == NULL)
2212                         encoded_file_name = strdup(filename);
2213
2214                 if(!em_encode_base64(encoded_file_name, EM_SAFE_STRLEN(encoded_file_name), &base64_file_name, (unsigned long*)&base64_file_name_length, &error)) {
2215                         EM_DEBUG_EXCEPTION("em_encode_base64 failed. error [%d]", error);
2216                         goto FINISH_OFF;
2217                 }
2218
2219                 result_file_name = em_replace_string(base64_file_name, "\015\012", "");
2220
2221                 EM_DEBUG_LOG("base64_file_name_length [%d]", base64_file_name_length);
2222
2223                 if(result_file_name) {
2224                         EM_SAFE_FREE(encoded_file_name);
2225                         encoded_file_name = em_malloc(EM_SAFE_STRLEN(result_file_name) + 15);
2226                         if(!encoded_file_name) {
2227                                 EM_DEBUG_EXCEPTION("em_malloc failed.");
2228                                 goto FINISH_OFF;
2229                         }
2230                         snprintf(encoded_file_name, EM_SAFE_STRLEN(result_file_name) + 15, "=?UTF-8?B?%s?=", result_file_name);
2231                         EM_DEBUG_LOG("encoded_file_name [%s]", encoded_file_name);
2232                 }
2233
2234                 extension = em_get_extension_from_file_path(filename, NULL);
2235
2236                 part->body.type = em_get_content_type_from_extension_string(extension, NULL);
2237                 if(part->body.type == TYPEIMAGE) {
2238                         part->body.subtype = strdup(extension);
2239                 } else if (part->body.type == TYPEPKCS7_SIGN) {
2240                         part->body.subtype = strdup("pkcs7-signature");
2241                         part->body.type = TYPEAPPLICATION;
2242                 } else if (part->body.type == TYPEPKCS7_MIME) {
2243                         part->body.subtype = strdup("pkcs7-mime");
2244                         part->body.type = TYPEAPPLICATION;
2245                 } else
2246                         part->body.subtype = strdup("octet-stream");
2247
2248                 part->body.encoding = ENCBINARY;
2249                 part->body.size.bytes = data_len;
2250
2251                 if (data)
2252                         part->body.sparep = EM_SAFE_STRDUP((char *)data); /*  file path */
2253                 else
2254                         part->body.sparep = NULL;
2255
2256                 SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "attachment");
2257
2258                 part->body.disposition.type = cpystr(content_disposition);
2259
2260                 /*  BODY PARAMETER */
2261                 /*  another parameter or get parameter-list from this   function-parameter */
2262                 param = mail_newbody_parameter();
2263                 if (param == NULL)  {
2264                         EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2265                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2266                         goto FINISH_OFF;
2267                 }
2268
2269                 param->attribute          = cpystr("name");
2270                 param->value              = cpystr(encoded_file_name);
2271                 param->next               = NULL;
2272                 last_param                = param;
2273                 last_part->body.parameter = last_param;
2274
2275                 if (is_inline) {
2276                         /*  CONTENT-ID */
2277                         part->body.id = emcore_generate_content_id_string("com.samsung.slp.email", &error);
2278                         part->body.type = TYPEIMAGE;
2279                         /*  EM_SAFE_FREE(part->body.subtype); */
2280                         /*  part->body.subtype = EM_SAFE_STRDUP(content_sub_type); */
2281                 }
2282
2283                 /*  DISPOSITION PARAMETER */
2284                 param = mail_newbody_parameter();
2285                 if (param == NULL)  {
2286                         EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2287                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2288                         goto FINISH_OFF;
2289                 }
2290
2291                 param->attribute                      = cpystr("filename");
2292                 param->value                          = cpystr(encoded_file_name);
2293                 param->next                           = NULL;
2294                 last_param                            = param;
2295                 last_part->body.disposition.parameter = last_param;
2296
2297                 if (is_inline)
2298                         last_part->body.disposition.type = strdup("inline");
2299         }
2300         else  {
2301                 /*  text body (plain/html) */
2302                 part->body.type = TYPETEXT;
2303                 part->body.size.bytes = data_len;
2304
2305                 if (data)
2306                         part->body.sparep = EM_SAFE_STRDUP((char *)data); /*  file path */
2307                 else
2308                         part->body.sparep = NULL;
2309
2310
2311                 if (!content_sub_type)  {
2312                         /* Plain text body */
2313                         part->body.encoding = ENC8BIT;
2314                         part->body.subtype = cpystr("plain");
2315                         last_param = part->body.parameter;
2316
2317                         if (last_param != NULL)  {
2318                                 while (last_param->next)
2319                                         last_param = last_param->next;
2320                         }
2321
2322                         param = mail_newbody_parameter();
2323
2324                         if (param == NULL)  {
2325                                 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2326                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2327                                 goto FINISH_OFF;
2328                         }
2329
2330                         param->attribute = cpystr("CHARSET");
2331
2332                         if (data != NULL) {
2333                                 gchar *extract_charset_plain = g_path_get_basename((const gchar *)data);
2334                                 if (extract_charset_plain != NULL && extract_charset_plain[0] != '\0')
2335                                         param->value = cpystr(extract_charset_plain);
2336                                 g_free(extract_charset_plain);
2337                         }
2338                         else
2339                                 param->value = cpystr("UTF-8");
2340
2341                         if(!param->value)
2342                                 param->value = cpystr("UTF-8");
2343
2344                         param->next = NULL;
2345
2346                         if (last_param != NULL)
2347                                 last_param->next = param;
2348                         else
2349                                 part->body.parameter = param;
2350                 }
2351                 else {
2352                         /* HTML text body */
2353                         part->body.encoding = ENC8BIT;
2354                         part->body.subtype  = cpystr(content_sub_type);
2355
2356                         last_param = part->body.parameter;
2357
2358                         if (last_param != NULL)  {
2359                                 while (last_param->next)
2360                                         last_param = last_param->next;
2361                         }
2362
2363                         param = mail_newbody_parameter();
2364
2365                         if (param == NULL)  {
2366                                 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2367                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2368                                 goto FINISH_OFF;
2369                         }
2370
2371                         param->attribute = cpystr("CHARSET");
2372
2373                         char *pHtml = NULL;
2374                         if (data != NULL) {
2375                                 gchar *extract_charset = g_path_get_basename((const gchar *)data);
2376                                 if (extract_charset != NULL) {
2377                                         if ((pHtml = strstr(extract_charset, ".htm")) != NULL) {
2378                                                 extract_charset[pHtml-extract_charset] = '\0';
2379                                                 param->value = cpystr(extract_charset);
2380                                         }
2381                                 }
2382
2383                                 if(!param->value)
2384                                         param->value = cpystr("UTF-8");
2385
2386                                 EM_SAFE_FREE(extract_charset);
2387                         }
2388                         else
2389                                 param->value = cpystr("UTF-8");
2390                         param->next = NULL;
2391
2392                         if (last_param != NULL)
2393                                 last_param->next = param;
2394                         else
2395                                 part->body.parameter = param;
2396                 }
2397
2398                 /* NOTE : need to require this code. */
2399                 /* sprintf(content_disposition, "%s\0", "inline"); */
2400                 if (is_inline) {
2401                         SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "inline");
2402                         part->body.disposition.type = cpystr(content_disposition);
2403                 }
2404         }
2405
2406         ret = true;
2407
2408 FINISH_OFF:
2409         EM_SAFE_FREE(encoded_file_name);
2410         EM_SAFE_FREE(result_file_name); /*prevent 26242*/
2411         EM_SAFE_FREE(base64_file_name);
2412         if (err_code != NULL)
2413                 *err_code = error;
2414         EM_DEBUG_FUNC_END();
2415         return ret;
2416 }
2417
2418 static PART *attach_mutipart_with_sub_type(BODY *parent_body, char *sub_type, int *err_code)
2419 {
2420         EM_DEBUG_FUNC_BEGIN("parent_body[%p], sub_type [%s], err_code[%p]", parent_body, sub_type, err_code);
2421
2422         int error = EMAIL_ERROR_NONE;
2423
2424         PART *tail_part_cur = NULL;
2425         PART *new_part = NULL;
2426
2427         if (!parent_body || !sub_type)  {
2428                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2429                 error = EMAIL_ERROR_INVALID_PARAM;
2430                 goto FINISH_OFF;
2431         }
2432
2433         if (parent_body->nested.part)  {
2434                 tail_part_cur = parent_body->nested.part;
2435
2436                 if (tail_part_cur != NULL)  {
2437                         while (tail_part_cur->next)
2438                                 tail_part_cur = tail_part_cur->next;
2439                 }
2440         }
2441
2442         new_part = mail_newbody_part();
2443
2444         if (new_part == NULL)  {
2445                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2446                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2447                 goto FINISH_OFF;
2448
2449         }
2450
2451         new_part->next = NULL;
2452         new_part->body.type = TYPEMULTIPART;
2453         new_part->body.subtype = EM_SAFE_STRDUP(sub_type);
2454
2455         if (tail_part_cur)
2456                 tail_part_cur->next = new_part;
2457         else
2458                 parent_body->nested.part = new_part;
2459
2460 FINISH_OFF:
2461
2462         if (err_code)
2463                 *err_code = error;
2464
2465         EM_DEBUG_FUNC_END();
2466
2467         return new_part;
2468 }
2469 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
2470 static int attach_attachment_to_body(BODY **multipart_body, BODY *text_body, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_tbl_count, int *err_code)
2471 {
2472         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);
2473
2474         int ret = false;
2475         int i = 0;
2476         int error = EMAIL_ERROR_NONE;
2477         BODY *frame_body = NULL;
2478         /*  make multipart body(multipart frame_body..) .. that has not content..  */
2479
2480         if (!multipart_body || !text_body || !input_attachment_tbl) {
2481                 EM_DEBUG_EXCEPTION(" multipart_body[%p], text_body[%p], input_attachment_tbl[%p]", multipart_body, text_body, input_attachment_tbl);
2482                 error = EMAIL_ERROR_INVALID_PARAM;
2483                 goto FINISH_OFF;
2484         }
2485
2486         frame_body = mail_newbody();
2487         if (frame_body == NULL) {
2488                 EM_DEBUG_EXCEPTION("mail_newbody failed...");
2489                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2490                 goto FINISH_OFF;
2491         }
2492
2493         frame_body->type = TYPEMULTIPART;
2494         frame_body->contents.text.data = NULL;
2495         frame_body->contents.text.size = 0;
2496         frame_body->size.bytes = 0;
2497
2498         /*  insert original text_body to frame_body.. */
2499         if (!attach_part(frame_body, text_body->sparep, 0, NULL, NULL, false, &error))  {
2500                 EM_DEBUG_EXCEPTION(" attach_part failed [%d]", error);
2501                 goto FINISH_OFF;
2502         }
2503
2504         /*  insert files..  */
2505         emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
2506         char *name = NULL;
2507         struct stat st_buf;
2508
2509         for(i = 0; i < input_attachment_tbl_count; i++) {
2510                 temp_attachment_tbl = input_attachment_tbl + i;
2511
2512                 EM_DEBUG_LOG("insert files - attachment id[%d]", temp_attachment_tbl->attachment_id);
2513
2514                 if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0)  {
2515                         if (!temp_attachment_tbl->attachment_name)  {
2516                                 if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
2517                                         EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
2518                                         goto FINISH_OFF;
2519                                 }
2520                         }
2521                         else
2522                                 name = temp_attachment_tbl->attachment_name;
2523
2524                         if (!attach_part(frame_body, (unsigned char *)temp_attachment_tbl->attachment_path, 0, name, NULL, false, &error))  {
2525                                 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
2526                                 goto FINISH_OFF;
2527                         }
2528                 }
2529         }
2530
2531         ret = true;
2532
2533 FINISH_OFF:
2534         if (ret == true)
2535                 *multipart_body = frame_body;
2536         else if (frame_body != NULL)
2537                 mail_free_body(&frame_body);
2538
2539         if (err_code != NULL)
2540                 *err_code = error;
2541         EM_DEBUG_FUNC_END();
2542         return ret;
2543 }
2544 #endif
2545 static char *emcore_encode_rfc2047_text(char *utf8_text, int *err_code)
2546 {
2547         EM_DEBUG_FUNC_BEGIN("utf8_text[%s], err_code[%p]", utf8_text, err_code);
2548
2549         if (utf8_text == NULL)  {
2550                 if (err_code != NULL)
2551                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2552                 return NULL;
2553         }
2554
2555         gsize len = EM_SAFE_STRLEN(utf8_text);
2556
2557         EM_DEBUG_FUNC_END();
2558
2559         if (len > 0)
2560                 return g_strdup_printf("=?UTF-8?B?%s?=", g_base64_encode((const guchar  *)utf8_text, len));
2561         else
2562                 return strdup("");
2563 }
2564
2565 static void emcore_encode_rfc2047_address(ADDRESS *address, int *err_code)
2566 {
2567         EM_DEBUG_FUNC_BEGIN("address[%p], err_code[%p]", address, err_code);
2568
2569         while (address)  {
2570                 if (address->personal)  {
2571                         char *rfc2047_personal = emcore_encode_rfc2047_text(address->personal, err_code);
2572                         EM_SAFE_FREE(address->personal);
2573                         address->personal = rfc2047_personal;
2574                 }
2575                 address = address->next;
2576         }
2577         EM_DEBUG_FUNC_END();
2578 }
2579
2580 #define DATE_STR_LENGTH 100
2581
2582 static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope)
2583 {
2584         EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], output_envelope[%p]", input_mail_tbl_data, output_envelope);
2585
2586         int     ret = false;
2587         int       error                   = EMAIL_ERROR_NONE;
2588         int       is_incomplete           = 0;
2589         char     *pAdd                    = NULL;
2590         ENVELOPE *envelope                = NULL;
2591         email_account_t *ref_account      = NULL;
2592
2593         if (!input_mail_tbl_data || !output_envelope)  {
2594                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2595                 error = EMAIL_ERROR_INVALID_PARAM;
2596                 return error; /* prevent 32729 */
2597         }
2598
2599         if ( (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0 && !input_mail_tbl_data->body_download_status) {
2600                 EM_DEBUG_EXCEPTION("input_mail_tbl_data->body_download_status[%p]", input_mail_tbl_data->body_download_status);
2601                 error = EMAIL_ERROR_INVALID_PARAM;
2602                 goto FINISH_OFF;
2603         }
2604
2605         if (!(envelope = mail_newenvelope()))  {
2606                 EM_DEBUG_EXCEPTION("mail_newenvelope failed...");
2607                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2608                 goto FINISH_OFF;
2609         }
2610
2611         is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
2612
2613         if (is_incomplete && (input_mail_tbl_data->account_id > 0))  {
2614                 ref_account = emcore_get_account_reference(input_mail_tbl_data->account_id);
2615                 if (!ref_account)  {
2616                         EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
2617                         error = EMAIL_ERROR_INVALID_ACCOUNT;
2618                         goto FINISH_OFF;
2619                 }
2620
2621                 if (ref_account->user_email_address && ref_account->user_email_address[0] != '\0')  {
2622                         char *p = cpystr(ref_account->user_email_address);
2623
2624                         if (p == NULL)  {
2625                                 EM_DEBUG_EXCEPTION("cpystr failed...");
2626                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2627                                 goto FINISH_OFF;
2628                         }
2629
2630                         EM_DEBUG_LOG("Assign envelope->from");
2631
2632                         if (input_mail_tbl_data->full_address_from) {
2633                                 char *temp_address_string = NULL ;
2634                                 em_skip_whitespace(input_mail_tbl_data->full_address_from , &temp_address_string);
2635                                 EM_DEBUG_LOG("address[temp_address_string][%s]", temp_address_string);
2636                                 rfc822_parse_adrlist(&envelope->from, temp_address_string, NULL);
2637                                 EM_SAFE_FREE(temp_address_string);
2638                                 temp_address_string = NULL ;
2639                         }
2640                         else
2641                                 envelope->from = rfc822_parse_mailbox(&p, NULL);
2642
2643                         EM_SAFE_FREE(p);
2644                         if (!envelope->from) {
2645                                 EM_DEBUG_EXCEPTION("rfc822_parse_mailbox failed...");
2646                                 error = EMAIL_ERROR_INVALID_ADDRESS;
2647                                 goto FINISH_OFF;
2648                         }
2649                         else  {
2650                                 if (envelope->from->personal == NULL) {
2651                                         if (ref_account->options.display_name_from && ref_account->options.display_name_from[0] != '\0')
2652                                                 envelope->from->personal = cpystr(ref_account->options.display_name_from);
2653                                         else
2654                                                 envelope->from->personal =
2655                                                                 (ref_account->user_display_name && ref_account->user_display_name[0] != '\0') ?
2656                                                                 cpystr(ref_account->user_display_name)  :  NULL;
2657                                 }
2658                         }
2659                 }
2660
2661                 if (ref_account->return_address && ref_account->return_address[0] != '\0')  {
2662                         char *p = cpystr(ref_account->return_address);
2663
2664                         if (p == NULL)  {
2665                                 EM_DEBUG_EXCEPTION("cpystr failed...");
2666                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2667                                 goto FINISH_OFF;
2668                         }
2669                         envelope->return_path = rfc822_parse_mailbox(&p, NULL);
2670                         EM_SAFE_FREE(p);
2671                 }
2672         }
2673         else  {
2674                 if (!input_mail_tbl_data->full_address_from || !input_mail_tbl_data->full_address_to)  {
2675                         EM_DEBUG_EXCEPTION("input_mail_tbl_data->full_address_from[%p], input_mail_tbl_data->full_address_to[%p]", input_mail_tbl_data->full_address_from, input_mail_tbl_data->full_address_to);
2676                         error = EMAIL_ERROR_INVALID_MAIL;
2677                         goto FINISH_OFF;
2678                 }
2679
2680                 int i, j;
2681
2682                 if (input_mail_tbl_data->full_address_from)  {
2683                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_from); i < j; i++)  {
2684                                 if (input_mail_tbl_data->full_address_from[i] == ';')
2685                                         input_mail_tbl_data->full_address_from[i] = ',';
2686                         }
2687                 }
2688
2689                 if (input_mail_tbl_data->full_address_return)  {
2690                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_return); i < j; i++)  {
2691                                 if (input_mail_tbl_data->full_address_return[i] == ';')
2692                                         input_mail_tbl_data->full_address_return[i] = ',';
2693                         }
2694                 }
2695                 em_skip_whitespace(input_mail_tbl_data->full_address_from , &pAdd);
2696                 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2697
2698                 rfc822_parse_adrlist(&envelope->from, pAdd, NULL);
2699                 EM_SAFE_FREE(pAdd);
2700                 pAdd = NULL;
2701
2702                 em_skip_whitespace(input_mail_tbl_data->full_address_return , &pAdd);
2703                 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2704
2705                 rfc822_parse_adrlist(&envelope->return_path, pAdd, NULL);
2706                 EM_SAFE_FREE(pAdd);
2707                 pAdd = NULL;
2708         }
2709
2710         {
2711                 int i, j;
2712
2713                 if (input_mail_tbl_data->full_address_to)  {
2714                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_to); i < j; i++)  {
2715                                 if (input_mail_tbl_data->full_address_to[i] == ';')
2716                                         input_mail_tbl_data->full_address_to[i] = ',';
2717                         }
2718                 }
2719
2720                 if (input_mail_tbl_data->full_address_cc)  {
2721                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_cc); i < j; i++)  {
2722                                 if (input_mail_tbl_data->full_address_cc[i] == ';')
2723                                         input_mail_tbl_data->full_address_cc[i] = ',';
2724                         }
2725                 }
2726
2727                 if (input_mail_tbl_data->full_address_bcc)  {
2728                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_bcc); i < j; i++)  {
2729                                 if (input_mail_tbl_data->full_address_bcc[i] == ';')
2730                                         input_mail_tbl_data->full_address_bcc[i] = ',';
2731                         }
2732                 }
2733         }
2734
2735         em_skip_whitespace(input_mail_tbl_data->full_address_to , &pAdd);
2736         EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2737
2738         rfc822_parse_adrlist(&envelope->to, pAdd, NULL);
2739         EM_SAFE_FREE(pAdd);
2740         pAdd = NULL ;
2741
2742         EM_DEBUG_LOG("address[input_mail_tbl_data->full_address_cc][%s]", input_mail_tbl_data->full_address_cc);
2743         em_skip_whitespace(input_mail_tbl_data->full_address_cc , &pAdd);
2744         EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2745
2746         rfc822_parse_adrlist(&envelope->cc, pAdd, NULL);
2747         EM_SAFE_FREE(pAdd);
2748         pAdd = NULL ;
2749
2750         em_skip_whitespace(input_mail_tbl_data->full_address_bcc , &pAdd);
2751         rfc822_parse_adrlist(&envelope->bcc, pAdd, NULL);
2752         EM_SAFE_FREE(pAdd);
2753                 pAdd = NULL ;
2754
2755         emcore_encode_rfc2047_address(envelope->return_path, &error);
2756         emcore_encode_rfc2047_address(envelope->from, &error);
2757         emcore_encode_rfc2047_address(envelope->sender, &error);
2758         emcore_encode_rfc2047_address(envelope->reply_to, &error);
2759         emcore_encode_rfc2047_address(envelope->to, &error);
2760         emcore_encode_rfc2047_address(envelope->cc, &error);
2761         emcore_encode_rfc2047_address(envelope->bcc, &error);
2762
2763         if (input_mail_tbl_data->subject)
2764                 envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
2765
2766         char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
2767         rfc822_date(rfc822_date_string);
2768
2769         if (!is_incomplete)  {
2770                 char  localtime_string[DATE_STR_LENGTH] = { 0, };
2771                 strftime(localtime_string, 128, "%a, %e %b %Y %H : %M : %S ", localtime(&input_mail_tbl_data->date_time));
2772                 /* append last 5byes("+0900") */
2773                 g_strlcat(localtime_string, rfc822_date_string + (EM_SAFE_STRLEN(rfc822_date_string) -  5), DATE_STR_LENGTH);
2774                 envelope->date = (unsigned char *)cpystr((const char *)localtime_string);
2775         }
2776         else {
2777                 envelope->date = (unsigned char *)cpystr((const char *)rfc822_date_string);
2778         }
2779
2780         ret = true;
2781
2782 FINISH_OFF:
2783
2784         if (ret)
2785                 *output_envelope = envelope;
2786         else {
2787                 mail_free_envelope(&envelope);
2788                 *output_envelope = NULL;
2789         }
2790
2791         if (ref_account) {
2792                 emcore_free_account(ref_account);
2793                 EM_SAFE_FREE(ref_account);
2794         }
2795
2796         EM_DEBUG_FUNC_END("error [%d]", error);
2797         return error;
2798 }
2799 /*  Description : Make RFC822 text file from mail_tbl data */
2800 /*  Parameters :  */
2801 /*                      input_mail_tbl_data :   */
2802 /*                      is_draft  :  this mail is draft mail. */
2803 /*                      file_path :  path of file that rfc822 data will be written to. */
2804 INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(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)
2805 {
2806         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);
2807
2808         int       ret = false;
2809         int       error = EMAIL_ERROR_NONE;
2810         int       i = 0;
2811         ENVELOPE *envelope      = NULL;
2812         BODY     *text_body     = NULL;
2813         BODY     *html_body     = NULL;
2814         BODY     *root_body     = NULL;
2815         PART     *part_for_html = NULL;
2816         PARAMETER *param = NULL;
2817         PART     *part_for_text = NULL;
2818         char     *fname = NULL;
2819
2820         if (!input_mail_tbl_data)  {
2821                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2822                 error = EMAIL_ERROR_INVALID_PARAM;
2823                 goto FINISH_OFF;
2824         }
2825
2826         if ( (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0 && !input_mail_tbl_data->body_download_status) {
2827                 EM_DEBUG_EXCEPTION("input_mail_tbl_data->body_download_status[%p]", input_mail_tbl_data->body_download_status);
2828                 error = EMAIL_ERROR_INVALID_PARAM;
2829                 goto FINISH_OFF;
2830         }
2831
2832         if ( (error = emcore_make_envelope_from_mail(input_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
2833                 EM_DEBUG_EXCEPTION("emcore_make_envelope_from_mail failed [%d]", error);
2834                 goto FINISH_OFF;
2835         }
2836
2837         EM_DEBUG_LOG("input_mail_tbl_data->file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
2838         EM_DEBUG_LOG("input_mail_tbl_data->file_path_html[%s]", input_mail_tbl_data->file_path_html);
2839         EM_DEBUG_LOG("input_mail_tbl_data->file_path_mime_entity[%s]", input_mail_tbl_data->file_path_mime_entity);
2840         EM_DEBUG_LOG("input_mail_tbl_data->body->attachment_num[%d]", input_mail_tbl_data->attachment_count);
2841
2842         if ((input_mail_tbl_data->attachment_count > 0) || (input_mail_tbl_data->file_path_plain && input_mail_tbl_data->file_path_html))  {
2843                 EM_DEBUG_LOG("attachment_num [%d]", input_mail_tbl_data->attachment_count);
2844
2845                 root_body = mail_newbody();
2846
2847                 if (root_body == NULL)  {
2848                         EM_DEBUG_EXCEPTION("mail_newbody failed...");
2849                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2850                         goto FINISH_OFF;
2851                 }
2852
2853                 param = mail_newbody_parameter();
2854
2855                 if (param == NULL) {
2856                         EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2857                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2858                         goto FINISH_OFF;
2859                 }
2860
2861                 if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE) {
2862
2863                         root_body->type               = TYPEMULTIPART;
2864                         root_body->subtype            = strdup("MIXED");
2865
2866                         mail_free_body_parameter(&param);
2867                         param = NULL;
2868
2869                 } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED) {
2870                         PARAMETER *protocol_param     = mail_newbody_parameter();
2871
2872                         root_body->type               = TYPEMULTIPART;
2873                         root_body->subtype            = strdup("SIGNED");
2874
2875                         param->attribute       = cpystr("micalg");
2876                         switch (input_mail_tbl_data->digest_type) {
2877                         case DIGEST_TYPE_SHA1:
2878                                 param->value   = cpystr("sha1");
2879                                 break;
2880                         case DIGEST_TYPE_MD5:
2881                                 param->value   = cpystr("md5");
2882                                 break;
2883                         default:
2884                                 EM_DEBUG_EXCEPTION("Invalid digest type");
2885                                 break;
2886                         }
2887
2888                         protocol_param->attribute   = cpystr("protocol");
2889                         protocol_param->value       = cpystr("application/pkcs7-signature");
2890                         protocol_param->next        = NULL;
2891                         param->next                 = protocol_param;
2892
2893                         input_mail_tbl_data->file_path_plain = NULL;
2894                         input_mail_tbl_data->file_path_html = NULL;
2895
2896                         input_attachment_tbl = input_attachment_tbl + (input_attachment_count - 1);
2897
2898                         input_attachment_count = 1;
2899
2900                 } else {
2901
2902                         root_body->type    = TYPEAPPLICATION;
2903                         root_body->subtype = strdup("PKCS7-MIME");
2904
2905                         param->attribute = cpystr("name");
2906                         param->value = cpystr("smime.p7m");
2907                         param->next = NULL;
2908
2909                         input_mail_tbl_data->file_path_plain = NULL;
2910                         input_mail_tbl_data->file_path_html = NULL;
2911                         input_mail_tbl_data->file_path_mime_entity = NULL;
2912
2913                         input_attachment_count = 1;
2914                 }
2915
2916                 root_body->contents.text.data = NULL;
2917                 root_body->contents.text.size = 0;
2918                 root_body->size.bytes         = 0;
2919                 root_body->parameter          = param;
2920
2921                 if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE && input_mail_tbl_data->file_path_plain && input_mail_tbl_data->file_path_html) {
2922                         part_for_text = attach_mutipart_with_sub_type(root_body, "ALTERNATIVE", &error);
2923
2924                         if (!part_for_text) {
2925                                 EM_DEBUG_EXCEPTION("attach_mutipart_with_sub_type [part_for_text] failed [%d]", error);
2926                                 goto FINISH_OFF;
2927                         }
2928
2929                         text_body = &part_for_text->body;
2930
2931                         if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0) {
2932                                 EM_DEBUG_LOG("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
2933                                 if (!attach_part(text_body, (unsigned char *)input_mail_tbl_data->file_path_plain, 0, NULL, NULL, false, &error)) {
2934                                         EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
2935                                         goto FINISH_OFF;
2936                                 }
2937                         }
2938
2939                         if (input_mail_tbl_data->file_path_html && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_html) > 0) {
2940                                 EM_DEBUG_LOG("file_path_html[%s]", input_mail_tbl_data->file_path_html);
2941
2942                                 part_for_html = attach_mutipart_with_sub_type(text_body, "RELATED", &error);
2943                                 if (!part_for_html) {
2944                                         EM_DEBUG_EXCEPTION("attach_mutipart_with_sub_type [part_for_html] failed [%d]", error);
2945                                         goto FINISH_OFF;
2946                                 }
2947
2948                                 if (!attach_part(&(part_for_html->body) , (unsigned char *)input_mail_tbl_data->file_path_html, 0, NULL, "html", false, &error)) {
2949                                         EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
2950                                         goto FINISH_OFF;
2951                                 }
2952                         }
2953                 }
2954
2955                 if (input_mail_tbl_data->file_path_mime_entity && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_mime_entity) > 0) {
2956                         EM_DEBUG_LOG("file_path_mime_entity[%s]", input_mail_tbl_data->file_path_mime_entity);
2957                         root_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_mime_entity);
2958                 }
2959
2960                 if (input_attachment_tbl && input_attachment_count)  {
2961                         emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
2962                         char *name = NULL;
2963                         BODY *body_to_attach = NULL;
2964                         struct stat st_buf;
2965
2966                         for(i = 0; i < input_attachment_count; i++) {
2967                                 temp_attachment_tbl = input_attachment_tbl + i;
2968                                 EM_DEBUG_LOG("attachment_name[%s], attachment_path[%s]", temp_attachment_tbl->attachment_name, temp_attachment_tbl->attachment_path);
2969                                 if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0)  {
2970                                         if (!temp_attachment_tbl->attachment_name)  {
2971                                                 if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
2972                                                         EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
2973                                                         continue;
2974                                                 }
2975                                         }
2976                                         else
2977                                                 name = temp_attachment_tbl->attachment_name;
2978
2979                                         EM_DEBUG_LOG("name[%s]", name);
2980
2981                                         if (temp_attachment_tbl->attachment_inline_content_status && part_for_html)
2982                                                 body_to_attach = &(part_for_html->body);
2983                                         else
2984                                                 body_to_attach = root_body;
2985
2986                                         if (!attach_part(body_to_attach, (unsigned char *)temp_attachment_tbl->attachment_path, 0, name, NULL, temp_attachment_tbl->attachment_inline_content_status, &error))  {
2987                                                 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
2988                                                 continue;
2989                                         }
2990                                 }
2991                         }
2992                 }
2993                 text_body = NULL;
2994         }
2995         else {
2996                 text_body = mail_newbody();
2997
2998                 if (text_body == NULL)  {
2999                         EM_DEBUG_EXCEPTION("mail_newbody failed...");
3000
3001                         error = EMAIL_ERROR_OUT_OF_MEMORY;
3002                         goto FINISH_OFF;
3003                 }
3004
3005                 text_body->type = TYPETEXT;
3006                 text_body->encoding = ENC8BIT;
3007                 if (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)
3008                         text_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_plain ? input_mail_tbl_data->file_path_plain  :  input_mail_tbl_data->file_path_html);
3009                 else
3010                         text_body->sparep = NULL;
3011
3012                 if (input_mail_tbl_data->file_path_html != NULL && input_mail_tbl_data->file_path_html[0] != '\0')
3013                         text_body->subtype = strdup("html");
3014                 if (text_body->sparep)
3015                         text_body->size.bytes = EM_SAFE_STRLEN(text_body->sparep);
3016                 else
3017                         text_body->size.bytes = 0;
3018         }
3019
3020
3021         if (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) {
3022                 /*  Report mail */
3023                 EM_DEBUG_LOG("REPORT MAIL");
3024                 envelope->references = cpystr(input_mail_tbl_data->message_id);
3025         }
3026
3027         if (file_path)  {
3028                 EM_DEBUG_LOG("write rfc822 : file_path[%p]", file_path);
3029
3030                 if (part_for_html)
3031                         html_body = &(part_for_html->body);
3032
3033                 if (!emcore_write_rfc822(envelope, root_body ? root_body : text_body, html_body, input_mail_tbl_data->priority, input_mail_tbl_data->report_status, &fname, &error))  {
3034                         EM_DEBUG_EXCEPTION("emcore_write_rfc822 failed [%d]", error);
3035                         goto FINISH_OFF;
3036                 }
3037
3038                 *file_path = fname;
3039         }
3040
3041         ret = true;
3042
3043 FINISH_OFF:
3044         if ((ret == true) && (env != NULL))
3045                 *env = envelope;
3046         else if (envelope != NULL)
3047                 mail_free_envelope(&envelope);
3048
3049         if (text_body != NULL)
3050                 mail_free_body(&text_body);
3051
3052         if (root_body != NULL)
3053                 mail_free_body(&root_body);
3054
3055         if (err_code != NULL)
3056                 *err_code = error;
3057
3058         EM_DEBUG_FUNC_END("ret [%d]", ret);
3059         return ret;
3060 }
3061
3062 INTERNAL_FUNC int emcore_make_rfc822_file(email_mail_data_t *input_mail_tbl_data, email_attachment_data_t *input_attachment_tbl, int input_attachment_count, char **file_path, int *err_code)
3063 {
3064         EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], file_path[%p], err_code[%p]", input_mail_tbl_data, file_path, err_code);
3065
3066         int       ret                  = false;
3067         int       error                = EMAIL_ERROR_NONE;
3068         int       is_incomplete        = 0;
3069         int       i                    = 0;
3070         ENVELOPE *envelope             = NULL;
3071         BODY     *text_body            = NULL;
3072         BODY     *html_body            = NULL;
3073         BODY     *root_body            = NULL;
3074         PART     *part_for_html        = NULL;
3075         PART     *part_for_text        = NULL;
3076         char      temp_file_path_plain[512];
3077         char      temp_file_path_html[512];
3078         char     *pAdd                 = NULL;
3079         char     *fname                = NULL;
3080         emstorage_account_tbl_t *ref_account     = NULL;
3081
3082         if (!input_mail_tbl_data)  {
3083                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3084                 error = EMAIL_ERROR_INVALID_PARAM;
3085                 goto FINISH_OFF;
3086         }
3087
3088         if ( (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0 && !input_mail_tbl_data->body_download_status) {
3089                 EM_DEBUG_EXCEPTION("input_mail_tbl_data->body_download_status[%p]", input_mail_tbl_data->body_download_status);
3090                 error = EMAIL_ERROR_INVALID_PARAM;
3091                 goto FINISH_OFF;
3092         }
3093
3094         if (!emstorage_get_account_by_id(input_mail_tbl_data->account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &ref_account, true, &error)) {
3095                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", error);
3096                 goto FINISH_OFF;
3097         }
3098
3099         if (!ref_account)  {
3100                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
3101                 error = EMAIL_ERROR_INVALID_ACCOUNT;
3102                 goto FINISH_OFF;
3103         }
3104
3105         if (!(envelope = mail_newenvelope()))  {
3106                 EM_DEBUG_EXCEPTION("mail_newenvelope failed...");
3107                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3108                 goto FINISH_OFF;
3109         }
3110
3111         is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
3112
3113         if (is_incomplete)  {
3114                 if (ref_account->user_email_address && ref_account->user_email_address[0] != '\0')  {
3115                         char *p = cpystr(ref_account->user_email_address);
3116
3117                         if (p == NULL)  {
3118                                 EM_DEBUG_EXCEPTION("cpystr failed...");
3119                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3120                                 goto FINISH_OFF;
3121                         }
3122
3123                         EM_DEBUG_LOG("Assign envelop->from");
3124
3125                         if (input_mail_tbl_data->full_address_from) {
3126                                 char *temp_address_string = NULL ;
3127                                 em_skip_whitespace(input_mail_tbl_data->full_address_from , &temp_address_string);
3128                                 EM_DEBUG_LOG("address[temp_address_string][%s]", temp_address_string);
3129                                 rfc822_parse_adrlist(&envelope->from, temp_address_string, ref_account->outgoing_server_address);
3130                                 EM_SAFE_FREE(temp_address_string);
3131                                 temp_address_string = NULL ;
3132                         }
3133                         else
3134                                 envelope->from = rfc822_parse_mailbox(&p, NULL);
3135
3136                         EM_SAFE_FREE(p);
3137                         if (!envelope->from)  {
3138                                 EM_DEBUG_EXCEPTION("rfc822_parse_mailbox failed...");
3139                                 error = EMAIL_ERROR_INVALID_ADDRESS;
3140                                 goto FINISH_OFF;
3141                         }
3142                         else  {
3143
3144                                 if (envelope->from->personal == NULL) {
3145                                         envelope->from->personal =
3146                                                 (ref_account->user_display_name && ref_account->user_display_name[0] != '\0')?
3147                                                 cpystr(ref_account->user_display_name)  :  NULL;
3148                                 }
3149                         }
3150                 }
3151         }
3152
3153         if (ref_account->return_address && ref_account->return_address[0] != '\0')  {
3154                 char *p = cpystr(ref_account->return_address);
3155
3156                 if (p == NULL)  {
3157                         EM_DEBUG_EXCEPTION("cpystr failed...");
3158
3159                         error = EMAIL_ERROR_OUT_OF_MEMORY;
3160                         goto FINISH_OFF;
3161                 }
3162                 envelope->return_path = rfc822_parse_mailbox(&p, NULL);
3163                 EM_SAFE_FREE(p);
3164         }
3165         else  {
3166                 if (!input_mail_tbl_data->full_address_from || !input_mail_tbl_data->full_address_to)  {
3167                         EM_DEBUG_EXCEPTION("input_mail_tbl_data->full_address_from[%p], input_mail_tbl_data->full_address_to[%p]", input_mail_tbl_data->full_address_from, input_mail_tbl_data->full_address_to);
3168                         error = EMAIL_ERROR_INVALID_MAIL;
3169                         goto FINISH_OFF;
3170                 }
3171
3172                 int i, j;
3173
3174                 if (input_mail_tbl_data->full_address_from)  {
3175                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_from); i < j; i++)  {
3176                                 if (input_mail_tbl_data->full_address_from[i] == ';')
3177                                         input_mail_tbl_data->full_address_from[i] = ',';
3178                         }
3179                 }
3180
3181                 if (input_mail_tbl_data->full_address_return)  {
3182                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_return); i < j; i++)  {
3183                                 if (input_mail_tbl_data->full_address_return[i] == ';')
3184                                         input_mail_tbl_data->full_address_return[i] = ',';
3185                         }
3186                 }
3187                         em_skip_whitespace(input_mail_tbl_data->full_address_from , &pAdd);
3188                 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3189
3190                 rfc822_parse_adrlist(&envelope->from, pAdd, ref_account->outgoing_server_address);
3191                 EM_SAFE_FREE(pAdd);
3192                         pAdd = NULL ;
3193
3194                 em_skip_whitespace(input_mail_tbl_data->full_address_return , &pAdd);
3195                 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3196
3197                 rfc822_parse_adrlist(&envelope->return_path, pAdd, ref_account->outgoing_server_address);
3198                 EM_SAFE_FREE(pAdd);
3199                 pAdd = NULL ;
3200         }
3201
3202         {
3203                 int i, j;
3204
3205                 if (input_mail_tbl_data->full_address_to)  {
3206                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_to); i < j; i++)  {
3207                                 if (input_mail_tbl_data->full_address_to[i] == ';')
3208                                         input_mail_tbl_data->full_address_to[i] = ',';
3209                         }
3210                 }
3211
3212                 if (input_mail_tbl_data->full_address_cc)  {
3213                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_cc); i < j; i++)  {
3214                                 if (input_mail_tbl_data->full_address_cc[i] == ';')
3215                                         input_mail_tbl_data->full_address_cc[i] = ',';
3216                         }
3217                 }
3218
3219                 if (input_mail_tbl_data->full_address_bcc)  {
3220                         for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_bcc); i < j; i++)  {
3221                                 if (input_mail_tbl_data->full_address_bcc[i] == ';')
3222                                         input_mail_tbl_data->full_address_bcc[i] = ',';
3223                         }
3224                 }
3225         }
3226
3227         em_skip_whitespace(input_mail_tbl_data->full_address_to , &pAdd);
3228         EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3229
3230         rfc822_parse_adrlist(&envelope->to, pAdd, ref_account->outgoing_server_address);
3231         EM_SAFE_FREE(pAdd);
3232         pAdd = NULL ;
3233
3234         EM_DEBUG_LOG("address[input_mail_tbl_data->full_address_cc][%s]", input_mail_tbl_data->full_address_cc);
3235         em_skip_whitespace(input_mail_tbl_data->full_address_cc , &pAdd);
3236         EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3237
3238         rfc822_parse_adrlist(&envelope->cc, pAdd, ref_account->outgoing_server_address);
3239         EM_SAFE_FREE(pAdd);
3240                 pAdd = NULL ;
3241
3242         em_skip_whitespace(input_mail_tbl_data->full_address_bcc , &pAdd);
3243         rfc822_parse_adrlist(&envelope->bcc, pAdd, ref_account->outgoing_server_address);
3244         EM_SAFE_FREE(pAdd);
3245                 pAdd = NULL ;
3246
3247         emcore_encode_rfc2047_address(envelope->return_path, &error);
3248         emcore_encode_rfc2047_address(envelope->from, &error);
3249         emcore_encode_rfc2047_address(envelope->sender, &error);
3250         emcore_encode_rfc2047_address(envelope->reply_to, &error);
3251         emcore_encode_rfc2047_address(envelope->to, &error);
3252         emcore_encode_rfc2047_address(envelope->cc, &error);
3253         emcore_encode_rfc2047_address(envelope->bcc, &error);
3254
3255         if (input_mail_tbl_data->subject)
3256                 envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
3257
3258         char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
3259         char localtime_string[DATE_STR_LENGTH] = {0, };
3260
3261         rfc822_date(rfc822_date_string);
3262
3263         if (!is_incomplete)  {
3264                 strftime(localtime_string, 128, "%a, %e %b %Y %H : %M : %S ", localtime(&input_mail_tbl_data->date_time));
3265                 /*  append last 5byes("+0900") */
3266                 g_strlcat(localtime_string, rfc822_date_string + (EM_SAFE_STRLEN(rfc822_date_string) -  5), DATE_STR_LENGTH);
3267                 envelope->date = (unsigned char *)cpystr((const char *)localtime_string);
3268         }
3269         else {
3270                 envelope->date = (unsigned char *)cpystr((const char *)rfc822_date_string);
3271         }
3272         /*  check report input_mail_tbl_data */
3273
3274         /* Non-report input_mail_tbl_data */
3275         EM_DEBUG_LOG("input_mail_tbl_data->file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3276         EM_DEBUG_LOG("input_mail_tbl_data->file_path_html[%s]", input_mail_tbl_data->file_path_html);
3277         EM_DEBUG_LOG("input_mail_tbl_data->body->attachment_num[%d]", input_mail_tbl_data->attachment_count);
3278
3279         if (input_mail_tbl_data->file_path_plain) {
3280                 memset(temp_file_path_plain, 0x00, sizeof(temp_file_path_plain));
3281                 SNPRINTF(temp_file_path_plain, sizeof(temp_file_path_plain), "%s%s%s", MAILTEMP, DIR_SEPERATOR, "UTF-8");
3282
3283                 if (!emstorage_copy_file(input_mail_tbl_data->file_path_plain, temp_file_path_plain, 0, &error)) {
3284                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed : [%d]", error);
3285                         goto FINISH_OFF;
3286                 }
3287         }
3288
3289         if (input_mail_tbl_data->file_path_html) {
3290                 memset(temp_file_path_html, 0x00, sizeof(temp_file_path_html));
3291                 SNPRINTF(temp_file_path_html, sizeof(temp_file_path_html), "%s%s%s", MAILTEMP, DIR_SEPERATOR, "UTF-8.htm");
3292
3293                 if (!emstorage_copy_file(input_mail_tbl_data->file_path_html, temp_file_path_html, 0, &error)) {
3294                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed : [%d]", error);
3295                         goto FINISH_OFF;
3296                 }
3297         }
3298
3299         if ((input_mail_tbl_data->attachment_count > 0) || (input_mail_tbl_data->file_path_plain && input_mail_tbl_data->file_path_html))  {
3300                 EM_DEBUG_LOG("attachment_num  :  %d", input_mail_tbl_data->attachment_count);
3301                 root_body = mail_newbody();
3302
3303                 if (root_body == NULL)  {
3304                         EM_DEBUG_EXCEPTION("mail_newbody failed...");
3305                         error = EMAIL_ERROR_OUT_OF_MEMORY;
3306                         goto FINISH_OFF;
3307                 }
3308
3309                 root_body->type               = TYPEMULTIPART;
3310                 root_body->subtype            = strdup("MIXED");
3311                 root_body->contents.text.data = NULL;
3312                 root_body->contents.text.size = 0;
3313                 root_body->size.bytes         = 0;
3314
3315                 part_for_text = attach_mutipart_with_sub_type(root_body, "ALTERNATIVE", &error);
3316
3317                 if (!part_for_text) {
3318                         EM_DEBUG_EXCEPTION("attach_mutipart_with_sub_type [part_for_text] failed [%d]", error);
3319                         goto FINISH_OFF;
3320                 }
3321
3322                 text_body = &part_for_text->body;
3323
3324                 if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0)  {
3325                         EM_DEBUG_LOG("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
3326                         if (!attach_part(text_body, (unsigned char *)temp_file_path_plain, 0, NULL, NULL, false, &error))  {
3327                                 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3328                                 goto FINISH_OFF;
3329                         }
3330                 }
3331
3332                 if (input_mail_tbl_data->file_path_html && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_html) > 0)  {
3333                         EM_DEBUG_LOG("file_path_html[%s]", input_mail_tbl_data->file_path_html);
3334
3335                         part_for_html = attach_mutipart_with_sub_type(text_body, "RELATED", &error);
3336                         if (!part_for_html) {
3337                                 EM_DEBUG_EXCEPTION("attach_mutipart_with_sub_type [part_for_html] failed [%d]", error);
3338                                 goto FINISH_OFF;
3339                         }
3340
3341                         if (!attach_part(&(part_for_html->body) , (unsigned char *)temp_file_path_html, 0, NULL, "html", false, &error))  {
3342                                 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3343                                 goto FINISH_OFF;
3344                         }
3345                 }
3346
3347                 if (input_attachment_tbl && input_attachment_count)  {
3348                         email_attachment_data_t *temp_attachment_tbl = NULL;
3349                         char *name = NULL;
3350                         BODY *body_to_attach = NULL;
3351                         struct stat st_buf;
3352
3353                         for(i = 0; i < input_attachment_count; i++) {
3354                                 temp_attachment_tbl = input_attachment_tbl + i;
3355                                 EM_DEBUG_LOG("attachment_name[%s], attachment_path[%s]", temp_attachment_tbl->attachment_name, temp_attachment_tbl->attachment_path);
3356                                 if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0)  {
3357                                         if (!temp_attachment_tbl->attachment_name)  {
3358                                                 if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
3359                                                         EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
3360                                                         continue;
3361                                                 }
3362                                         }
3363                                         else
3364                                                 name = temp_attachment_tbl->attachment_name;
3365                                         EM_DEBUG_LOG("name[%s]", name);
3366
3367                                         if (temp_attachment_tbl->inline_content_status && part_for_html)
3368                                                 body_to_attach = &(part_for_html->body);
3369                                         else
3370                                                 body_to_attach = root_body;
3371
3372                                         if (!attach_part(body_to_attach, (unsigned char *)temp_attachment_tbl->attachment_path, 0, name, NULL, temp_attachment_tbl->inline_content_status, &error))  {
3373                                                 EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
3374                                                 continue;
3375                                         }
3376                                 }
3377                         }
3378                 }
3379                 text_body = NULL;
3380         } else  {
3381                 text_body = mail_newbody();
3382
3383                 if (text_body == NULL)  {
3384                         EM_DEBUG_EXCEPTION("mail_newbody failed...");
3385
3386                         error = EMAIL_ERROR_OUT_OF_MEMORY;
3387                         goto FINISH_OFF;
3388                 }
3389
3390                 text_body->type = TYPETEXT;
3391                 text_body->encoding = ENC8BIT;
3392                 if (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)
3393                         text_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_plain ? temp_file_path_plain  :  temp_file_path_html);
3394                 else
3395                         text_body->sparep = NULL;
3396
3397                 if (input_mail_tbl_data->file_path_html != NULL && input_mail_tbl_data->file_path_html[0] != '\0')
3398                         text_body->subtype = strdup("html");
3399                 if (text_body->sparep)
3400                         text_body->size.bytes = EM_SAFE_STRLEN(text_body->sparep);
3401                 else
3402                         text_body->size.bytes = 0;
3403         }
3404
3405         if (file_path)  {
3406                 EM_DEBUG_LOG("write rfc822  :  file_path[%s]", file_path);
3407
3408                 if (part_for_html)
3409                         html_body = &(part_for_html->body);
3410
3411                 if (!emcore_write_rfc822(envelope, root_body ? root_body  :  text_body, html_body, input_mail_tbl_data->priority, input_mail_tbl_data->report_status, &fname, &error))  {
3412                         EM_DEBUG_EXCEPTION("emcore_write_rfc822 failed [%d]", error);
3413                         goto FINISH_OFF;
3414                 }
3415
3416                 *file_path = fname;
3417         }
3418
3419         if (EM_SAFE_STRLEN(temp_file_path_plain) > 0)  {
3420                 if (!emstorage_delete_file(temp_file_path_plain, &error))  {
3421                         EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", error);
3422                         goto FINISH_OFF;
3423                 }
3424         }
3425
3426         if (EM_SAFE_STRLEN(temp_file_path_html) > 0) {
3427                 if (!emstorage_delete_file(temp_file_path_html, &error))  {
3428                         EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", error);
3429                         goto FINISH_OFF;
3430                 }
3431         }
3432
3433         ret = true;
3434
3435 FINISH_OFF:
3436
3437         emstorage_free_account(&ref_account, 1, NULL);
3438
3439         if (envelope != NULL)
3440                 mail_free_envelope(&envelope);
3441
3442         if (text_body != NULL)
3443                 mail_free_body(&text_body);
3444
3445         if (root_body != NULL)
3446                 mail_free_body(&root_body);
3447
3448         if (err_code != NULL)
3449                 *err_code = error;
3450
3451         EM_DEBUG_FUNC_END("ret [%d]", ret);
3452         return ret;
3453 }
3454
3455 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
3456 static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body, int *err_code)
3457 {
3458         EM_DEBUG_FUNC_BEGIN("envelope[%p], mulitpart_body[%p], err_code[%p]", envelope, multipart_body, err_code);
3459
3460         int ret = false;
3461         int err = EMAIL_ERROR_NONE;
3462
3463         BODY *m_body = NULL;
3464         BODY *p_body = NULL;
3465         BODY *text_body = NULL;
3466         PARAMETER *param = NULL;
3467         emstorage_attachment_tbl_t temp_attachment_tbl;
3468         FILE *fp = NULL;
3469         char *fname = NULL;
3470         char buf[512] = {0x00, };
3471         int sz = 0;
3472
3473         if (!envelope || !multipart_body)  {
3474                 EM_DEBUG_EXCEPTION(" envelope[%p], mulitpart_body[%p]", envelope, multipart_body);
3475                 err = EMAIL_ERROR_INVALID_PARAM;
3476                 goto FINISH_OFF;
3477         }
3478
3479         if (!(text_body = mail_newbody()))  {
3480                 EM_DEBUG_EXCEPTION(" mail_newbody failed...");
3481                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3482                 goto FINISH_OFF;
3483         }
3484
3485         if (!emcore_get_temp_file_name(&fname, &err))  {
3486                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed [%d]", err);
3487                 goto FINISH_OFF;
3488         }
3489
3490         if (!(fp = fopen(fname, "wb+")))  {
3491                 EM_DEBUG_EXCEPTION(" fopen failed - %s", fname);
3492                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3493                 goto FINISH_OFF;
3494         }
3495
3496         if (!envelope->from || !envelope->from->mailbox || !envelope->from->host)  {
3497                 if (!envelope->from)
3498                         EM_DEBUG_EXCEPTION(" envelope->from[%p]", envelope->from);
3499                 else
3500                         EM_DEBUG_LOG(" envelope->from->mailbox[%p], envelope->from->host[%p]", envelope->from->mailbox, envelope->from->host);
3501
3502                 err = EMAIL_ERROR_INVALID_PARAM;
3503                 goto FINISH_OFF;
3504         }
3505
3506         /*
3507         if (envelope->from->personal)
3508                 SNPRINTF(buf, sizeof(buf), "%s <%s@%s>", envelope->from->personal, envelope->from->mailbox, envelope->from->host);
3509         else
3510         */
3511                 SNPRINTF(buf, sizeof(buf), "%s@%s", envelope->from->mailbox, envelope->from->host);
3512
3513         fprintf(fp, "Your message has been read by %s"CRLF_STRING, buf);
3514         fprintf(fp, "Date :  %s", envelope->date);
3515
3516         fclose(fp); fp = NULL;
3517
3518         if (!emcore_get_file_size(fname, &sz, &err))  {
3519                 EM_DEBUG_EXCEPTION(" emcore_get_file_size failed [%d]", err);
3520                 goto FINISH_OFF;
3521         }
3522
3523         text_body->type = TYPETEXT;
3524         text_body->encoding = ENC8BIT;
3525         text_body->sparep = EM_SAFE_STRDUP(fname);
3526         text_body->size.bytes = (unsigned long)sz;
3527
3528         if (!emcore_get_temp_file_name(&fname, &err))  {
3529                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed [%d]", err);
3530                 goto FINISH_OFF;
3531         }
3532
3533         if (!(fp = fopen(fname, "wb+")))  {
3534                 EM_DEBUG_EXCEPTION(" fopen failed - %s", fname);
3535                 err = EMAIL_ERROR_SYSTEM_FAILURE;               /* EMAIL_ERROR_UNKNOWN; */
3536                 goto FINISH_OFF;
3537         }
3538
3539         if (!envelope->references)  {
3540                 EM_DEBUG_EXCEPTION(" envelope->references[%p]", envelope->references);
3541                 err = EMAIL_ERROR_INVALID_PARAM;
3542                 goto FINISH_OFF;
3543         }
3544
3545         fprintf(fp, "Final-Recipient :  rfc822;%s@%s\r", envelope->from->mailbox, envelope->from->host);
3546         fprintf(fp, "Original-Message-ID:  %s\r", envelope->references);
3547         fprintf(fp, "Disposition :  manual-action/MDN-sent-manually; displayed");
3548
3549         fclose(fp); fp = NULL;
3550
3551         memset(&temp_attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3552
3553         temp_attachment_tbl.attachment_path = EM_SAFE_STRDUP(fname);
3554
3555         if (!emcore_get_file_size(fname, &temp_attachment_tbl.attachment_size, &err))  {
3556                 EM_DEBUG_EXCEPTION(" emcore_get_file_size failed [%d]", err);
3557                 goto FINISH_OFF;
3558         }
3559
3560         if (!attach_attachment_to_body(&m_body, text_body, &temp_attachment_tbl, 1, &err))  {
3561                 EM_DEBUG_EXCEPTION(" attach_attachment_to_body failed [%d]", err);
3562                 goto FINISH_OFF;
3563         }
3564
3565         text_body->contents.text.data = NULL;
3566
3567         /*  change mail header */
3568
3569         /*  set content-type to multipart/report */
3570         m_body->subtype = strdup("report");
3571
3572         /*  set report-type parameter in content-type */
3573         param = em_malloc(sizeof(PARAMETER));
3574         if (!param)  {
3575                 EM_DEBUG_EXCEPTION(" malloc failed...");
3576                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3577                 goto FINISH_OFF;
3578         }
3579
3580         param->attribute  = strdup("report-type");
3581         param->value      = strdup("disposition-notification");
3582         param->next       = m_body->parameter;
3583
3584         m_body->parameter = param;
3585
3586         /*  change body-header */
3587
3588         p_body = &m_body->nested.part->next->body;
3589
3590         /*  set content-type to message/disposition-notification */
3591         p_body->type      = TYPEMESSAGE;
3592         p_body->encoding  = ENC7BIT;
3593
3594         EM_SAFE_FREE(p_body->subtype);
3595
3596         p_body->subtype = strdup("disposition-notification");
3597
3598         /*  set parameter */
3599         mail_free_body_parameter(&p_body->parameter);
3600         mail_free_body_parameter(&p_body->disposition.parameter);
3601
3602         EM_SAFE_FREE(p_body->disposition.type);
3603
3604         p_body->disposition.type = strdup("inline");
3605
3606         ret = true;
3607
3608 FINISH_OFF:
3609         if ((ret == true) && (multipart_body != NULL))
3610                 *multipart_body = m_body;
3611         else if (m_body != NULL)
3612                 mail_free_body(&m_body);
3613
3614         if (text_body != NULL)
3615                 mail_free_body(&text_body);
3616
3617         if (fp != NULL)
3618                 fclose(fp);
3619
3620         EM_SAFE_FREE(fname);
3621
3622         if (err_code != NULL)
3623                 *err_code = err;
3624
3625         EM_DEBUG_FUNC_END("err [%d]", err);
3626         return ret;
3627 }
3628 #endif
3629
3630 INTERNAL_FUNC int emcore_get_body_buff(char *file_path, char **buff)
3631 {
3632         EM_DEBUG_FUNC_BEGIN();
3633
3634         FILE *r_fp = NULL;
3635         int read_size = 0;
3636         int ret = false;
3637         char *read_buff = NULL;
3638
3639
3640         if (file_path)
3641                 r_fp = fopen(file_path, "r");
3642
3643         if (!r_fp) {
3644                 EM_DEBUG_EXCEPTION(" Filename %s failed to open", file_path);
3645                 goto FINISH_OFF;
3646         }
3647
3648
3649         struct stat stbuf;
3650         stat(file_path, &stbuf);
3651         EM_DEBUG_LOG(" File Size [ %d ] ", stbuf.st_size);
3652         read_buff = calloc(1, (stbuf.st_size+ 1));
3653         read_size = fread(read_buff, 1, stbuf.st_size, r_fp);
3654         read_buff[stbuf.st_size] = '\0';
3655
3656         if (ferror(r_fp)) {
3657                 EM_DEBUG_EXCEPTION("file read failed - %s", file_path);
3658                 EM_SAFE_FREE(read_buff);
3659                 goto FINISH_OFF;
3660         }
3661         ret = true;
3662         *buff = read_buff;
3663
3664 FINISH_OFF:
3665         if (r_fp)
3666                 fclose(r_fp);
3667
3668         return ret;
3669 }
3670
3671 static int emcore_copy_attachment_from_original_mail(int input_original_mail_id, int input_target_mail_id)
3672 {
3673         EM_DEBUG_FUNC_BEGIN("input_original_mail_id[%d] input_target_mail_id[%d]", input_original_mail_id, input_target_mail_id);
3674         int err = EMAIL_ERROR_NONE;
3675         int i = 0, j = 0;
3676         int original_mail_attachment_count = 0;
3677         int target_mail_attachment_count = 0;
3678         int attachment_id = 0;
3679         char output_file_name[MAX_PATH] = { 0, };
3680         char output_file_path[MAX_PATH] = { 0, };
3681         emstorage_attachment_tbl_t *original_mail_attachment_array = NULL;
3682         emstorage_attachment_tbl_t *target_mail_attachment_array = NULL;
3683         emstorage_attachment_tbl_t *target_attach = NULL;
3684
3685         if((err = emstorage_get_attachment_list(input_original_mail_id, false, &original_mail_attachment_array, &original_mail_attachment_count)) != EMAIL_ERROR_NONE) {
3686                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
3687                 goto FINISH_OFF;
3688         }
3689
3690         if((err = emstorage_get_attachment_list(input_target_mail_id, false, &target_mail_attachment_array, &target_mail_attachment_count)) != EMAIL_ERROR_NONE) {
3691                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
3692                 goto FINISH_OFF;
3693         }
3694
3695         for(i = 0; i < original_mail_attachment_count; i++) {
3696                 for(j = 0; j < target_mail_attachment_count; j++) {
3697                         if(strcmp(original_mail_attachment_array[i].attachment_name, target_mail_attachment_array[j].attachment_name) == 0 ) {
3698                                 target_attach = target_mail_attachment_array + j;
3699
3700                                 /* If attachment is inline content, fild path should not include attachment id */
3701                                 if(target_attach->attachment_inline_content_status == 1)
3702                                         attachment_id = 0;
3703                                 else
3704                                         attachment_id = target_attach->attachment_id;
3705
3706                                 EM_DEBUG_LOG("attachment_inline_content_status [%d] attachment_id[%d]", target_attach->attachment_inline_content_status, attachment_id);
3707
3708                                 if(!emcore_save_mail_file(target_attach->account_id, target_attach->mail_id, attachment_id, original_mail_attachment_array[i].attachment_path, original_mail_attachment_array[i].attachment_name, output_file_path, &err)) {
3709                                         EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
3710                                         goto FINISH_OFF;
3711                                 }
3712
3713                                 EM_SAFE_FREE(target_attach->attachment_path);
3714                                 target_attach->attachment_path = EM_SAFE_STRDUP(output_file_path);
3715                                 target_attach->attachment_save_status = 1;
3716
3717                                 if(!emstorage_update_attachment(target_attach, false, &err)) {
3718                                         EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3719                                         goto FINISH_OFF;
3720                                 }
3721                                 memset(output_file_path, 0, MAX_PATH);
3722                                 memset(output_file_name, 0, MAX_PATH);
3723                                 break;
3724                         }
3725                 }
3726         }
3727
3728 FINISH_OFF:
3729         if(original_mail_attachment_array)
3730                 emstorage_free_attachment(&original_mail_attachment_array, original_mail_attachment_count, NULL);
3731         if(target_mail_attachment_array)
3732                 emstorage_free_attachment(&target_mail_attachment_array, target_mail_attachment_count, NULL);
3733
3734
3735         EM_DEBUG_FUNC_END("err[%d]", err);
3736         return err;
3737 }
3738
3739 /*  send a mail */
3740 INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id)
3741 {
3742         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d]", input_mail_id);
3743         int err = EMAIL_ERROR_NONE;
3744         int i = 0;
3745         int attachment_count = 0;
3746         email_mail_data_t *mail_to_be_sent = NULL;
3747         email_mail_data_t *original_mail = NULL;
3748         email_attachment_data_t *attachment_array = NULL;
3749
3750         /* Get mail data */
3751         if((err = emcore_get_mail_data(input_mail_id, &mail_to_be_sent)) != EMAIL_ERROR_NONE) {
3752                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
3753                 goto FINISH_OFF;
3754         }
3755
3756         if(mail_to_be_sent->reference_mail_id <= 0) {
3757                 err = EMAIL_ERROR_INVALID_REFERENCE_MAIL;
3758                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_REFERENCE_MAIL");
3759                 goto FINISH_OFF;
3760         }
3761
3762         /* Get original mail data */
3763         if((err = emcore_get_mail_data(mail_to_be_sent->reference_mail_id, &original_mail)) != EMAIL_ERROR_NONE) {
3764                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
3765                 goto FINISH_OFF;
3766         }
3767
3768         /* Check necessity of download */
3769         if((err = emcore_get_attachment_data_list(original_mail->mail_id, &attachment_array, &attachment_count)) != EMAIL_ERROR_NONE) {
3770                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
3771                 goto FINISH_OFF;
3772         }
3773
3774         /* If need be, download attachments */
3775         for(i = 0; i < attachment_count; i++) {
3776                 if(attachment_array[i].save_status != 1) {
3777                         if(!emcore_download_attachment(original_mail->account_id, original_mail->mail_id, i + 1, &err)) {
3778                                 EM_DEBUG_EXCEPTION("emcore_download_attachment failed [%d]", err);
3779                                 goto FINISH_OFF;
3780                         }
3781                 }
3782         }
3783
3784         /* Copy attachment to the mail to be sent */
3785         if((err = emcore_copy_attachment_from_original_mail(original_mail->mail_id, mail_to_be_sent->mail_id)) != EMAIL_ERROR_NONE) {
3786                 EM_DEBUG_EXCEPTION("emcore_download_attachment failed [%d]", err);
3787                 goto FINISH_OFF;
3788         }
3789
3790         /* Send the mail */
3791         if(!emcore_send_mail(mail_to_be_sent->account_id, mail_to_be_sent->mailbox_id, mail_to_be_sent->mail_id, &err)) {
3792                 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
3793                 goto FINISH_OFF;
3794         }
3795 FINISH_OFF:
3796
3797         if(attachment_array)
3798                 emcore_free_attachment_data(&attachment_array, attachment_count, NULL);
3799
3800         if(mail_to_be_sent) {
3801                 emcore_free_mail_data(mail_to_be_sent);
3802                 EM_SAFE_FREE(mail_to_be_sent);
3803         }
3804
3805         if(original_mail) {
3806                 emcore_free_mail_data(original_mail);
3807                 EM_SAFE_FREE(original_mail);
3808         }
3809
3810
3811         EM_DEBUG_FUNC_END("err [%d]", err);
3812         return err;
3813 }