Skip to content

Commit

Permalink
Merge pull request #2 from WKJay/develop
Browse files Browse the repository at this point in the history
增加附件功能
  • Loading branch information
WKJay authored Jun 22, 2020
2 parents a5dcff4 + 7d6887c commit 56a805b
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 126 deletions.
85 changes: 56 additions & 29 deletions example/smtp_client_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,90 @@
Date: 2019-10-14
Author: wangjunjie
Modify: 添加多收件人功能
3. Version: V1.0.2
Date: 2020-06-22
Author: WKJay
Modify: 增加附件功能
*************************************************/
#include "smtp_client.h"
#include "rtthread.h"

//若使用TLS加密则需要更大的堆栈空间
#ifdef SMTP_CLIENT_USING_TLS
#define SMTP_CLIENT_THREAD_STACK_SIZE 4096
#define SMTP_CLIENT_THREAD_STACK_SIZE 8192
#else
#define SMTP_CLIENT_THREAD_STACK_SIZE 2048
#define SMTP_CLIENT_THREAD_STACK_SIZE 4096
#endif

#define DBG_ENABLE
#define DBG_LEVEL 3
#define DBG_COLOR
#define DBG_SECTION_NAME "SMTP_EXAMPLE"
#include "rtdbg.h"

/*
*邮件信息相关宏定义
*/
//smtp 服务器域名
#define SMTP_SERVER_ADDR "smtp.qq.com"
#define SMTP_SERVER_ADDR "smtp.163.com"
//smtp 服务器端口号
#define SMTP_SERVER_PORT "25"
//smtp 登录用户名
#define SMTP_USERNAME ""
#define SMTP_USERNAME ""
//smtp 登录密码(或凭证)
#define SMTP_PASSWORD ""
#define SMTP_PASSWORD ""
//邮件主题
#define SMTP_SUBJECT "SMTP TEST"

#define SMTP_SUBJECT "SMTP TEST"

//邮件内容
char *content = "THIS IS SMTP TEST\r\n"
"HELLO SMTP\r\n"
"--------------------------------------\r\n"
"based on ---> RT-Thread\r\n"
"based on ---> SMTP_CLIENT\r\n";


uint8_t send_enable = 0;

void smtp_thread(void *param)
{
//手动延时等待网络初始化成功
rt_thread_delay(10000);

//初始化smtp客户端
smtp_client_init();
//设置服务器地址
smtp_set_server_addr(SMTP_SERVER_ADDR, ADDRESS_TYPE_DOMAIN, SMTP_SERVER_PORT);
//设置服务器认证信息
smtp_set_auth(SMTP_USERNAME, SMTP_PASSWORD);

//添加收件人1
smtp_add_receiver("[email protected]");
//添加收件人2
smtp_add_receiver("[email protected]");
//添加收件人3
smtp_add_receiver("[email protected]");
//删除收件人2
smtp_delete_receiver("[email protected]");
smtp_add_receiver("[email protected]");

//发送邮件
rt_kprintf("\r\n[smtp]: O > start to send mail\r\n");
if (smtp_send_mail(SMTP_SUBJECT, content) == 0)
{
//发送成功
rt_kprintf("\r\n[smtp]: O > send mail success!\r\n");
}
else
while (1)
{
//发送失败
rt_kprintf("\r\n[smtp]: X > send mail fail!\r\n");
if (send_enable)
{
smtp_add_attachment("/a.txt", "a.txt");
smtp_add_attachment("/b.txt", "b.txt");
//发送邮件
LOG_D("start to send mail");
if (smtp_send_mail(SMTP_SUBJECT, content) == 0)
{
//发送成功
LOG_I("send mail success!");
}
else
{
//发送失败
LOG_E("send mail fail!");
}
//清除附件
smtp_clear_attachments();
//防止频繁发送
rt_thread_mdelay(30000);
send_enable = 0;
}
else
{
rt_thread_mdelay(500);
}
}
}

Expand All @@ -92,3 +111,11 @@ int smtp_thread_entry(void)
return RT_EOK;
}
INIT_APP_EXPORT(smtp_thread_entry);

int smtp_test(uint8_t argc, char *argv[])
{
send_enable = 1;
return 0;
}
MSH_CMD_EXPORT(smtp_test, smtp test);

12 changes: 12 additions & 0 deletions inc/smtp_client.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __SMTP_H
#define __SMTP_H
#include <stdint.h>
#include <rtconfig.h>

//域名类型
#define ADDRESS_TYPE_DOMAIN 0
Expand All @@ -19,4 +20,15 @@ int smtp_send_mail(char *subject, char *body);
int smtp_add_receiver(char *receiver_addr);
//删除指定收件人
int smtp_delete_receiver(char *receiver_addr);
//删除所有收件人
void smtp_clear_receiver(void);

#ifdef SMTP_CLIENT_USING_ATTACHMENT
//添加附件
int smtp_add_attachment(char *file_path, char *file_name);
//清空附件
void smtp_clear_attachments(void);

#endif

#endif /* __SMTP_H */
28 changes: 23 additions & 5 deletions inc/smtp_client_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
#include <tls_client.h>
#endif

#ifdef SMTP_CLIENT_USING_ATTACHMENT
#include <dfs_posix.h>
#endif

#define SMTP_MAX_ADDR_LEN 100
#define SMTP_MAX_AUTH_LEN 50
#define SMTP_MAX_FILE_PATH_LEN 64
#define SMTP_ATTACHMENT_MAX_NAME_LEN 32
#define SMTP_SEND_CMD_MAX_LEN 100
#define SMTP_SEND_DATA_HEAD_MAX_LENGTH 128
#define SMTP_SEND_DATA_HEAD_MAX_LENGTH 256
#define SMTP_SEND_DATA_MAX_LEN 512
#define SMTP_RESPONSE_MAX_LEN 512

Expand Down Expand Up @@ -52,6 +58,14 @@ typedef struct _smtp_address_to
struct _smtp_address_to *next;
} smtp_address_to_t;

//smtp附件链
typedef struct _smtp_attachments
{
char file_path[SMTP_MAX_FILE_PATH_LEN];
char file_name[SMTP_ATTACHMENT_MAX_NAME_LEN];
struct _smtp_attachments *next;
} smtp_attachments_t;

//smtp 会话结构
typedef struct
{
Expand All @@ -60,11 +74,11 @@ typedef struct
//会话超时时间,如果时间为0,标志超时,则自动关闭连接
uint16_t timer;
//smtp服务器域名
const char *server_domain;
char *server_domain;
//smtp服务器ip
const char *server_ip;
char *server_ip;
//smtp服务器端口号
const char *server_port;
char *server_port;
//用户名
char username[SMTP_MAX_AUTH_LEN * 2];
//密码(有些邮箱服务器需要的是用户凭据)
Expand All @@ -79,6 +93,9 @@ typedef struct
char *body;
//smtp连接句柄
int conn_fd;
#ifdef SMTP_CLIENT_USING_ATTACHMENT
smtp_attachments_t *attachments;
#endif
#ifdef SMTP_CLIENT_USING_TLS
//tls会话
MbedTLSSession *tls_session;
Expand All @@ -94,6 +111,7 @@ extern smtp_session_t smtp_session;
#define SMTP_RESP_354 "354"
#define SMTP_RESP_LOGIN_UNAME "VXNlcm5hbWU6"
#define SMTP_RESP_LOGIN_PASS "UGFzc3dvcmQ6"
#define SMTP_MAIL_BOUNDARY "smtp_client_boundary"

#define SMTP_CMD_EHLO "EHLO DM11\r\n"
#define SMTP_CMD_AUTHLOGIN "AUTH LOGIN\r\n"
Expand All @@ -112,7 +130,7 @@ extern smtp_session_t smtp_session;

#ifdef SMTP_CLIENT_USING_TLS
//向SSL/TLS中写入数据
int smtp_mbedtls_client_write(MbedTLSSession *tls_session, char *buf);
int smtp_mbedtls_client_write(MbedTLSSession *tls_session, uint8_t *buf, uint32_t len);
//从 SSL/TLS 中读取数据
int smtp_mbedtls_client_read(MbedTLSSession *tls_session, char *buf, size_t len);

Expand Down
Loading

0 comments on commit 56a805b

Please sign in to comment.