bool mime_builder::save_as(acl::ofstream& fp) { acl::string buf; if (attachs_.empty()) { //header_.set_type(MIME_CTYPE_TEXT, body_html_ ? // MIME_STYPE_HTML : MIME_STYPE_PLAIN); header_.set_type("text", body_html_ ? "html" : "plain"); header_.add_header("Content-Transfer-Encoding", "base64"); } else { //header_.set_type(MIME_CTYPE_MULTIPART, // MIME_STYPE_MIXED); header_.set_type("multipart", "mixed"); delimeter_.format("------=_Part_%d_%ld.%ld", getpid(), acl_pthread_self(), time(NULL)); header_.set_boundary(delimeter_.c_str()); } header_.build_head(buf, false); if (fp.write(buf) == -1) { logger_error("write head to file %s error %s", fp.file_path(), acl::last_serror()); return false; } if (!delimeter_.empty()) { if (add_boundary(fp) == false) return false; } if (add_body(fp) == false) return false; if (delimeter_.empty()) return true; std::vector<char*>::const_iterator cit = attachs_.begin(); for (; cit != attachs_.end(); ++cit) { if (add_boundary(fp) == false) return false; if (add_attach(fp, *cit) == false) return false; } // 添加最后一个分隔符 return add_boundary(fp, true); }
gint main (gint argc, gchar **argv) { ECalClient *cal_client; GError *error = NULL; icalcomponent *icalcomp; struct icaltimetype now; gchar *uid = NULL; main_initialize (); cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL); g_return_val_if_fail (cal_client != NULL, FALSE); if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) { report_error ("client open sync", &error); g_object_unref (cal_client); return 1; } now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ()); icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT); icalcomponent_set_summary (icalcomp, "Test event summary"); icalcomponent_set_dtstart (icalcomp, now); icalcomponent_set_dtend (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0)); add_attach (icalcomp, ATTACH1); add_attach (icalcomp, ATTACH2); add_attach (icalcomp, ATTACH3); if (!e_cal_client_create_object_sync (cal_client, icalcomp, &uid, NULL, &error)) { report_error ("create object sync", &error); icalcomponent_free (icalcomp); g_object_unref (cal_client); return 1; } icalcomponent_free (icalcomp); g_object_set_data_full (G_OBJECT (cal_client), "use-uid", uid, g_free); /* synchronously without main-loop */ if (!test_sync (cal_client)) { g_object_unref (cal_client); return 1; } start_in_thread_with_main_loop (test_sync_in_thread, cal_client); if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) { report_error ("client remove sync", &error); g_object_unref (cal_client); return 1; } g_object_unref (cal_client); if (get_main_loop_stop_result () == 0) g_print ("Test finished successfully.\n"); return get_main_loop_stop_result (); }
int batch_mail(int conn, char *command) { char instance[63] = ""; int business = 0; row_t *rows = NULL; row_t *row = NULL; row_t *rr = NULL; int rowc; int count = 0; int flags = 0; char *sql; char *email = NULL; char *file; char *filename; char *tmp = NULL; smtp_recipient_t *r = NULL; smtp_header_t *h = NULL; smtp_attach_t *a = NULL; if (sscanf(command, "MAIL %[^.].%i\n", instance, &business) != 2) { chat(conn, "ERROR: Invalid syntax\n"); return 0; } chat(conn, "Sending email batch for instance '%s', business '%i' ... ", instance, business); db_connect(config->dbs); /* verify instance and business exist */ asprintf(&sql, "SELECT * FROM instance WHERE id='%s';", instance); rowc = batch_fetch_rows(NULL, 0, sql, &rows); free(sql); if (rowc == 0) { chat(conn, "ERROR: instance '%s' does not exist\n", instance); db_disconnect(config->dbs); return 0; } rows = NULL; asprintf(&sql, "SELECT * FROM business WHERE id='%i';", business); rowc = batch_fetch_rows(instance, 0, sql, &rows); free(sql); if (rowc == 0) { chat(conn, "ERROR: business '%s.%i' does not exist\n", instance, business); db_disconnect(config->dbs); return 0; } rows = NULL; chat(conn, CLERK_RESP_OK); /* lock emaildetail table */ batch_exec_sql(instance, business, "BEGIN WORK; LOCK TABLE emaildetail IN EXCLUSIVE MODE"); /* fetch emails to send */ rowc = batch_fetch_rows(instance, business, "SELECT * FROM email_unsent", &rows); row = rows; while (row != NULL) { /* get id of email */ email = db_field(row, "email")->fvalue; if (email == NULL) continue; /* loop through recipients */ asprintf(&sql, "SELECT * FROM emailrecipient WHERE email=%s", email); rowc = batch_fetch_rows(instance, business, sql, &rr); free(sql); if (rowc == 0) { /* skip email with no recipients */ syslog(LOG_DEBUG, "Skipping email with no recipients"); row = row->next; continue; } flags = 0; while (rr != NULL) { if (strcmp(db_field(rr, "is_to")->fvalue, "t") == 0) flags += EMAIL_TO; if (strcmp(db_field(rr, "is_cc")->fvalue, "t") == 0) flags += EMAIL_CC; add_recipient(&r, "", db_field(rr, "emailaddress")->fvalue, flags); rr = rr->next; } /* loop through headers */ asprintf(&sql, "SELECT * FROM emailheader WHERE email=%s", email); rowc = batch_fetch_rows(instance, business, sql, &rr); free(sql); while (rr != NULL) { add_header(&h, db_field(rr, "header")->fvalue, db_field(rr, "value")->fvalue); rr = rr->next; } /* loop through attachments */ asprintf(&sql, "SELECT * FROM emailpart WHERE email=%s", email); rowc = batch_fetch_rows(instance, business, sql, &rr); free(sql); while (rr != NULL) { file = db_field(rr, "file")->fvalue; tmp = strdup(file); filename = basename(tmp); add_attach(&a, file, filename); free(tmp); rr = rr->next; } /* send email */ /* FIXME: this will quietly crash if db_field returns NULL */ if (send_email( db_field(row, "sendername")->fvalue, db_field(row, "sendermail")->fvalue, db_field(row, "body")->fvalue, r, h, a) == 0) { /* update email with sent time */ asprintf(&sql, "SELECT email_sent(%s);", email); chat(conn, "sql: %s\n", sql); batch_exec_sql(instance, business, sql); free(sql); count++; } free_recipient(r); r = NULL; free_header(h); h = NULL; free_attach(a); a = NULL; row = row->next; } /* commit changes and unlock emaildetail table */ batch_exec_sql(instance, business, "COMMIT WORK;"); db_disconnect(config->dbs); chat(conn, "%i/%i emails sent\n", count, rowc); return 0; }