MailPad開発記 DAY 5 - 送信用MHファイルの作成

ここで一度、メール送信の流れを整理してみると、
1.アカウント作成
2.送信用MHファイル作成
3.(POP3で認証)
4.(SMTP認証+)送信
となります。
2.は~/lisylph/examples/libsylph-compose.c
3.は~/lisylph/examples/libsylph-pop.c
4.は~/lisylph/examples/libsylph-send.c
それぞれ対応しています。


1.は昨日作ったので、今日は2.を作ってみましょう。
libsylph-compose.cに記述してある、do_compose関数をほとんどそのまま使えそうです。
そのままだとアカウントを設定ファイルから読み込むようになっているので、
そこだけ少し変えて、引数で渡すようにします。
#include を追加


callbacks.c

void
do_compose(const PrefsAccount *ac, const gchar *to, const gchar *subject, const gchar *body)
{
ComposeInfo *compose;

compose = compose_info_new(ac, COMPOSE_NEW);

compose_set_headers(compose, to, NULL, NULL, NULL, subject);
compose_set_body(compose, body);
compose_set_encoding(compose, CS_UTF_8, NULL);

compose_write_to_file(compose, "mail.txt", NULL);

compose_info_free(compose);
}


callbacks.c

void
on_button_clicked (GtkButton *button, gpointer user_data)
{
syl_init();
set_debug_mode(TRUE);

GtkEntry *entry_to = (GtkEntry*)lookup_widget((GtkWidget*)button, (gchar *)"entry_to");
GtkEntry *entry_subject = (GtkEntry*)lookup_widget((GtkWidget*)button, (gchar *)"entry_subject");
GtkTextView *textview = (GtkTextView*)lookup_widget((GtkWidget*)button, (gchar *)"textview");

gchar *to = (gchar*)gtk_entry_get_text(entry_to);
gchar *subject = (gchar*)gtk_entry_get_text(entry_subject);

GtkTextBuffer *textbuffer = gtk_text_view_get_buffer(textview);
GtkTextIter start, end;
gtk_text_buffer_get_start_iter(textbuffer,&start);
gtk_text_buffer_get_end_iter(textbuffer,&end);
gchar *body = gtk_text_buffer_get_text(textbuffer,&start,&end,TRUE);

PrefsAccount *ac = set_account((GtkWidget *)button);
do_compose(ac, to, subject, body);

syl_cleanup();
}

宛先や件名、本文をentryから取得する一連の流れをon_button_clickedに直接書いていますが、
widgetのポインタを受け取って宛先や件名、本文を格納した構造体なんかを返す関数を作った方がいいでしょう。


UMPCを起動して「SEND」ボタンを押すと、mail.txtが生成されているはずです。