NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
smtp.h File Reference

Send email to an SMTP server. More...

#include "config.h"
#include <stdbool.h>
+ Include dependency graph for smtp.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool smtp_auth_is_valid (const char *authenticator)
 Check if string is a valid smtp authentication method. More...
 
int mutt_smtp_send (const struct AddressList *from, const struct AddressList *to, const struct AddressList *cc, const struct AddressList *bcc, const char *msgfile, bool eightbit, struct ConfigSubset *sub)
 Send a message using SMTP. More...
 

Detailed Description

Send email to an SMTP server.

Authors
  • Richard Russon
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file smtp.h.

Function Documentation

◆ smtp_auth_is_valid()

bool smtp_auth_is_valid ( const char *  authenticator)

Check if string is a valid smtp authentication method.

Parameters
authenticatorAuthenticator string to check
Return values
trueArgument is a valid auth method

Validate whether an input string is an accepted smtp authentication method as defined by SmtpAuthenticators.

Definition at line 740 of file smtp.c.

741 {
742  for (size_t i = 0; i < mutt_array_size(SmtpAuthenticators); i++)
743  {
744  const struct SmtpAuth *auth = &SmtpAuthenticators[i];
745  if (auth->method && mutt_istr_equal(auth->method, authenticator))
746  return true;
747  }
748 
749  return false;
750 }
#define mutt_array_size(x)
Definition: memory.h:33
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:727
static const struct SmtpAuth SmtpAuthenticators[]
Accepted authentication methods.
Definition: smtp.c:720
SMTP authentication multiplexor.
Definition: smtp.c:106
const char * method
Name of authentication method supported, NULL means variable.
Definition: smtp.c:115
+ Here is the call graph for this function:

◆ mutt_smtp_send()

int mutt_smtp_send ( const struct AddressList *  from,
const struct AddressList *  to,
const struct AddressList *  cc,
const struct AddressList *  bcc,
const char *  msgfile,
bool  eightbit,
struct ConfigSubset sub 
)

Send a message using SMTP.

Parameters
fromFrom Address
toTo Address
ccCc Address
bccBcc Address
msgfileMessage to send to the server
eightbitIf true, try for an 8-bit friendly connection
subConfig Subset
Return values
0Success
-1Error

Definition at line 903 of file smtp.c.

