+/*
+ * 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';
+
+ return ret;
+}
+#endif