+static void event_cb (smtp_session_t session __attribute__ ((unused)),
+ int event_no, void *arg,...)
+{
+ va_list alist;
+ int *ok;
+
+ va_start(alist, arg);
+ switch(event_no) {
+ case SMTP_EV_CONNECT:
+ case SMTP_EV_MAILSTATUS:
+ case SMTP_EV_RCPTSTATUS:
+ case SMTP_EV_MESSAGEDATA:
+ case SMTP_EV_MESSAGESENT:
+ case SMTP_EV_DISCONNECT: break;
+ case SMTP_EV_WEAK_CIPHER: {
+ int bits;
+ bits = va_arg(alist, long); ok = va_arg(alist, int*);
+ mutt_message (_("SMTP_EV_WEAK_CIPHER, bits=%d - accepted."), bits);
+ sleep(1);
+ *ok = 1; break;
+ }
+ case SMTP_EV_STARTTLS_OK:
+ mutt_message (_("Using TLS"));
+ sleep(1);
+ break;
+ case SMTP_EV_INVALID_PEER_CERTIFICATE: {
+ long vfy_result;
+ vfy_result = va_arg(alist, long); ok = va_arg(alist, int*);
+ *ok = handle_invalid_peer_certificate(vfy_result);
+ sleep(1);
+ break;
+ }
+ case SMTP_EV_NO_PEER_CERTIFICATE: {
+ ok = va_arg(alist, int*);
+ mutt_message (_("SMTP_EV_NO_PEER_CERTIFICATE - accepted."));
+ sleep(1);
+ *ok = 1; break;
+ }
+ case SMTP_EV_WRONG_PEER_CERTIFICATE: {
+ ok = va_arg(alist, int*);
+ mutt_message (_("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted."));
+ sleep(1);
+ *ok = 1; break;
+ }
+ case SMTP_EV_NO_CLIENT_CERTIFICATE: {
+ ok = va_arg(alist, int*);
+ mutt_message (_("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted."));
+ sleep(1);
+ *ok = 1; break;
+ }
+ case SMTP_EV_EXTNA_DSN:
+ extna ("DSN");
+ break;
+ case SMTP_EV_EXTNA_STARTTLS:
+ extna ("StartTLS");
+ break;
+ case SMTP_EV_EXTNA_8BITMIME:
+ extna ("8BITMIME");
+ break;
+ default:
+ mutt_message(_("Got unhandled event ID = %d - ignored."), event_no);
+ sleep(1);
+ }
+ va_end(alist);
+}
+
+static void do_dsn_notify (smtp_message_t message, const char* from) {
+ int flags = Notify_NOTSET;
+ smtp_recipient_t self = NULL;
+
+ if (m_strisempty(MTransport.dsn_notify) || !message || m_strisempty(from) ||
+ strstr (MTransport.dsn_notify, "never") != NULL)
+ return;
+
+ if (strstr (MTransport.dsn_notify, "failure") != NULL)
+ flags |= Notify_FAILURE;
+ if (strstr (MTransport.dsn_notify, "delay") != NULL)
+ flags |= Notify_DELAY;
+ if (strstr (MTransport.dsn_notify, "success") != NULL)
+ flags |= Notify_SUCCESS;
+
+ if (flags != Notify_NOTSET) {
+ if (!(self = smtp_add_recipient (message, from)))
+ return;
+ smtp_dsn_set_notify (self, flags);
+ }
+}
+
+static void do_dsn_ret (smtp_message_t message) {
+ if (m_strisempty(MTransport.dsn_return) || !message)
+ return;
+ if (ascii_strncasecmp (MTransport.dsn_return, "hdrs", 4) == 0)
+ smtp_dsn_set_ret (message, Ret_HDRS);
+ else if (ascii_strncasecmp (MTransport.dsn_return, "full", 4) == 0)
+ smtp_dsn_set_ret (message, Ret_FULL);
+}
+
+int send_smtp_check_usetls (const char* option, unsigned long p,
+ char* errbuf, ssize_t errlen) {
+ char* val = (char*) p;
+ if (m_strisempty(val))
+ return (1);
+ if (m_strncmp(val, "enabled", 7) != 0 &&
+ m_strncmp(val, "required", 8) != 0) {
+ if (errbuf)
+ snprintf (errbuf, errlen, _("'%s' is invalid for %s"), val, option);
+ return (0);
+ }
+ return (1);
+}
+
+/*
+ * send_smtp_invoke
+ * Sends a mail message to the provided recipients using libesmtp.
+ * Returns 0 upon success, -1 upon failure (and prints an error
+ * message).
+ */
+static int
+send_smtp_invoke(address_t *from, address_t *to, address_t *cc,
+ address_t *bcc, const char *msg, int eightbit)
+{ /* message contains 8bit chars */
+ int ret = 0; /* return value, default = success */
+ smtp_session_t session;
+ smtp_message_t message;
+ char *hostportstr = NULL;
+ size_t hostportlen;
+ FILE *fp = NULL;
+ auth_context_t authctx = NULL;
+ const smtp_status_t *status;
+ char* envfrom = from->mailbox;
+
+ _send_smtp_ensure_init ();
+
+ if ((session = smtp_create_session ()) == NULL)
+ SMTPFAIL ("smtp_create_session");
+
+#ifdef HAVE_GNUTLS_OPENSSL_H
+ if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
+ smtp_starttls_enable(session, Starttls_ENABLED);
+ } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
+ smtp_starttls_enable(session, Starttls_REQUIRED);
+ }
+#endif
+
+ /* Create hostname:port string and tell libesmtp */
+ /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
+ hostportlen = m_strlen(SmtpHost) + 7;
+ hostportstr = p_new(char, hostportlen);
+ snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
+ if (!smtp_set_server (session, hostportstr))
+ SMTPFAIL ("smtp_set_server");
+
+ if (SmtpAuthUser) {
+ if ((authctx = auth_create_context ()) == NULL)
+ MSGFAIL ("auth_create_context failed");
+ auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
+ auth_set_interact_cb (authctx, _send_smtp_auth_interact, NULL);
+
+ if (!smtp_auth_set_context (session, authctx))
+ SMTPFAIL ("smtp_auth_set_context");
+ }
+
+#ifdef HAVE_GNUTLS_OPENSSL_H
+ smtp_starttls_set_ctx (session, NULL);
+#endif
+ smtp_set_eventcb (session, event_cb, NULL);
+
+ if ((message = smtp_add_message (session)) == NULL)
+ SMTPFAIL ("smtp_add_message");
+
+ /* Initialize envelope sender */
+ if (MTransport.use_envelope_from && MTransport.envelope_from_address)
+ envfrom = MTransport.envelope_from_address->mailbox;
+ if (!smtp_set_reverse_path (message, envfrom))
+ SMTPFAIL ("smtp_set_reverse_path");
+
+ /* set up DSN for message */
+ do_dsn_notify (message, envfrom);
+ do_dsn_ret (message);
+
+ /* set up 8bitmime flag */
+ if (eightbit && MTransport.use_8bitmime)
+ smtp_8bitmime_set_body (message, E8bitmime_8BITMIME);
+
+ if ((fp = fopen (msg, "r")) == NULL)
+ LIBCFAIL ("fopen");
+ if (!smtp_set_messagecb (message, _send_smtp_messagefp_cb, fp))
+ SMTPFAIL ("smtp_set_messagecb");
+ if (_send_smtp_add_recipients (message, to))
+ FAIL ();
+ if (_send_smtp_add_recipients (message, cc))
+ FAIL ();
+ if (_send_smtp_add_recipients (message, bcc))
+ FAIL ();
+ if (!smtp_start_session (session))
+ SMTPFAIL ("smtp_start_session");
+
+ status = smtp_message_transfer_status (message);
+ if (status->code < 200 || status->code > 299) {
+ char buf[256];
+
+ snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
+ status->code, status->text);
+ MSGFAIL (buf);
+ }
+
+Done:
+ m_fclose(&fp);
+ if (hostportstr != NULL)
+ p_delete(&hostportstr);
+ if (session != NULL)
+ smtp_destroy_session (session);
+ if (authctx != NULL)
+ auth_destroy_context (authctx);
+
+ /* Forget user-entered SMTP AUTH password if send fails */
+ if (ret != 0)
+ authpass[0] = '\0';