examples/sendmail: Few fixes to make the example run on a local SMTP
server.
This commit is contained in:
parent
72a934c6c2
commit
eb1af03259
@ -33,12 +33,24 @@ config EXAMPLES_SENDMAIL_DRIPADDR
|
|||||||
---help---
|
---help---
|
||||||
The default router address. Default 10.0.0.1 (0x0a000001)
|
The default router address. Default 10.0.0.1 (0x0a000001)
|
||||||
|
|
||||||
|
config EXAMPLES_SENDMAIL_SERVERADDR
|
||||||
|
hex "SMTP Server address"
|
||||||
|
default 0x0a000003
|
||||||
|
---help---
|
||||||
|
The SMTP Server address. Default 10.0.0.3 (0x0a000003)
|
||||||
|
|
||||||
config EXAMPLES_SENDMAIL_NETMASK
|
config EXAMPLES_SENDMAIL_NETMASK
|
||||||
hex "Network Mask"
|
hex "Network Mask"
|
||||||
default 0xffffff00
|
default 0xffffff00
|
||||||
---help---
|
---help---
|
||||||
The network mask. Default: 255.255.255.0 (0xffffff00)
|
The network mask. Default: 255.255.255.0 (0xffffff00)
|
||||||
|
|
||||||
|
config EXAMPLES_SENDMAIL_PORT
|
||||||
|
int "Server Port"
|
||||||
|
default 25
|
||||||
|
---help---
|
||||||
|
The SMTP server's port. Default 25.
|
||||||
|
|
||||||
config EXAMPLES_SENDMAIL_RECIPIENT
|
config EXAMPLES_SENDMAIL_RECIPIENT
|
||||||
string "Recipient email"
|
string "Recipient email"
|
||||||
default "Jane Doe <jane.doe@janedoemail.com>"
|
default "Jane Doe <jane.doe@janedoemail.com>"
|
||||||
|
@ -68,6 +68,14 @@
|
|||||||
# error "You must provide CONFIG_EXAMPLES_SENDMAIL_DRIPADDR"
|
# error "You must provide CONFIG_EXAMPLES_SENDMAIL_DRIPADDR"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_EXAMPLES_SENDMAIL_SERVERADDR
|
||||||
|
# error "You must provide CONFIG_EXAMPLES_SENDMAIL_SERVERADDR"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_EXAMPLES_SENDMAIL_PORT
|
||||||
|
# error "You must provide CONFIG_EXAMPLES_SENDMAIL_PORT"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_EXAMPLES_SENDMAIL_NETMASK
|
#ifndef CONFIG_EXAMPLES_SENDMAIL_NETMASK
|
||||||
# error "You must provide CONFIG_EXAMPLES_SENDMAIL_NETMASK"
|
# error "You must provide CONFIG_EXAMPLES_SENDMAIL_NETMASK"
|
||||||
#endif
|
#endif
|
||||||
@ -88,7 +96,6 @@
|
|||||||
* Private Data
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static const char g_host_name[] = "localhost";
|
|
||||||
static const char g_recipient[] = CONFIG_EXAMPLES_SENDMAIL_RECIPIENT;
|
static const char g_recipient[] = CONFIG_EXAMPLES_SENDMAIL_RECIPIENT;
|
||||||
static const char g_sender[] = CONFIG_EXAMPLES_SENDMAIL_SENDER;
|
static const char g_sender[] = CONFIG_EXAMPLES_SENDMAIL_SENDER;
|
||||||
static const char g_subject[] = CONFIG_EXAMPLES_SENDMAIL_SUBJECT;
|
static const char g_subject[] = CONFIG_EXAMPLES_SENDMAIL_SUBJECT;
|
||||||
@ -105,6 +112,7 @@ static const char g_msg_body[] = CONFIG_EXAMPLES_SENDMAIL_BODY "\r\n";
|
|||||||
int main(int argc, FAR char *argv[])
|
int main(int argc, FAR char *argv[])
|
||||||
{
|
{
|
||||||
struct in_addr addr;
|
struct in_addr addr;
|
||||||
|
in_port_t port;
|
||||||
#if defined(CONFIG_EXAMPLES_SENDMAIL_NOMAC)
|
#if defined(CONFIG_EXAMPLES_SENDMAIL_NOMAC)
|
||||||
uint8_t mac[IFHWADDRLEN];
|
uint8_t mac[IFHWADDRLEN];
|
||||||
#endif
|
#endif
|
||||||
@ -150,11 +158,12 @@ int main(int argc, FAR char *argv[])
|
|||||||
|
|
||||||
/* Then send the mail */
|
/* Then send the mail */
|
||||||
|
|
||||||
net_ipaddr(addr.s_addr, 127, 0, 0, 1);
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_SERVERADDR);
|
||||||
|
port = HTONS(CONFIG_EXAMPLES_SENDMAIL_PORT);
|
||||||
handle = smtp_open();
|
handle = smtp_open();
|
||||||
if (handle)
|
if (handle)
|
||||||
{
|
{
|
||||||
smtp_configure(handle, g_host_name, &addr.s_addr);
|
smtp_configure(handle, CONFIG_LIB_HOSTNAME, &addr.s_addr, &port);
|
||||||
smtp_send(handle, g_recipient, NULL, g_sender, g_subject,
|
smtp_send(handle, g_recipient, NULL, g_sender, g_subject,
|
||||||
g_msg_body, strlen(g_msg_body));
|
g_msg_body, strlen(g_msg_body));
|
||||||
smtp_close(handle);
|
smtp_close(handle);
|
||||||
|
@ -68,8 +68,8 @@ extern "C"
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
void *smtp_open(void);
|
void *smtp_open(void);
|
||||||
void smtp_configure(FAR void *handle, FAR const char *localhostname,
|
void smtp_configure(FAR void *handle, FAR const char *hostname,
|
||||||
FAR const in_addr_t *paddr);
|
FAR const in_addr_t *paddr, FAR const in_port_t *port);
|
||||||
int smtp_send(FAR void *handle, FAR const char *to, FAR const char *cc,
|
int smtp_send(FAR void *handle, FAR const char *to, FAR const char *cc,
|
||||||
FAR const char *from, FAR const char *subject,
|
FAR const char *from, FAR const char *subject,
|
||||||
FAR const char *msg, int msglen);
|
FAR const char *msg, int msglen);
|
||||||
|
@ -91,7 +91,8 @@ struct smtp_state
|
|||||||
bool connected;
|
bool connected;
|
||||||
sem_t sem;
|
sem_t sem;
|
||||||
in_addr_t smtpserver;
|
in_addr_t smtpserver;
|
||||||
const char *localhostname;
|
in_port_t port;
|
||||||
|
const char *hostname;
|
||||||
const char *to;
|
const char *to;
|
||||||
const char *cc;
|
const char *cc;
|
||||||
const char *from;
|
const char *from;
|
||||||
@ -136,7 +137,7 @@ static inline int smtp_send_message(int sockfd, struct smtp_state *psmtp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n",
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n",
|
||||||
g_smtphelo, psmtp->localhostname);
|
g_smtphelo, psmtp->hostname);
|
||||||
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
||||||
{
|
{
|
||||||
return ERROR;
|
return ERROR;
|
||||||
@ -245,7 +246,7 @@ static inline int smtp_send_message(int sockfd, struct smtp_state *psmtp)
|
|||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n",
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n\r\n",
|
||||||
g_smtpsubject, psmtp->subject);
|
g_smtpsubject, psmtp->subject);
|
||||||
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
||||||
{
|
{
|
||||||
@ -295,12 +296,13 @@ static inline int smtp_send_message(int sockfd, struct smtp_state *psmtp)
|
|||||||
* configured.
|
* configured.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void smtp_configure(FAR void *handle, FAR const char *lhostname,
|
void smtp_configure(FAR void *handle, FAR const char *hostname,
|
||||||
FAR const in_addr_t *paddr)
|
FAR const in_addr_t *paddr, FAR const in_port_t *port)
|
||||||
{
|
{
|
||||||
FAR struct smtp_state *psmtp = (FAR struct smtp_state *)handle;
|
FAR struct smtp_state *psmtp = (FAR struct smtp_state *)handle;
|
||||||
psmtp->localhostname = lhostname;
|
psmtp->hostname = hostname;
|
||||||
net_ipv4addr_copy(psmtp->smtpserver, paddr);
|
net_ipv4addr_copy(psmtp->smtpserver, *paddr);
|
||||||
|
psmtp->port = *port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send an e-mail.
|
/* Send an e-mail.
|
||||||
@ -345,8 +347,8 @@ int smtp_send(void *handle, const char *to, const char *cc, const char *from,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
server.sin_family = AF_INET;
|
server.sin_family = AF_INET;
|
||||||
memcpy(&server.sin_addr.s_addr, &psmtp->smtpserver, sizeof(in_addr_t));
|
net_ipv4addr_copy(server.sin_addr.s_addr, psmtp->smtpserver);
|
||||||
server.sin_port = HTONS(25);
|
server.sin_port = psmtp->port;
|
||||||
|
|
||||||
if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0)
|
if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user