906 {
907  struct SmtpAccountData adata = { 0 };
908  struct ConnAccount cac = { { 0 } };
909  const char *envfrom = NULL;
910  char buf[1024];
911  int rc = -1;
912 
913  adata.sub = sub;
914  adata.fqdn = mutt_fqdn(false, adata.sub);
915  if (!adata.fqdn)
916  adata.fqdn = NONULL(ShortHostname);
917 
918  const struct Address *c_envelope_from_address =
919  cs_subset_address(adata.sub, "envelope_from_address");
920 
921  /* it might be better to synthesize an envelope from from user and host
922  * but this condition is most likely arrived at accidentally */
923  if (c_envelope_from_address)
924  envfrom = c_envelope_from_address->mailbox;
925  else if (from && !TAILQ_EMPTY(from))
926  envfrom = TAILQ_FIRST(from)->mailbox;
927  else
928  {
929  mutt_error(_("No from address given"));
930  return -1;
931  }
932 
933  if (smtp_fill_account(&adata, &cac) < 0)
934  return rc;
935 
936  adata.conn = mutt_conn_find(&cac);
937  if (!adata.conn)
938  return -1;
939 
940  const char *const c_dsn_return = cs_subset_string(adata.sub, "dsn_return");
941 
942  do
943  {
944  /* send our greeting */
945  rc = smtp_open(&adata, eightbit);
946  if (rc != 0)
947  break;
948  FREE(&adata.auth_mechs);
949 
950  /* send the sender's address */
951  int len = snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", envfrom);
952  if (eightbit && (adata.capabilities & SMTP_CAP_EIGHTBITMIME))
953  {
954  mutt_strn_cat(buf, sizeof(buf), " BODY=8BITMIME", 15);
955  len += 14;
956  }
957  if (c_dsn_return && (adata.capabilities & SMTP_CAP_DSN))
958  len += snprintf(buf + len, sizeof(buf) - len, " RET=%s", c_dsn_return);
959  if ((adata.capabilities & SMTP_CAP_SMTPUTF8) &&
962  {
963  snprintf(buf + len, sizeof(buf) - len, " SMTPUTF8");
964  }
965  mutt_strn_cat(buf, sizeof(buf), "\r\n", 3);
966  if (mutt_socket_send(adata.conn, buf) == -1)
967  {
968  rc = SMTP_ERR_WRITE;
969  break;
970  }
971  rc = smtp_get_resp(&adata);
972  if (rc != 0)
973  break;
974 
975  /* send the recipient list */
976  if ((rc = smtp_rcpt_to(&adata, to)) || (rc = smtp_rcpt_to(&adata, cc)) ||
977  (rc = smtp_rcpt_to(&adata, bcc)))
978  {
979  break;
980  }
981 
982  /* send the message data */
983  rc = smtp_data(&adata, msgfile);
984  if (rc != 0)
985  break;
986 
987  mutt_socket_send(adata.conn, "QUIT\r\n");
988 
989  rc = 0;
990  } while (false);
991 
992  mutt_socket_close(adata.conn);
993  FREE(&adata.conn);
994 
995  if (rc == SMTP_ERR_READ)
996  mutt_error(_("SMTP session failed: read error"));
997  else if (rc == SMTP_ERR_WRITE)
998  mutt_error(_("SMTP session failed: write error"));
999  else if (rc == SMTP_ERR_CODE)
1000  mutt_error(_("Invalid server response"));
1001 
1002  return rc;
1003 }
bool mutt_addrlist_uses_unicode(const struct AddressList *al)
Do any of a list of addresses use Unicode characters.
Definition: address.c:1532
bool mutt_addr_uses_unicode(const char *str)
Does this address use Unicode character.
Definition: address.c:1512
#define mutt_error(...)
Definition: logging.h:87
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: helpers.c:49
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
#define FREE(x)
Definition: memory.h:40
#define _(a)
Definition: message.h:28
char * mutt_strn_cat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:225
char * ShortHostname
Short version of the hostname.
Definition: mutt_globals.h:50
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:89
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:37
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_EMPTY(head)
Definition: queue.h:721
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:1195
static int smtp_get_resp(struct SmtpAccountData *adata)
Read a command response from the SMTP server.
Definition: smtp.c:137
#define SMTP_ERR_READ
Definition: smtp.c:64
#define SMTP_ERR_CODE
Definition: smtp.c:66
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:84
static int smtp_data(struct SmtpAccountData *adata, const char *msgfile)
Send data to an SMTP server.
Definition: smtp.c:224
#define SMTP_ERR_WRITE
Definition: smtp.c:65
static int smtp_fill_account(struct SmtpAccountData *adata, struct ConnAccount *cac)
Create ConnAccount object from SMTP Url.
Definition: smtp.c:342
#define SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
Definition: smtp.c:85
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:83
static int smtp_rcpt_to(struct SmtpAccountData *adata, const struct AddressList *al)
Set the recipient to an Address.
Definition: smtp.c:187
static int smtp_open(struct SmtpAccountData *adata, bool esmtp)
Open an SMTP Connection.
Definition: smtp.c:822
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:97
#define NONULL(x)
Definition: string2.h:37
An email address.
Definition: address.h:36
char * mailbox
Mailbox and host address.
Definition: address.h:38
Login details for a remote server.
Definition: connaccount.h:53
Server connection data.
Definition: smtp.c:94
const char * fqdn
Fully-qualified domain name.
Definition: smtp.c:99
struct ConfigSubset * sub
Config scope.
Definition: smtp.c:98
struct Connection * conn
Server Connection.
Definition: smtp.c:97
const char * auth_mechs
Allowed authorisation mechanisms.
Definition: smtp.c:95
SmtpCapFlags capabilities
Server capabilities.
Definition: smtp.c:96
+ Here is the call graph for this function:
+ Here is the caller graph for this function: