NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
smtp.h File Reference

Send email to an SMTP server. More...

#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.
 
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.
 

Detailed Description

Send email to an SMTP server.

Authors
  • Pietro Cerutti
  • Richard Russon

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 959 of file smtp.c.

960{
961 for (size_t i = 0; i < countof(SmtpAuthenticators); i++)
962 {
963 const struct SmtpAuth *auth = &SmtpAuthenticators[i];
964 if (auth->method && mutt_istr_equal(auth->method, authenticator))
965 return true;
966 }
967
968 return false;
969}
#define countof(x)
Definition memory.h:49
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
static const struct SmtpAuth SmtpAuthenticators[]
Accepted authentication methods.
Definition smtp.c:936
SMTP authentication multiplexor.
Definition smtp.c:114
const char * method
Name of authentication method supported, NULL means variable.
Definition smtp.c:125
+ Here is the call graph for this function:
+ Here is the caller 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 1132 of file smtp.c.

1135{
1136 struct SmtpAccountData adata = { 0 };
1137 struct ConnAccount cac = { { 0 } };
1138 const char *envfrom = NULL;
1139 int rc = -1;
1140
1141 adata.sub = sub;
1142 adata.fqdn = mutt_fqdn(false, adata.sub);
1143 if (!adata.fqdn)
1144 adata.fqdn = NONULL(ShortHostname);
1145
1146 const struct Address *c_envelope_from_address = cs_subset_address(adata.sub, "envelope_from_address");
1147
1148 if (smtp_fill_account(&adata, &cac) < 0)
1149 return rc;
1150
1151 adata.conn = mutt_conn_find(&cac);
1152 if (!adata.conn)
1153 return -1;
1154
1155 /* it might be better to synthesize an envelope from from user and host
1156 * but this condition is most likely arrived at accidentally */
1157 if (c_envelope_from_address)
1158 {
1159 envfrom = buf_string(c_envelope_from_address->mailbox);
1160 }
1161 else if (from && !TAILQ_EMPTY(from))
1162 {
1163 envfrom = buf_string(TAILQ_FIRST(from)->mailbox);
1164 }
1165 else
1166 {
1167 mutt_error(_("No from address given"));
1168 mutt_socket_close(adata.conn);
1169 return -1;
1170 }
1171
1172 const char *const c_dsn_return = cs_subset_string(adata.sub, "dsn_return");
1173
1174 struct Buffer *buf = buf_pool_get();
1175 do
1176 {
1177 /* send our greeting */
1178 rc = smtp_open(&adata, eightbit);
1179 if (rc != 0)
1180 break;
1181 FREE(&adata.auth_mechs);
1182
1183 /* send the sender's address */
1184 buf_printf(buf, "MAIL FROM:<%s>", envfrom);
1185 if (eightbit && (adata.capabilities & SMTP_CAP_EIGHTBITMIME))
1186 buf_addstr(buf, " BODY=8BITMIME");
1187
1188 if (c_dsn_return && (adata.capabilities & SMTP_CAP_DSN))
1189 buf_add_printf(buf, " RET=%s", c_dsn_return);
1190
1191 if ((adata.capabilities & SMTP_CAP_SMTPUTF8) &&
1194 {
1195 buf_addstr(buf, " SMTPUTF8");
1196 }
1197 buf_addstr(buf, "\r\n");
1198 if (mutt_socket_send(adata.conn, buf_string(buf)) == -1)
1199 {
1200 rc = SMTP_ERR_WRITE;
1201 break;
1202 }
1203 rc = smtp_get_resp(&adata);
1204 if (rc != 0)
1205 break;
1206
1207 /* send the recipient list */
1208 if ((rc = smtp_rcpt_to(&adata, to)) || (rc = smtp_rcpt_to(&adata, cc)) ||
1209 (rc = smtp_rcpt_to(&adata, bcc)))
1210 {
1211 break;
1212 }
1213
1214 /* send the message data */
1215 rc = smtp_data(&adata, msgfile);
1216 if (rc != 0)
1217 break;
1218
1219 mutt_socket_send(adata.conn, "QUIT\r\n");
1220
1221 rc = 0;
1222 } while (false);
1223
1224 mutt_socket_close(adata.conn);
1225 FREE(&adata.conn);
1226 FREE(&adata.auth_mechs);
1227
1228 if (rc == SMTP_ERR_READ)
1229 mutt_error(_("SMTP session failed: read error"));
1230 else if (rc == SMTP_ERR_WRITE)
1231 mutt_error(_("SMTP session failed: write error"));
1232 else if (rc == SMTP_ERR_CODE)
1233 mutt_error(_("Invalid server response"));
1234
1235 buf_pool_release(&buf);
1236 return rc;
1237}
bool mutt_addrlist_uses_unicode(const struct AddressList *al)
Do any of a list of addresses use Unicode characters.
Definition address.c:1531
bool mutt_addr_uses_unicode(const char *str)
Does this address use Unicode character.
Definition address.c:1511
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
char * ShortHostname
Short version of the hostname.
Definition globals.c:36
#define mutt_error(...)
Definition logging2.h:94
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define _(a)
Definition message.h:28
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition mutt_socket.c:88
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define TAILQ_FIRST(head)
Definition queue.h:780
#define TAILQ_EMPTY(head)
Definition queue.h:778
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition sendlib.c:713
static int smtp_get_resp(struct SmtpAccountData *adata)
Read a command response from the SMTP server.
Definition smtp.c:146
#define SMTP_ERR_READ
Error reading from server.
Definition smtp.c:69
#define SMTP_ERR_CODE
Invalid server response code.
Definition smtp.c:71
static int smtp_data(struct SmtpAccountData *adata, const char *msgfile)
Send data to an SMTP server.
Definition smtp.c:251
#define SMTP_ERR_WRITE
Error writing to server.
Definition smtp.c:70
static int smtp_fill_account(struct SmtpAccountData *adata, struct ConnAccount *cac)
Create ConnAccount object from SMTP Url.
Definition smtp.c:369
@ SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
Definition smtp.c:91
@ SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition smtp.c:90
@ SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition smtp.c:89
static int smtp_rcpt_to(struct SmtpAccountData *adata, const struct AddressList *al)
Set the recipient to an Address.
Definition smtp.c:204
static int smtp_open(struct SmtpAccountData *adata, bool esmtp)
Open an SMTP Connection.
Definition smtp.c:1050
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition socket.c:100
#define mutt_socket_send(conn, buf)
Definition socket.h:56
#define NONULL(x)
Definition string2.h:44
An email address.
Definition address.h:35
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
String manipulation buffer.
Definition buffer.h:36
Login details for a remote server.
Definition connaccount.h:59
Server connection data.
Definition smtp.c:102
const char * fqdn
Fully-qualified domain name.
Definition smtp.c:107
struct ConfigSubset * sub
Config scope.
Definition smtp.c:106
struct Connection * conn
Server Connection.
Definition smtp.c:105
const char * auth_mechs
Allowed authorisation mechanisms.
Definition smtp.c:103
SmtpCapFlags capabilities
Server capabilities.
Definition smtp.c:104
+ Here is the call graph for this function:
+ Here is the caller graph for this function: