23 #include <libetpan/libetpan.h> 24 #include "mrmailbox_internal.h" 33 #define LOCK_SMTP pthread_mutex_lock(&ths->m_mutex); smtp_locked = 1; 34 #define UNLOCK_SMTP if( smtp_locked ) { pthread_mutex_unlock(&ths->m_mutex); smtp_locked = 0; } 45 if( (ths=calloc(1,
sizeof(mrsmtp_t)))==NULL ) {
49 ths->m_log_connect_errors = 1;
51 ths->m_mailbox = mailbox;
52 pthread_mutex_init(&ths->m_mutex, NULL);
57 void mrsmtp_unref(mrsmtp_t* ths)
62 mrsmtp_disconnect(ths);
63 pthread_mutex_destroy(&ths->m_mutex);
74 int mrsmtp_is_connected(
const mrsmtp_t* ths)
76 return (ths && ths->m_hEtpan)? 1 : 0;
80 static void body_progress(
size_t current,
size_t maximum,
void* user_data)
83 printf(
"body_progress called with current=%i, maximum=%i.", (
int)current, (
int)maximum);
89 static void logger(mailsmtp* smtp,
int log_type,
const char* buffer__,
size_t size,
void* user_data)
91 char* buffer = malloc(size+1);
92 memcpy(buffer, buffer__, size);
94 printf(
"SMPT: %i: %s", log_type, buffer__);
99 int mrsmtp_connect(mrsmtp_t* ths,
const mrloginparam_t* lp)
101 int success = 0, smtp_locked = 0;
104 if( ths == NULL || lp == NULL ) {
110 if( ths->m_mailbox->m_cb(ths->m_mailbox, MR_EVENT_IS_ONLINE, 0, 0)!=1 ) {
111 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, MR_ERR_NONETWORK, NULL);
115 if( ths->m_hEtpan ) {
116 mrmailbox_log_warning(ths->m_mailbox, 0,
"Already connected to SMTP server.");
121 if( lp->m_addr == NULL || lp->m_send_server == NULL || lp->m_send_port == 0 ) {
122 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"Cannot connect to SMTP; bad parameters.");
127 ths->m_from = safe_strdup(lp->m_addr);
129 ths->m_hEtpan = mailsmtp_new(0, NULL);
130 if( ths->m_hEtpan == NULL ) {
131 mrmailbox_log_error(ths->m_mailbox, 0,
"SMTP-object creationed failed.");
134 mailsmtp_set_progress_callback(ths->m_hEtpan, body_progress, ths);
136 mailsmtp_set_logger(ths->m_hEtpan, logger, ths);
140 if( lp->m_server_flags&(MR_SMTP_SOCKET_STARTTLS|MR_SMTP_SOCKET_PLAIN) )
142 mrmailbox_log_info(ths->m_mailbox, 0,
"Connecting to SMTP-server \"%s:%i\"...", lp->m_send_server, (
int)lp->m_send_port);
143 if( (r=mailsmtp_socket_connect(ths->m_hEtpan, lp->m_send_server, lp->m_send_port)) != MAILSMTP_NO_ERROR ) {
144 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"SMTP-STARTTLS connection to %s:%i failed (%s)", lp->m_send_server, (
int)lp->m_send_port, mailsmtp_strerror(r));
150 mrmailbox_log_info(ths->m_mailbox, 0,
"Connecting to SMTP-server \"%s:%i\" via SSL...", lp->m_send_server, (
int)lp->m_send_port);
151 if( (r=mailsmtp_ssl_connect(ths->m_hEtpan, lp->m_send_server, lp->m_send_port)) != MAILSMTP_NO_ERROR ) {
152 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"SMPT-SSL connection to %s:%i failed (%s)", lp->m_send_server, (
int)lp->m_send_port, mailsmtp_strerror(r));
159 if( try_esmtp && (r=mailesmtp_ehlo(ths->m_hEtpan))==MAILSMTP_NO_ERROR ) {
162 else if( !try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED ) {
163 r = mailsmtp_helo(ths->m_hEtpan);
166 if( r != MAILSMTP_NO_ERROR ) {
167 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"SMTP-helo failed (%s)", mailsmtp_strerror(r));
171 if( lp->m_server_flags&MR_SMTP_SOCKET_STARTTLS )
173 mrmailbox_log_info(ths->m_mailbox, 0,
"Switching to SMTP-STARTTLS.");
174 if( (r=mailsmtp_socket_starttls(ths->m_hEtpan)) != MAILSMTP_NO_ERROR ) {
175 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"SMTP-STARTTLS failed (%s)", mailsmtp_strerror(r));
180 if( try_esmtp && (r=mailesmtp_ehlo(ths->m_hEtpan))==MAILSMTP_NO_ERROR ) {
183 else if( !try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED ) {
184 r = mailsmtp_helo(ths->m_hEtpan);
187 if (r != MAILSMTP_NO_ERROR) {
188 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"SMTP-helo failed (%s)", mailsmtp_strerror(r));
192 mrmailbox_log_info(ths->m_mailbox, 0,
"Connection to SMTP-server ok.");
194 if( lp->m_send_user )
196 mrmailbox_log_info(ths->m_mailbox, 0,
"Login to SMTP-server as \"%s\"...", lp->m_send_user);
198 if((r=mailsmtp_auth(ths->m_hEtpan, lp->m_send_user, lp->m_send_pw))!=MAILSMTP_NO_ERROR ) {
199 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"SMTP-login failed for user %s (%s)", lp->m_send_user, mailsmtp_strerror(r));
203 mrmailbox_log_info(ths->m_mailbox, 0,
"SMTP-Login ok.");
210 if( ths->m_hEtpan ) {
211 mailsmtp_free(ths->m_hEtpan);
212 ths->m_hEtpan = NULL;
222 void mrsmtp_disconnect(mrsmtp_t* ths)
232 if( ths->m_hEtpan ) {
234 mailsmtp_free(ths->m_hEtpan);
235 ths->m_hEtpan = NULL;
247 int mrsmtp_send_msg(mrsmtp_t* ths,
const clist* recipients,
const char* data_not_terminated,
size_t data_bytes)
249 int success = 0, r, smtp_locked = 0;
256 if( recipients == NULL || clist_count(recipients)==0 || data_not_terminated == NULL || data_bytes == 0 ) {
262 if( ths->m_hEtpan==NULL ) {
267 if( (r=(ths->m_esmtp?
268 mailesmtp_mail(ths->m_hEtpan, ths->m_from, 1,
"etPanSMTPTest") :
269 mailsmtp_mail(ths->m_hEtpan, ths->m_from))) != MAILSMTP_NO_ERROR )
273 mrmailbox_log_error_if(&ths->m_log_usual_error, ths->m_mailbox, 0,
"mailsmtp_mail: %s, %s (%i)", ths->m_from, mailsmtp_strerror(r), (
int)r);
274 ths->m_log_usual_error = 1;
278 ths->m_log_usual_error = 0;
281 for( iter=clist_begin(recipients); iter!=NULL; iter=clist_next(iter)) {
282 const char* rcpt = clist_content(iter);
283 if( (r = (ths->m_esmtp?
284 mailesmtp_rcpt(ths->m_hEtpan, rcpt, MAILSMTP_DSN_NOTIFY_FAILURE|MAILSMTP_DSN_NOTIFY_DELAY, NULL) :
285 mailsmtp_rcpt(ths->m_hEtpan, rcpt))) != MAILSMTP_NO_ERROR) {
286 mrmailbox_log_error_if(&ths->m_log_connect_errors, ths->m_mailbox, 0,
"mailsmtp_rcpt: %s: %s", rcpt, mailsmtp_strerror(r));
292 if ((r = mailsmtp_data(ths->m_hEtpan)) != MAILSMTP_NO_ERROR) {
293 fprintf(stderr,
"mailsmtp_data: %s\n", mailsmtp_strerror(r));
297 if ((r = mailsmtp_data_message(ths->m_hEtpan, data_not_terminated, data_bytes)) != MAILSMTP_NO_ERROR) {
298 fprintf(stderr,
"mailsmtp_data_message: %s\n", mailsmtp_strerror(r));
An object representing a single mailbox.