text
stringlengths
0
357
/* connect to SMTP server */
if (lp->server_flags&(DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_PLAIN))
{
if ((r=mailsmtp_socket_connect(smtp->etpan, lp->send_server, lp->send_port)) != MAILSMTP_NO_ERROR) {
dc_log_event_seq(smtp->context, DC_EVENT_ERROR_NETWORK, &smtp->log_connect_errors,
"SMTP-Socket connection to %s:%i failed (%s)",
lp->send_server, (int)lp->send_port, mailsmtp_strerror(r));
goto cleanup;
}
}
else
{
if ((r=mailsmtp_ssl_connect(smtp->etpan, lp->send_server, lp->send_port)) != MAILSMTP_NO_ERROR) {
dc_log_event_seq(smtp->context, DC_EVENT_ERROR_NETWORK, &smtp->log_connect_errors,
"SMTP-SSL connection to %s:%i failed (%s)",
lp->send_server, (int)lp->send_port, mailsmtp_strerror(r));
goto cleanup;
}
}
try_esmtp = 1;
smtp->esmtp = 0;
if (try_esmtp && (r=mailesmtp_ehlo(smtp->etpan))==MAILSMTP_NO_ERROR) {
smtp->esmtp = 1;
}
else if (!try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED) {
r = mailsmtp_helo(smtp->etpan);
}
if (r != MAILSMTP_NO_ERROR) {
dc_log_event_seq(smtp->context, DC_EVENT_ERROR_NETWORK, &smtp->log_connect_errors,
"SMTP-helo failed (%s)", mailsmtp_strerror(r));
goto cleanup;
}
if (lp->server_flags&DC_LP_SMTP_SOCKET_STARTTLS)
{
if ((r=mailsmtp_socket_starttls(smtp->etpan)) != MAILSMTP_NO_ERROR) {
dc_log_event_seq(smtp->context, DC_EVENT_ERROR_NETWORK, &smtp->log_connect_errors,
"SMTP-STARTTLS failed (%s)", mailsmtp_strerror(r));
goto cleanup;
}
smtp->esmtp = 0;
if (try_esmtp && (r=mailesmtp_ehlo(smtp->etpan))==MAILSMTP_NO_ERROR) {
smtp->esmtp = 1;
}
else if (!try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED) {
r = mailsmtp_helo(smtp->etpan);
}
if (r != MAILSMTP_NO_ERROR) {
dc_log_event_seq(smtp->context, DC_EVENT_ERROR_NETWORK, &smtp->log_connect_errors,
"SMTP-helo failed (%s)", mailsmtp_strerror(r));
goto cleanup;
}
dc_log_info(smtp->context, 0, "SMTP-server %s:%i STARTTLS-connected.", lp->send_server, (int)lp->send_port);
}
else if (lp->server_flags&DC_LP_SMTP_SOCKET_PLAIN)
{
dc_log_info(smtp->context, 0, "SMTP-server %s:%i connected.", lp->send_server, (int)lp->send_port);
}
else
{
dc_log_info(smtp->context, 0, "SMTP-server %s:%i SSL-connected.", lp->send_server, (int)lp->send_port);
}
if (lp->send_user)
{
if (lp->server_flags&DC_LP_AUTH_OAUTH2)
{
dc_log_info(smtp->context, 0, "SMTP-OAuth2 connect...");
char* access_token = dc_get_oauth2_access_token(smtp->context, lp->addr, lp->send_pw, 0);
// if we want to support outlook, there is a mailsmtp_oauth2_outlook_authenticate() ...
if ((r = mailsmtp_oauth2_authenticate(smtp->etpan, lp->send_user, access_token)) != MAILSMTP_NO_ERROR) {
free(access_token);
access_token = dc_get_oauth2_access_token(smtp->context, lp->addr, lp->send_pw, DC_REGENERATE);
r = mailsmtp_oauth2_authenticate(smtp->etpan, lp->send_user, access_token);
}
free(access_token);
}
else if((r=mailsmtp_auth(smtp->etpan, lp->send_user, lp->send_pw))!=MAILSMTP_NO_ERROR)
{
/*
* There are some Mailservers which do not correclty implement PLAIN auth (hMail)
* So here we try a workaround. See https://github.com/deltachat/deltachat-android/issues/67
*/
if (smtp->etpan->auth & MAILSMTP_AUTH_PLAIN) {
dc_log_info(smtp->context, 0, "Trying SMTP-Login workaround \"%s\"...", lp->send_user);
int err;
char hostname[513];
err = gethostname(hostname, sizeof(hostname));
if (err < 0) {
dc_log_error(smtp->context, 0, "SMTP-Login: Cannot get hostname.");
goto cleanup;
}
r = mailesmtp_auth_sasl(smtp->etpan, "PLAIN", hostname, NULL, NULL, NULL, lp->send_user, lp->send_pw, NULL);
}
}