4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <sys/types.h>
29 #include "email-internal-types.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"
50 #include "email-debug-log.h"
52 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
53 static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body, int *err_code);
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);
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);
64 #ifndef __FEATURE_SEND_OPTMIZATION__
65 extern long smtp_soutr(void *stream, char *s);
68 #ifdef __FEATURE_SEND_OPTMIZATION__
69 extern long smtp_soutr_test(void *stream, char *s);
75 void mail_send_notify(email_send_status_t status, int total, int sent, int account_id, int mail_id, int err_code)
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);
80 case EMAIL_SEND_CONNECTION_FAIL:
81 case EMAIL_SEND_FINISH:
85 case EMAIL_SEND_PROGRESS:
89 emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, total, sent, status, account_id, mail_id, -1, err_code);
93 /* ------ rfc822 handle ---------------------------------------------------*/
94 long buf_flush(void *stream, char *string)
96 EM_DEBUG_FUNC_BEGIN("stream[%p], string[%s]", stream, string);
102 #define RFC822_READ_BLOCK_SIZE 1024
103 #define RFC822_STRING_BUFFER_SIZE 1536
105 static char *emcore_find_img_tag(char *source_string)
107 EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
109 int cur = 0, string_length;
113 string_length = EM_SAFE_STRLEN(source_string);
115 for (cur = 0; cur < string_length; cur++) {
116 if (source_string[cur] == 'I' || source_string[cur] == 'i') {
118 if (source_string[cur] == 'M' || source_string[cur] == 'm') {
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;
127 EM_DEBUG_FUNC_END("Can't find");
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)
134 EM_DEBUG_FUNC_BEGIN("source_string[%p], html_body[%p], err_code[%p]", source_string, html_body, err_code);
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;
141 if (!source_string || !html_body) {
142 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
143 err = EMAIL_ERROR_INVALID_PARAM;
147 input_string = EM_SAFE_STRDUP(source_string);
149 cur_part = html_body->nested.part;
152 cur_body = &(cur_part->body);
153 result_string = NULL; /*prevent 37865*/
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);
160 EM_SAFE_STRNCPY(content_id_buffer, cur_body->id + 1, CONTENT_ID_BUFFER_SIZE - 1); /* Removing <, > */
162 char *last_bracket = rindex(content_id_buffer, '>');
163 *last_bracket = NULL_CHAR;
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);
175 EM_SAFE_FREE(input_string);
176 input_string = result_string;
178 cur_part = cur_part->next;
182 result_string = EM_SAFE_STRDUP(input_string);
185 EM_SAFE_FREE(input_string);
189 EM_DEBUG_FUNC_END("ret[%s]", result_string);
190 return result_string;
193 static int emcore_write_body(BODY *body, BODY *html_body, FILE *fp, int *err_code)
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;
200 char *replaced_string = NULL;
201 int fd, nread, nwrite, error = EMAIL_ERROR_NONE;
204 file_path = body->sparep;
206 if (!file_path || EM_SAFE_STRLEN(file_path) == 0) {
207 EM_DEBUG_LOG("There is no file path");
208 switch (body->encoding) {
212 p = cpystr((const char *)body->contents.text.data);
213 len = body->contents.text.size;
218 EM_DEBUG_LOG("p[%s]", p);
219 fprintf(fp, "%s"CRLF_STRING CRLF_STRING, p);
223 EM_SAFE_FREE(body->sparep);
228 EM_DEBUG_LOG("Opening a file[%s]", file_path);
229 fd = open(file_path, O_RDONLY);
232 EM_DEBUG_EXCEPTION("open(\"%s\") failed...", file_path);
237 memset(&buf, 0x00, RFC822_STRING_BUFFER_SIZE + 1);
238 nread = read(fd, buf, (body->encoding == ENCBASE64 ? 57 : RFC822_READ_BLOCK_SIZE - 2));
241 EM_DEBUG_LOG("Can't read anymore : nread[%d]", nread);
248 /* EM_DEBUG_LOG("body->type[%d], body->subtype[%c]", body->type, body->subtype[0]); */
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);
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);
263 EM_DEBUG_LOG("emcore_replace_inline_image_path_with_content_id failed[%d]", error);
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);
273 p = (char *)rfc822_binary((void *)buf, (unsigned long)nread, (unsigned long *)&len);
281 nwrite = fprintf(fp, "%s", (p ? p : buf));
286 EM_DEBUG_EXCEPTION("fprintf failed nwrite[%d], len[%d]", nwrite, len);
292 if (body->encoding == ENCQUOTEDPRINTABLE || body->encoding == ENCBASE64)
293 fprintf(fp, CRLF_STRING);
295 fprintf(fp, CRLF_STRING);
308 static int emcore_write_rfc822_body(BODY *body, BODY *html_body, FILE *fp, int *err_code)
310 EM_DEBUG_FUNC_BEGIN("body[%p], html_body[%p], fp[%p], err_code[%p]", body, html_body, fp, err_code);
312 PARAMETER *param = NULL;
314 char *p = NULL, *bndry = NULL, buf[1025];
315 int error = EMAIL_ERROR_NONE;
317 switch (body->type) {
319 EM_DEBUG_LOG("body->type = TYPEMULTIPART");
320 part = body->nested.part;
322 for (param = body->parameter; param; param = param->next) {
323 if (strcasecmp(param->attribute, "BOUNDARY") == 0) {
324 bndry = param->value;
330 p = buf; p[0] = '\0';
332 rfc822_write_body_header(&p, &part->body);
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);
340 fprintf(fp, "--%s"CRLF_STRING, bndry);
343 fprintf(fp, "%s"CRLF_STRING, buf);
345 emcore_write_rfc822_body(&part->body, html_body, fp, err_code);
346 } while ((part = part->next));
348 fprintf(fp, "--%s--"CRLF_STRING, bndry);
352 EM_DEBUG_LOG("body->type is not TYPEMULTIPART");
354 if (!emcore_write_body(body, html_body, fp, &error)) {
355 EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
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)
368 EM_DEBUG_FUNC_BEGIN("env[%p], body[%p], data[%p], err_code[%p]", env, body, data, err_code);
371 int error = EMAIL_ERROR_NONE;
379 EM_DEBUG_EXCEPTION("Invalid Parameters");
380 error = EMAIL_ERROR_INVALID_PARAM;
386 rfc822_encode_body_7bit(env, body); /* if contents.text.data isn't NULL, the data will be encoded. */
388 /* FIXME : create memory map for this file */
389 p_len = (env->subject ? EM_SAFE_STRLEN(env->subject) : 0) + 8192;
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;
400 /* write at start of buffer */
401 buf.end = (buf.beg = buf.cur = p) + p_len - 1;
406 /* rfc822_output_header(&buf, env, body, NIL, T); */ /* including BCC */
407 rfc822_output_header(&buf, env, body, NIL, NIL); /* Excluding BCC */
409 *buf.cur = '\0'; /* tie off buffer */
411 gchar **tokens = g_strsplit(p, "CHARSET=X-UNKNOWN", 2);
413 if (g_strv_length(tokens) > 1) {
417 charset = g_path_get_basename(body->sparep);
419 if (charset != NULL) {
420 if ((pHtml = strstr(charset, ".htm")) != NULL)
421 charset[pHtml-charset] = '\0';
424 SNPRINTF(p, p_len, "%sCHARSET=%s%s", tokens[0], charset, tokens[1]);
428 EM_DEBUG_EXCEPTION("body->sparep is NULL");
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]);
440 EM_DEBUG_LOG(" =============================================================================== "
441 LF_STRING"%s"LF_STRING
442 " =============================================================================== ", p);
444 if (EM_SAFE_STRLEN(p) > 2)
445 *(p + EM_SAFE_STRLEN(p) - 2) = '\0';
448 if (input_report_flag) {
449 char buf[512] = {0x00, };
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" */
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" */
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);
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);
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);
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);
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);
490 SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), CRLF_STRING);
492 if (!emcore_get_temp_file_name(&fname, &error)) {
493 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed[%d]", error);
497 if (!(fp = fopen(fname, "w+"))) {
498 EM_DEBUG_EXCEPTION("fopen failed[%s]", fname);
499 error = EMAIL_ERROR_SYSTEM_FAILURE;
503 fprintf(fp, "%s", p);
506 if (!emcore_write_rfc822_body(body, html_body, fp, &error)) {
507 EM_DEBUG_EXCEPTION("emcore_write_rfc822_body failed[%d]", error);
519 #ifdef USE_SYNC_LOG_FILE
520 emstorage_copy_file(fname, "/tmp/phone2pc.eml", false, NULL);
525 else if (fname != NULL) {
532 if (err_code != NULL)
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)
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);
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;
547 char name_buf[MAX_PATH] = {0x00, };
548 char *body_text_file_name = NULL;
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,};
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;
568 if (emcore_is_storage_full(&err) == true) {
569 EM_DEBUG_EXCEPTION("Storage is full");
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;
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");
585 mail_data = input_mail_data;
586 attachment_data_list = input_attachment_data_list;
587 attachment_count = input_attachment_count;
590 mailbox_id_target = mail_data->mailbox_id;
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;
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;
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;
623 if (!input_mail_data->full_address_from)
624 input_mail_data->full_address_from = EM_SAFE_STRDUP(account_tbl_item->user_email_address);
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);
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;
639 /* Create report mail body */
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);
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);
652 if (opt->block_address || opt->block_subject) {
653 int is_completed = false;
656 if (!opt->block_address)
657 type = EMAIL_FILTER_SUBJECT;
658 else if (!opt->block_subject)
659 type = EMAIL_FILTER_FROM;
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);
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);
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);
677 if (rule_matched >= 0 && mailbox_id_spam)
678 mailbox_id_target = mailbox_id_spam;
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);
686 if (!emstorage_increase_mail_id(&mail_data->mail_id, true, &err)) {
687 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
691 EM_DEBUG_LOG("mail_data->mail_size [%d]", mail_data->mail_size);
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. */
696 EM_DEBUG_LOG("input_from_eas [%d] mail_data->body_download_status [%d]", input_from_eas, mail_data->body_download_status);
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);
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); */
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;
716 EM_SAFE_STRNCPY(body_text_file_name, UNKNOWN_CHARSET_PLAIN_TEXT_FILE, MAX_PATH);
718 EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH);
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);
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);
730 if (mail_data->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_NONE)
731 mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
733 EM_SAFE_FREE(mail_data->file_path_plain);
734 mail_data->file_path_plain = EM_SAFE_STRDUP(name_buf);
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); */
741 EM_SAFE_FREE(body_text_file_name);
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;
750 EM_SAFE_STRNCPY(body_text_file_name, UNKNOWN_CHARSET_HTML_TEXT_FILE, MAX_PATH);
752 EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH);
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);
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);
765 if (mail_data->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_NONE)
766 mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
768 EM_SAFE_FREE(mail_data->file_path_html);
769 mail_data->file_path_html = EM_SAFE_STRDUP(name_buf);
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);
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);
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);
786 EM_SAFE_FREE(mail_data->file_path_mime_entity);
787 mail_data->file_path_mime_entity = EM_SAFE_STRDUP(name_buf);
790 if (!mail_data->date_time) {
792 mail_data->date_time = time(NULL);
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;
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++;
808 mail_data->inline_content_count = local_inline_content_count;
809 mail_data->attachment_count = local_attachment_count;
811 EM_DEBUG_LOG("inline_content_count [%d]", local_inline_content_count);
812 EM_DEBUG_LOG("attachment_count [%d]", local_attachment_count);
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);
819 if (err != EMAIL_ERROR_EMPTY_FILE)
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);
829 converted_mail_tbl->mailbox_id = mailbox_tbl->mailbox_id;
830 converted_mail_tbl->mailbox_name = EM_SAFE_STRDUP(mailbox_tbl->mailbox_name);
832 /* Fill address information */
833 emcore_fill_address_information_of_mail_tbl(converted_mail_tbl);
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");
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;
845 converted_mail_tbl->thread_id = thread_id;
850 thread_item_count = 2;
853 mail_data->thread_id = converted_mail_tbl->thread_id;
855 emstorage_begin_transaction(NULL, NULL, NULL);
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);
866 /* Update thread information */
867 EM_DEBUG_LOG("thread_item_count [%d]", thread_item_count);
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);
878 /* Insert attachment information to DB */
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;
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);
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);
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);
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);
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);
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;
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);
941 attachment_data_list[i].attachment_id = attachment_tbl.attachment_id;
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);
957 emstorage_commit_transaction(NULL, NULL, NULL);
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.");
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;
968 EM_DEBUG_LOG("emcore_remove_overflowed_mails failed [%d]", err);
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();
983 EM_SAFE_FREE(body_text_file_name);
985 if (account_tbl_item)
986 emstorage_free_account(&account_tbl_item, 1, NULL);
989 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
991 if (converted_mail_tbl)
992 emstorage_free_mail(&converted_mail_tbl, 1, NULL);
998 INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id)
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;
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);
1016 receipt_mail_data = em_malloc(sizeof(email_mail_data_t));
1018 if (!receipt_mail_data) {
1019 EM_DEBUG_EXCEPTION("em_malloc failed...");
1020 err = EMAIL_ERROR_OUT_OF_MEMORY;
1024 memcpy(receipt_mail_data, read_mail_data, sizeof(email_mail_data_t));
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);
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;
1037 SNPRINTF(receipt_mail_data->subject, EM_SAFE_STRLEN(read_mail_data->subject) + 7, "Read: %s", read_mail_data->subject);
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);
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;
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);
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);
1063 envelope->references = EM_SAFE_STRDUP(read_mail_data->message_id);
1065 if (!emcore_get_report_mail_body(envelope, &root_body, &err)) {
1066 EM_DEBUG_EXCEPTION("emcore_get_report_mail_body failed [%d]", err);
1070 receipt_mail_data->file_path_plain = EM_SAFE_STRDUP(root_body->nested.part->body.sparep);
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 */
1078 receipt_mail_data->attachment_count = 1;
1079 attachment_count = 1;
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;
1088 attachment_data->save_status = 1;
1089 attachment_data->attachment_path = EM_SAFE_STRDUP(root_body->nested.part->next->body.sparep);
1091 if (!emcore_get_file_name(attachment_data->attachment_path, &p, &err)) {
1092 EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", err);
1096 attachment_data->attachment_name = cpystr(p);
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);
1104 *output_receipt_mail_id = receipt_mail_data->mail_id;
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);
1115 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1118 emcore_free_mail_data_list(&read_mail_data, 1);
1121 emcore_free_attachment_data(&attachment_data, 1, NULL);
1123 if(receipt_mail_tbl_data)
1124 emstorage_free_mail(&receipt_mail_tbl_data, 1, NULL);
1126 EM_DEBUG_FUNC_END("err [%d]", err);
1130 INTERNAL_FUNC int emcore_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code)
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);
1134 int err = EMAIL_ERROR_NONE;
1136 if (!meeting_req || meeting_req->mail_id <= 0) {
1138 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
1140 err = EMAIL_ERROR_INVALID_PARAM;
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);
1157 EM_DEBUG_FUNC_END();
1162 INTERNAL_FUNC int emcore_send_mail(int account_id, int input_mailbox_id, int mail_id, int *err_code)
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);
1167 int err = EMAIL_ERROR_NONE, err2 = EMAIL_ERROR_NONE;
1168 int status = EMAIL_SEND_FAIL;
1169 int attachment_tbl_count = 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;
1181 emstorage_mailbox_tbl_t* local_mailbox = NULL;
1182 int dst_mailbox_id = 0;
1184 if (!account_id || !mail_id) {
1185 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1186 err = EMAIL_ERROR_INVALID_PARAM;
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;
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);
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);
1207 if (!emcore_check_send_mail_thread_status()) {
1208 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1209 err = EMAIL_ERROR_CANCELLED;
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);
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);
1226 if (!emcore_check_send_mail_thread_status()) {
1227 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1228 err = EMAIL_ERROR_CANCELLED;
1232 opt = &(ref_account->options);
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);
1240 #endif /* __FEATURE_SUPPORT_VALIDATION_SYSTEM__ */
1242 /*Update status flag to DB*/
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);
1250 if (!envelope || (!envelope->to && !envelope->cc && !envelope->bcc)) {
1251 EM_DEBUG_EXCEPTION(" no recipients found...");
1252 err = EMAIL_ERROR_NO_RECIPIENT;
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);
1262 if (!emcore_check_send_mail_thread_status()) {
1263 EM_DEBUG_EXCEPTION("emcore_check_send_mail_thread_status failed...");
1264 err = EMAIL_ERROR_CANCELLED;
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;
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);
1281 dst_mailbox_id = local_mailbox->mailbox_id;
1284 emstorage_free_mailbox(&local_mailbox, 1, NULL);
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);
1290 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1291 err = EMAIL_ERROR_CANCELLED;
1292 status = EMAIL_SEND_CONNECTION_FAIL;
1296 stream = (SENDSTREAM *)tmp_stream;
1298 if (!emcore_check_send_mail_thread_status()) {
1299 EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
1300 err = EMAIL_ERROR_CANCELLED;
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);
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;
1316 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
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);
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);
1330 dst_mailbox_id = local_mailbox->mailbox_id;
1333 emstorage_free_mailbox(&local_mailbox, 1, NULL);
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...");
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);
1348 dst_mailbox_id = local_mailbox->mailbox_id;
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 */ {
1356 emstorage_activity_tbl_t new_activity;
1359 if (false == emcore_get_next_activity_id(&activityid, &err)) {
1360 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed [%d] ", err);
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;
1372 if (!emcore_add_activity(&new_activity, &err)) {
1373 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed [%d] ", err);
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);
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;
1389 if (!emcore_delete_activity(&new_activity, &err)) {
1390 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_SAVEMAIL] [%d] ", err);
1398 /* On Successful Mail sent remove the Draft flag */
1399 mail_tbl_data->flags_draft_field = 0;
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);
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);
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);
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);
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);
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);
1434 if (EMAIL_MAIL_STATUS_SEND_CANCELED == mail_tbl_data->save_status)
1435 EM_DEBUG_LOG("EMAIL_MAIL_STATUS_SEND_CANCELED Already set for ");
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);
1445 emcore_free_account(ref_account);
1446 EM_SAFE_FREE(ref_account);
1449 #ifndef __FEATURE_KEEP_CONNECTION__
1452 #endif /* __FEATURE_KEEP_CONNECTION__ */
1454 mail_parameters(NULL, SET_SSLSTART, (void *)stls);
1456 if (attachment_tbl_data)
1457 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
1460 mail_free_envelope(&envelope);
1463 EM_DEBUG_LOG("REMOVE TEMP FILE : %s", fpath);
1469 emstorage_free_mailbox(&local_mailbox, 1, NULL);
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__
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);
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__
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);
1486 emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, err);
1490 emstorage_free_mail(&mail_tbl_data, 1, NULL);
1492 if (err_code != NULL)
1494 EM_PROFILE_END(profile_emcore_send_mail);
1495 EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
1499 /* send a saved all mails */
1500 INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_name, int *err_code)
1502 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_name[%p], err_code[%p]", account_id, input_mailbox_name, err_code);
1505 int err = EMAIL_ERROR_NONE;
1506 int status = EMAIL_SEND_FAIL;
1507 int *mail_ids = NULL;
1511 int attachment_tbl_count = 0;
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;
1524 if (!account_id || !input_mailbox_name) {
1525 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1526 err = EMAIL_ERROR_INVALID_PARAM;
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;
1537 FINISH_OFF_IF_CANCELED;
1539 opt = &(ref_account->options);
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);
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;
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);
1561 if (!emstorage_mail_search_end(handle, true, &err)) {
1562 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
1568 mail_send_notify(EMAIL_SEND_PREPARE, 0, 0, account_id, mail_ids[total], err);
1570 for (i = 0; i < total; i++) {
1571 FINISH_OFF_IF_CANCELED;
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);
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;
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);
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);
1598 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
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);
1605 FINISH_OFF_IF_CANCELED;
1607 /* connect mail server. */
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);
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);
1619 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
1620 err = EMAIL_ERROR_CANCELLED;
1622 status = EMAIL_SEND_CONNECTION_FAIL;
1626 stream = (SENDSTREAM *)tmp_stream;
1628 FINISH_OFF_IF_CANCELED;
1630 mail_send_notify(EMAIL_SEND_CONNECTION_SUCCEED, 0, 0, account_id, mail_ids[i], err);
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;
1640 mail_send_notify(EMAIL_SEND_START, 0, 0, account_id, mail_ids[i], err);
1643 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
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);
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);
1655 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
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);
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);
1665 dst_mailbox_id = local_mailbox->mailbox_id;
1667 emcore_move_mail(&mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, NULL);
1670 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1675 searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
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);
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);
1689 dst_mailbox_id = local_mailbox->mailbox_id;
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);
1695 emstorage_free_mailbox(&local_mailbox, 1, NULL);
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);
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);
1707 if(searched_mail_tbl_data) {
1708 emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
1709 searched_mail_tbl_data = NULL;
1712 if(attachment_tbl_data)
1713 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
1715 mail_free_envelope(&envelope); envelope = NULL;
1719 EM_SAFE_FREE(fpath);
1729 emcore_free_account(ref_account);
1730 EM_SAFE_FREE(ref_account);
1737 mail_parameters(NIL, SET_SSLSTART, (void *)stls);
1740 mail_free_envelope(&envelope);
1743 if (!emstorage_mail_search_end(handle, true, &err))
1744 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
1747 if (searched_mail_tbl_data)
1748 emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
1750 if(attachment_tbl_data)
1751 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
1755 EM_SAFE_FREE(fpath);
1759 emstorage_free_mailbox(&local_mailbox, 1, NULL);
1762 mail_send_notify(EMAIL_SEND_FINISH, 0, 0, account_id, mail_ids[total], err);
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);
1769 EM_SAFE_FREE(mail_ids);
1771 if (err_code != NULL)
1777 static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code)
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);
1783 int err = EMAIL_ERROR_NONE;
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;
1790 if (!env || !env->from || (!env->to && !env->cc && !env->bcc)) {
1792 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1793 err = EMAIL_ERROR_INVALID_PARAM;
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;
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;
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);
1815 SNPRINTF(buf, sizeof(buf), "FROM:<%s>", ref_account->user_email_address);
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));
1822 EM_DEBUG_LOG("stream->protocol.esmtp.dsn.ok [%d]", stream->protocol.esmtp.dsn.ok);
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);
1831 EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is not required or DSN is not supported");
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);
1838 if (send_ret != SMTP_RESPONSE_OK) {
1843 send_ret = smtp_send(stream, "MAIL", buf);
1844 EM_DEBUG_LOG("[SMTP] MAIL %s --------> %s", buf, stream->reply);
1847 case SMTP_RESPONSE_OK:
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;
1856 case SMTP_RESPONSE_UNAVAIL:
1857 EM_DEBUG_EXCEPTION("SMTP error : sending unavailable...");
1858 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1860 case SMTP_RESPONSE_CONNECTION_BROKEN:
1861 EM_DEBUG_EXCEPTION("SMTP error : SMTP connection broken...");
1862 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1865 EM_DEBUG_EXCEPTION("SMTP error : sending unavailable...");
1866 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
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);
1874 err = stream->replycode;
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);
1886 err = stream->replycode;
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);
1898 err = stream->replycode;
1908 EM_DEBUG_EXCEPTION("One or more recipients failed...");
1909 err = EMAIL_ERROR_INVALID_ADDRESS;
1913 EM_DEBUG_EXCEPTION("No valid recipients...");
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;
1923 err = EMAIL_ERROR_INVALID_ADDRESS;
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);
1933 if (send_ret != SMTP_RESPONSE_READY) {
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;
1945 EM_PROFILE_END(profile_open_file);
1948 #ifdef __FEATURE_SEND_OPTMIZATION__
1951 int read_size, allocSize, dataSize, gMaxAllocSize = 40960; /* 40KB */
1953 fseek(fp, 0, SEEK_END);
1955 fseek(fp, 0, SEEK_SET);
1956 EM_DEBUG_LOG("total size [%d]", total);
1958 if (total < gMaxAllocSize)
1959 allocSize = total + 1;
1961 allocSize = gMaxAllocSize;
1963 EM_PROFILE_BEGIN(profile_allocation);
1964 /* Allocate a buffer of max 2MB to read from file */
1965 data = (char *)em_malloc(allocSize);
1967 EM_PROFILE_END(profile_allocation);
1970 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1975 if (total < allocSize)
1978 dataSize = allocSize;
1980 memset(data, 0x0, dataSize+1);
1981 read_size = fread(data, sizeof (char), dataSize, fp);
1983 if (read_size != dataSize) {
1986 EM_DEBUG_EXCEPTION("Read from file failed");
1987 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
1992 EM_DEBUG_LOG("before smtp_soutr_test");
1993 if (!(send_ret = smtp_soutr_test(stream->netstream, data))) {
1995 EM_DEBUG_EXCEPTION("Failed to send the data ");
1996 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
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;
2006 EM_DEBUG_LOG("Sent data Successfully. sent[%d] total[%d]", sent, total);
2014 fseek(fp, 0, SEEK_END);
2016 fseek(fp, 0, SEEK_SET);
2018 while (fgets(buf, 1024, fp)) {
2019 #ifdef FEATURE_SEND_DATA_DEBUG
2020 EM_DEBUG_LOG("%s", buf);
2022 sent += EM_SAFE_STRLEN(buf);
2024 if (!(send_ret = smtp_soutr(stream->netstream, buf)))
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 >>>>");
2033 last_sent_percent = sent_percent;
2039 EM_DEBUG_EXCEPTION("smtp_soutr failed - %ld", send_ret);
2040 err = EMAIL_ERROR_SMTP_SEND_FAILURE;
2045 send_ret = smtp_send(stream, ".", 0);
2046 EM_DEBUG_LOG("[SMTP] . --------> %s", stream->reply);
2048 if (send_ret != SMTP_RESPONSE_OK) {
2057 smtp_send(stream, "RSET", 0);
2060 emcore_free_account(ref_account);
2061 EM_SAFE_FREE(ref_account);
2069 EM_PROFILE_END(profile_emcore_send_mail_smtp);
2070 EM_DEBUG_FUNC_END("ret [%d]", ret);
2074 /* ------ rfc822 handle --------------------------------------------------- */
2075 #define RANDOM_NUMBER_LENGTH 35
2077 char *emcore_generate_content_id_string(const char *hostname, int *err)
2079 EM_DEBUG_FUNC_BEGIN("hostname[%p]", hostname);
2082 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2084 *err = EMAIL_ERROR_INVALID_PARAM;
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;
2091 cid_string = malloc(cid_length);
2095 *err = EMAIL_ERROR_OUT_OF_MEMORY;
2099 memset(cid_string, 0, cid_length);
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();
2107 SNPRINTF(cid_string, cid_length, "<%08x%08x%08x%08x@%s>", random_number_1, random_number_2, random_number_3, random_number_4, hostname);
2110 *err = EMAIL_ERROR_NONE;
2112 EM_DEBUG_FUNC_END("cid_string [%s]", cid_string);
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)
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);
2127 int error = EMAIL_ERROR_NONE;
2128 int has_special_character = 0;
2129 int base64_file_name_length = 0;
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;
2142 SIZEDTEXT source_text;
2143 GError *glib_error = NULL;
2144 CHARSET *result_charset = NULL;
2147 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2148 error = EMAIL_ERROR_INVALID_PARAM;
2152 if (body->nested.part) {
2153 last_part = body->nested.part;
2155 if (last_part != NULL) {
2156 while (last_part->next)
2157 last_part = last_part->next;
2162 part = mail_newbody_part();
2164 EM_DEBUG_EXCEPTION("mail_newbody_part failed...");
2165 error = EMAIL_ERROR_OUT_OF_MEMORY;
2172 last_part->next = part;
2174 body->nested.part = part;
2179 /* content_data = (unsigned char *)fs_get(data_len + 1); */
2180 /* memcpy(content_data, data, data_len); */
2181 /* content_data[data_len] = 0; */
2183 /* part->body.contents.text.data = content_data; */
2184 /* part->body.contents.text.size = data_len; */
2186 if (filename) { /* attachment */
2187 source_text.data = (unsigned char*)filename;
2188 source_text.size = EM_SAFE_STRLEN(filename);
2190 result_charset = (CHARSET*)utf8_infercharset(&source_text);
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);
2198 while(filename[i]) {
2199 if(filename[i++] & 0x80) {
2200 has_special_character = 1;
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);
2209 EM_DEBUG_LOG("encoded_file_name [%s]", encoded_file_name);
2211 if(encoded_file_name == NULL)
2212 encoded_file_name = strdup(filename);
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);
2219 result_file_name = em_replace_string(base64_file_name, "\015\012", "");
2221 EM_DEBUG_LOG("base64_file_name_length [%d]", base64_file_name_length);
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.");
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);
2234 extension = em_get_extension_from_file_path(filename, NULL);
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;
2246 part->body.subtype = strdup("octet-stream");
2248 part->body.encoding = ENCBINARY;
2249 part->body.size.bytes = data_len;
2252 part->body.sparep = EM_SAFE_STRDUP((char *)data); /* file path */
2254 part->body.sparep = NULL;
2256 SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "attachment");
2258 part->body.disposition.type = cpystr(content_disposition);
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;
2269 param->attribute = cpystr("name");
2270 param->value = cpystr(encoded_file_name);
2273 last_part->body.parameter = last_param;
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); */
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;
2291 param->attribute = cpystr("filename");
2292 param->value = cpystr(encoded_file_name);
2295 last_part->body.disposition.parameter = last_param;
2298 last_part->body.disposition.type = strdup("inline");
2301 /* text body (plain/html) */
2302 part->body.type = TYPETEXT;
2303 part->body.size.bytes = data_len;
2306 part->body.sparep = EM_SAFE_STRDUP((char *)data); /* file path */
2308 part->body.sparep = NULL;
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;
2317 if (last_param != NULL) {
2318 while (last_param->next)
2319 last_param = last_param->next;
2322 param = mail_newbody_parameter();
2324 if (param == NULL) {
2325 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2326 error = EMAIL_ERROR_OUT_OF_MEMORY;
2330 param->attribute = cpystr("CHARSET");
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);
2339 param->value = cpystr("UTF-8");
2342 param->value = cpystr("UTF-8");
2346 if (last_param != NULL)
2347 last_param->next = param;
2349 part->body.parameter = param;
2352 /* HTML text body */
2353 part->body.encoding = ENC8BIT;
2354 part->body.subtype = cpystr(content_sub_type);
2356 last_param = part->body.parameter;
2358 if (last_param != NULL) {
2359 while (last_param->next)
2360 last_param = last_param->next;
2363 param = mail_newbody_parameter();
2365 if (param == NULL) {
2366 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2367 error = EMAIL_ERROR_OUT_OF_MEMORY;
2371 param->attribute = cpystr("CHARSET");
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);
2384 param->value = cpystr("UTF-8");
2386 EM_SAFE_FREE(extract_charset);
2389 param->value = cpystr("UTF-8");
2392 if (last_param != NULL)
2393 last_param->next = param;
2395 part->body.parameter = param;
2398 /* NOTE : need to require this code. */
2399 /* sprintf(content_disposition, "%s\0", "inline"); */
2401 SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "inline");
2402 part->body.disposition.type = cpystr(content_disposition);
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)
2414 EM_DEBUG_FUNC_END();
2418 static PART *attach_mutipart_with_sub_type(BODY *parent_body, char *sub_type, int *err_code)
2420 EM_DEBUG_FUNC_BEGIN("parent_body[%p], sub_type [%s], err_code[%p]", parent_body, sub_type, err_code);
2422 int error = EMAIL_ERROR_NONE;
2424 PART *tail_part_cur = NULL;
2425 PART *new_part = NULL;
2427 if (!parent_body || !sub_type) {
2428 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2429 error = EMAIL_ERROR_INVALID_PARAM;
2433 if (parent_body->nested.part) {
2434 tail_part_cur = parent_body->nested.part;
2436 if (tail_part_cur != NULL) {
2437 while (tail_part_cur->next)
2438 tail_part_cur = tail_part_cur->next;
2442 new_part = mail_newbody_part();
2444 if (new_part == NULL) {
2445 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2446 error = EMAIL_ERROR_OUT_OF_MEMORY;
2451 new_part->next = NULL;
2452 new_part->body.type = TYPEMULTIPART;
2453 new_part->body.subtype = EM_SAFE_STRDUP(sub_type);
2456 tail_part_cur->next = new_part;
2458 parent_body->nested.part = new_part;
2465 EM_DEBUG_FUNC_END();
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)
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);
2476 int error = EMAIL_ERROR_NONE;
2477 BODY *frame_body = NULL;
2478 /* make multipart body(multipart frame_body..) .. that has not content.. */
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;
2486 frame_body = mail_newbody();
2487 if (frame_body == NULL) {
2488 EM_DEBUG_EXCEPTION("mail_newbody failed...");
2489 error = EMAIL_ERROR_OUT_OF_MEMORY;
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;
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);
2504 /* insert files.. */
2505 emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
2509 for(i = 0; i < input_attachment_tbl_count; i++) {
2510 temp_attachment_tbl = input_attachment_tbl + i;
2512 EM_DEBUG_LOG("insert files - attachment id[%d]", temp_attachment_tbl->attachment_id);
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);
2522 name = temp_attachment_tbl->attachment_name;
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);
2535 *multipart_body = frame_body;
2536 else if (frame_body != NULL)
2537 mail_free_body(&frame_body);
2539 if (err_code != NULL)
2541 EM_DEBUG_FUNC_END();
2545 static char *emcore_encode_rfc2047_text(char *utf8_text, int *err_code)
2547 EM_DEBUG_FUNC_BEGIN("utf8_text[%s], err_code[%p]", utf8_text, err_code);
2549 if (utf8_text == NULL) {
2550 if (err_code != NULL)
2551 *err_code = EMAIL_ERROR_INVALID_PARAM;
2555 gsize len = EM_SAFE_STRLEN(utf8_text);
2557 EM_DEBUG_FUNC_END();
2560 return g_strdup_printf("=?UTF-8?B?%s?=", g_base64_encode((const guchar *)utf8_text, len));
2565 static void emcore_encode_rfc2047_address(ADDRESS *address, int *err_code)
2567 EM_DEBUG_FUNC_BEGIN("address[%p], err_code[%p]", address, err_code);
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;
2575 address = address->next;
2577 EM_DEBUG_FUNC_END();
2580 #define DATE_STR_LENGTH 100
2582 static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope)
2584 EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], output_envelope[%p]", input_mail_tbl_data, output_envelope);
2587 int error = EMAIL_ERROR_NONE;
2588 int is_incomplete = 0;
2590 ENVELOPE *envelope = NULL;
2591 email_account_t *ref_account = NULL;
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 */
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;
2605 if (!(envelope = mail_newenvelope())) {
2606 EM_DEBUG_EXCEPTION("mail_newenvelope failed...");
2607 error = EMAIL_ERROR_OUT_OF_MEMORY;
2611 is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
2613 if (is_incomplete && (input_mail_tbl_data->account_id > 0)) {
2614 ref_account = emcore_get_account_reference(input_mail_tbl_data->account_id);
2616 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
2617 error = EMAIL_ERROR_INVALID_ACCOUNT;
2621 if (ref_account->user_email_address && ref_account->user_email_address[0] != '\0') {
2622 char *p = cpystr(ref_account->user_email_address);
2625 EM_DEBUG_EXCEPTION("cpystr failed...");
2626 error = EMAIL_ERROR_OUT_OF_MEMORY;
2630 EM_DEBUG_LOG("Assign envelope->from");
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 ;
2641 envelope->from = rfc822_parse_mailbox(&p, NULL);
2644 if (!envelope->from) {
2645 EM_DEBUG_EXCEPTION("rfc822_parse_mailbox failed...");
2646 error = EMAIL_ERROR_INVALID_ADDRESS;
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);
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;
2661 if (ref_account->return_address && ref_account->return_address[0] != '\0') {
2662 char *p = cpystr(ref_account->return_address);
2665 EM_DEBUG_EXCEPTION("cpystr failed...");
2666 error = EMAIL_ERROR_OUT_OF_MEMORY;
2669 envelope->return_path = rfc822_parse_mailbox(&p, NULL);
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;
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] = ',';
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] = ',';
2695 em_skip_whitespace(input_mail_tbl_data->full_address_from , &pAdd);
2696 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2698 rfc822_parse_adrlist(&envelope->from, pAdd, NULL);
2702 em_skip_whitespace(input_mail_tbl_data->full_address_return , &pAdd);
2703 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2705 rfc822_parse_adrlist(&envelope->return_path, pAdd, NULL);
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] = ',';
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] = ',';
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] = ',';
2735 em_skip_whitespace(input_mail_tbl_data->full_address_to , &pAdd);
2736 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
2738 rfc822_parse_adrlist(&envelope->to, pAdd, NULL);
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);
2746 rfc822_parse_adrlist(&envelope->cc, pAdd, NULL);
2750 em_skip_whitespace(input_mail_tbl_data->full_address_bcc , &pAdd);
2751 rfc822_parse_adrlist(&envelope->bcc, pAdd, NULL);
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);
2763 if (input_mail_tbl_data->subject)
2764 envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
2766 char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
2767 rfc822_date(rfc822_date_string);
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);
2777 envelope->date = (unsigned char *)cpystr((const char *)rfc822_date_string);
2785 *output_envelope = envelope;
2787 mail_free_envelope(&envelope);
2788 *output_envelope = NULL;
2792 emcore_free_account(ref_account);
2793 EM_SAFE_FREE(ref_account);
2796 EM_DEBUG_FUNC_END("error [%d]", error);
2799 /* Description : Make RFC822 text file from mail_tbl data */
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)
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);
2809 int error = EMAIL_ERROR_NONE;
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;
2820 if (!input_mail_tbl_data) {
2821 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2822 error = EMAIL_ERROR_INVALID_PARAM;
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;
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);
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);
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);
2845 root_body = mail_newbody();
2847 if (root_body == NULL) {
2848 EM_DEBUG_EXCEPTION("mail_newbody failed...");
2849 error = EMAIL_ERROR_OUT_OF_MEMORY;
2853 param = mail_newbody_parameter();
2855 if (param == NULL) {
2856 EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
2857 error = EMAIL_ERROR_OUT_OF_MEMORY;
2861 if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE) {
2863 root_body->type = TYPEMULTIPART;
2864 root_body->subtype = strdup("MIXED");
2866 mail_free_body_parameter(¶m);
2869 } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED) {
2870 PARAMETER *protocol_param = mail_newbody_parameter();
2872 root_body->type = TYPEMULTIPART;
2873 root_body->subtype = strdup("SIGNED");
2875 param->attribute = cpystr("micalg");
2876 switch (input_mail_tbl_data->digest_type) {
2877 case DIGEST_TYPE_SHA1:
2878 param->value = cpystr("sha1");
2880 case DIGEST_TYPE_MD5:
2881 param->value = cpystr("md5");
2884 EM_DEBUG_EXCEPTION("Invalid digest type");
2888 protocol_param->attribute = cpystr("protocol");
2889 protocol_param->value = cpystr("application/pkcs7-signature");
2890 protocol_param->next = NULL;
2891 param->next = protocol_param;
2893 input_mail_tbl_data->file_path_plain = NULL;
2894 input_mail_tbl_data->file_path_html = NULL;
2896 input_attachment_tbl = input_attachment_tbl + (input_attachment_count - 1);
2898 input_attachment_count = 1;
2902 root_body->type = TYPEAPPLICATION;
2903 root_body->subtype = strdup("PKCS7-MIME");
2905 param->attribute = cpystr("name");
2906 param->value = cpystr("smime.p7m");
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;
2913 input_attachment_count = 1;
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;
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);
2924 if (!part_for_text) {
2925 EM_DEBUG_EXCEPTION("attach_mutipart_with_sub_type [part_for_text] failed [%d]", error);
2929 text_body = &part_for_text->body;
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);
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);
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);
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);
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);
2960 if (input_attachment_tbl && input_attachment_count) {
2961 emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
2963 BODY *body_to_attach = NULL;
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);
2977 name = temp_attachment_tbl->attachment_name;
2979 EM_DEBUG_LOG("name[%s]", name);
2981 if (temp_attachment_tbl->attachment_inline_content_status && part_for_html)
2982 body_to_attach = &(part_for_html->body);
2984 body_to_attach = root_body;
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);
2996 text_body = mail_newbody();
2998 if (text_body == NULL) {
2999 EM_DEBUG_EXCEPTION("mail_newbody failed...");
3001 error = EMAIL_ERROR_OUT_OF_MEMORY;
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);
3010 text_body->sparep = NULL;
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);
3017 text_body->size.bytes = 0;
3021 if (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) {
3023 EM_DEBUG_LOG("REPORT MAIL");
3024 envelope->references = cpystr(input_mail_tbl_data->message_id);
3028 EM_DEBUG_LOG("write rfc822 : file_path[%p]", file_path);
3031 html_body = &(part_for_html->body);
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);
3044 if ((ret == true) && (env != NULL))
3046 else if (envelope != NULL)
3047 mail_free_envelope(&envelope);
3049 if (text_body != NULL)
3050 mail_free_body(&text_body);
3052 if (root_body != NULL)
3053 mail_free_body(&root_body);
3055 if (err_code != NULL)
3058 EM_DEBUG_FUNC_END("ret [%d]", ret);
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)
3064 EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], file_path[%p], err_code[%p]", input_mail_tbl_data, file_path, err_code);
3067 int error = EMAIL_ERROR_NONE;
3068 int is_incomplete = 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];
3080 emstorage_account_tbl_t *ref_account = NULL;
3082 if (!input_mail_tbl_data) {
3083 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3084 error = EMAIL_ERROR_INVALID_PARAM;
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;
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);
3100 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
3101 error = EMAIL_ERROR_INVALID_ACCOUNT;
3105 if (!(envelope = mail_newenvelope())) {
3106 EM_DEBUG_EXCEPTION("mail_newenvelope failed...");
3107 error = EMAIL_ERROR_OUT_OF_MEMORY;
3111 is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
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);
3118 EM_DEBUG_EXCEPTION("cpystr failed...");
3119 error = EMAIL_ERROR_OUT_OF_MEMORY;
3123 EM_DEBUG_LOG("Assign envelop->from");
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 ;
3134 envelope->from = rfc822_parse_mailbox(&p, NULL);
3137 if (!envelope->from) {
3138 EM_DEBUG_EXCEPTION("rfc822_parse_mailbox failed...");
3139 error = EMAIL_ERROR_INVALID_ADDRESS;
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;
3153 if (ref_account->return_address && ref_account->return_address[0] != '\0') {
3154 char *p = cpystr(ref_account->return_address);
3157 EM_DEBUG_EXCEPTION("cpystr failed...");
3159 error = EMAIL_ERROR_OUT_OF_MEMORY;
3162 envelope->return_path = rfc822_parse_mailbox(&p, NULL);
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;
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] = ',';
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] = ',';
3187 em_skip_whitespace(input_mail_tbl_data->full_address_from , &pAdd);
3188 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3190 rfc822_parse_adrlist(&envelope->from, pAdd, ref_account->outgoing_server_address);
3194 em_skip_whitespace(input_mail_tbl_data->full_address_return , &pAdd);
3195 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3197 rfc822_parse_adrlist(&envelope->return_path, pAdd, ref_account->outgoing_server_address);
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] = ',';
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] = ',';
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] = ',';
3227 em_skip_whitespace(input_mail_tbl_data->full_address_to , &pAdd);
3228 EM_DEBUG_LOG("address[pAdd][%s]", pAdd);
3230 rfc822_parse_adrlist(&envelope->to, pAdd, ref_account->outgoing_server_address);
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);
3238 rfc822_parse_adrlist(&envelope->cc, pAdd, ref_account->outgoing_server_address);
3242 em_skip_whitespace(input_mail_tbl_data->full_address_bcc , &pAdd);
3243 rfc822_parse_adrlist(&envelope->bcc, pAdd, ref_account->outgoing_server_address);
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);
3255 if (input_mail_tbl_data->subject)
3256 envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
3258 char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
3259 char localtime_string[DATE_STR_LENGTH] = {0, };
3261 rfc822_date(rfc822_date_string);
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);
3270 envelope->date = (unsigned char *)cpystr((const char *)rfc822_date_string);
3272 /* check report input_mail_tbl_data */
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);
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");
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);
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");
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);
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();
3303 if (root_body == NULL) {
3304 EM_DEBUG_EXCEPTION("mail_newbody failed...");
3305 error = EMAIL_ERROR_OUT_OF_MEMORY;
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;
3315 part_for_text = attach_mutipart_with_sub_type(root_body, "ALTERNATIVE", &error);
3317 if (!part_for_text) {
3318 EM_DEBUG_EXCEPTION("attach_mutipart_with_sub_type [part_for_text] failed [%d]", error);
3322 text_body = &part_for_text->body;
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);
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);
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);
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);
3347 if (input_attachment_tbl && input_attachment_count) {
3348 email_attachment_data_t *temp_attachment_tbl = NULL;
3350 BODY *body_to_attach = NULL;
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);
3364 name = temp_attachment_tbl->attachment_name;
3365 EM_DEBUG_LOG("name[%s]", name);
3367 if (temp_attachment_tbl->inline_content_status && part_for_html)
3368 body_to_attach = &(part_for_html->body);
3370 body_to_attach = root_body;
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);
3381 text_body = mail_newbody();
3383 if (text_body == NULL) {
3384 EM_DEBUG_EXCEPTION("mail_newbody failed...");
3386 error = EMAIL_ERROR_OUT_OF_MEMORY;
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);
3395 text_body->sparep = NULL;
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);
3402 text_body->size.bytes = 0;
3406 EM_DEBUG_LOG("write rfc822 : file_path[%s]", file_path);
3409 html_body = &(part_for_html->body);
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);
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);
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);
3437 emstorage_free_account(&ref_account, 1, NULL);
3439 if (envelope != NULL)
3440 mail_free_envelope(&envelope);
3442 if (text_body != NULL)
3443 mail_free_body(&text_body);
3445 if (root_body != NULL)
3446 mail_free_body(&root_body);
3448 if (err_code != NULL)
3451 EM_DEBUG_FUNC_END("ret [%d]", ret);
3455 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
3456 static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body, int *err_code)
3458 EM_DEBUG_FUNC_BEGIN("envelope[%p], mulitpart_body[%p], err_code[%p]", envelope, multipart_body, err_code);
3461 int err = EMAIL_ERROR_NONE;
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;
3470 char buf[512] = {0x00, };
3473 if (!envelope || !multipart_body) {
3474 EM_DEBUG_EXCEPTION(" envelope[%p], mulitpart_body[%p]", envelope, multipart_body);
3475 err = EMAIL_ERROR_INVALID_PARAM;
3479 if (!(text_body = mail_newbody())) {
3480 EM_DEBUG_EXCEPTION(" mail_newbody failed...");
3481 err = EMAIL_ERROR_OUT_OF_MEMORY;
3485 if (!emcore_get_temp_file_name(&fname, &err)) {
3486 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed [%d]", err);
3490 if (!(fp = fopen(fname, "wb+"))) {
3491 EM_DEBUG_EXCEPTION(" fopen failed - %s", fname);
3492 err = EMAIL_ERROR_SYSTEM_FAILURE;
3496 if (!envelope->from || !envelope->from->mailbox || !envelope->from->host) {
3497 if (!envelope->from)
3498 EM_DEBUG_EXCEPTION(" envelope->from[%p]", envelope->from);
3500 EM_DEBUG_LOG(" envelope->from->mailbox[%p], envelope->from->host[%p]", envelope->from->mailbox, envelope->from->host);
3502 err = EMAIL_ERROR_INVALID_PARAM;
3507 if (envelope->from->personal)
3508 SNPRINTF(buf, sizeof(buf), "%s <%s@%s>", envelope->from->personal, envelope->from->mailbox, envelope->from->host);
3511 SNPRINTF(buf, sizeof(buf), "%s@%s", envelope->from->mailbox, envelope->from->host);
3513 fprintf(fp, "Your message has been read by %s"CRLF_STRING, buf);
3514 fprintf(fp, "Date : %s", envelope->date);
3516 fclose(fp); fp = NULL;
3518 if (!emcore_get_file_size(fname, &sz, &err)) {
3519 EM_DEBUG_EXCEPTION(" emcore_get_file_size failed [%d]", err);
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;
3528 if (!emcore_get_temp_file_name(&fname, &err)) {
3529 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed [%d]", err);
3533 if (!(fp = fopen(fname, "wb+"))) {
3534 EM_DEBUG_EXCEPTION(" fopen failed - %s", fname);
3535 err = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_UNKNOWN; */
3539 if (!envelope->references) {
3540 EM_DEBUG_EXCEPTION(" envelope->references[%p]", envelope->references);
3541 err = EMAIL_ERROR_INVALID_PARAM;
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");
3549 fclose(fp); fp = NULL;
3551 memset(&temp_attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3553 temp_attachment_tbl.attachment_path = EM_SAFE_STRDUP(fname);
3555 if (!emcore_get_file_size(fname, &temp_attachment_tbl.attachment_size, &err)) {
3556 EM_DEBUG_EXCEPTION(" emcore_get_file_size failed [%d]", err);
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);
3565 text_body->contents.text.data = NULL;
3567 /* change mail header */
3569 /* set content-type to multipart/report */
3570 m_body->subtype = strdup("report");
3572 /* set report-type parameter in content-type */
3573 param = em_malloc(sizeof(PARAMETER));
3575 EM_DEBUG_EXCEPTION(" malloc failed...");
3576 err = EMAIL_ERROR_OUT_OF_MEMORY;
3580 param->attribute = strdup("report-type");
3581 param->value = strdup("disposition-notification");
3582 param->next = m_body->parameter;
3584 m_body->parameter = param;
3586 /* change body-header */
3588 p_body = &m_body->nested.part->next->body;
3590 /* set content-type to message/disposition-notification */
3591 p_body->type = TYPEMESSAGE;
3592 p_body->encoding = ENC7BIT;
3594 EM_SAFE_FREE(p_body->subtype);
3596 p_body->subtype = strdup("disposition-notification");
3599 mail_free_body_parameter(&p_body->parameter);
3600 mail_free_body_parameter(&p_body->disposition.parameter);
3602 EM_SAFE_FREE(p_body->disposition.type);
3604 p_body->disposition.type = strdup("inline");
3609 if ((ret == true) && (multipart_body != NULL))
3610 *multipart_body = m_body;
3611 else if (m_body != NULL)
3612 mail_free_body(&m_body);
3614 if (text_body != NULL)
3615 mail_free_body(&text_body);
3620 EM_SAFE_FREE(fname);
3622 if (err_code != NULL)
3625 EM_DEBUG_FUNC_END("err [%d]", err);
3630 INTERNAL_FUNC int emcore_get_body_buff(char *file_path, char **buff)
3632 EM_DEBUG_FUNC_BEGIN();
3637 char *read_buff = NULL;
3641 r_fp = fopen(file_path, "r");
3644 EM_DEBUG_EXCEPTION(" Filename %s failed to open", file_path);
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';
3657 EM_DEBUG_EXCEPTION("file read failed - %s", file_path);
3658 EM_SAFE_FREE(read_buff);
3671 static int emcore_copy_attachment_from_original_mail(int input_original_mail_id, int input_target_mail_id)
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;
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;
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);
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);
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;
3700 /* If attachment is inline content, fild path should not include attachment id */
3701 if(target_attach->attachment_inline_content_status == 1)
3704 attachment_id = target_attach->attachment_id;
3706 EM_DEBUG_LOG("attachment_inline_content_status [%d] attachment_id[%d]", target_attach->attachment_inline_content_status, attachment_id);
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);
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;
3717 if(!emstorage_update_attachment(target_attach, false, &err)) {
3718 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3721 memset(output_file_path, 0, MAX_PATH);
3722 memset(output_file_name, 0, MAX_PATH);
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);
3735 EM_DEBUG_FUNC_END("err[%d]", err);
3740 INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id)
3742 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d]", input_mail_id);
3743 int err = EMAIL_ERROR_NONE;
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;
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);
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");
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);
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);
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);
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);
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);
3797 if(attachment_array)
3798 emcore_free_attachment_data(&attachment_array, attachment_count, NULL);
3800 if(mail_to_be_sent) {
3801 emcore_free_mail_data(mail_to_be_sent);
3802 EM_SAFE_FREE(mail_to_be_sent);
3806 emcore_free_mail_data(original_mail);
3807 EM_SAFE_FREE(original_mail);
3811 EM_DEBUG_FUNC_END("err [%d]", err);