smtp.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef HV_SMTP_H_
  2. #define HV_SMTP_H_
  3. #include "hexport.h"
  4. #define SMTP_PORT 25
  5. #define SMTPS_PORT 465
  6. #define SMTP_EOB "\r\n.\r\n"
  7. #define SMTP_EOB_LEN 5
  8. // smtp_command
  9. // XX(name, string)
  10. #define SMTP_COMMAND_MAP(XX)\
  11. XX(HELO, HELO) \
  12. XX(EHLO, EHLO) \
  13. XX(AUTH, AUTH) \
  14. XX(MAIL, MAIL FROM:) \
  15. XX(RCPT, RCPT TO:) \
  16. XX(DATA, DATA) \
  17. XX(QUIT, QUIT) \
  18. enum smtp_command {
  19. #define XX(name, string) SMTP_##name,
  20. SMTP_COMMAND_MAP(XX)
  21. #undef XX
  22. };
  23. // smtp_status
  24. // XXX(code, name, string)
  25. #define SMTP_STATUS_MAP(XXX) \
  26. XXX(220, READY, Ready) \
  27. XXX(221, BYE, Bye) \
  28. XXX(235, AUTH_SUCCESS, Authentication success) \
  29. XXX(250, OK, OK) \
  30. XXX(334, AUTH, Auth input) \
  31. XXX(354, DATA, End with <CR><LF>.<CR><LF>) \
  32. XXX(500, BAD_SYNTAX, Bad syntax) \
  33. XXX(502, NOT_IMPLEMENTED,Command not implemented) \
  34. XXX(503, BAD_SEQUENCE, Bad sequence of commands) \
  35. XXX(504, UNRECOGNIZED_AUTH_TYPE, Unrecognized authentication type) \
  36. XXX(535, AUTH_FAILED, Authentication failed) \
  37. XXX(553, ERR_MAIL, Mailbox name not allowed) \
  38. XXX(554, ERR_DATA, Transaction failed) \
  39. enum smtp_status {
  40. #define XXX(code, name, string) SMTP_STATUS_##name = code,
  41. SMTP_STATUS_MAP(XXX)
  42. #undef XXX
  43. };
  44. typedef struct mail_s {
  45. char* from;
  46. char* to;
  47. char* subject;
  48. char* body;
  49. } mail_t;
  50. BEGIN_EXTERN_C
  51. HV_EXPORT const char* smtp_command_str(enum smtp_command cmd);
  52. HV_EXPORT const char* smtp_status_str(enum smtp_status status);
  53. // cmd param\r\n
  54. HV_EXPORT int smtp_build_command(enum smtp_command cmd, const char* param, char* buf, int buflen);
  55. // status_code status_message\r\n
  56. HV_EXPORT int sendmail(const char* smtp_server,
  57. const char* username,
  58. const char* password,
  59. mail_t* mail);
  60. END_EXTERN_C
  61. #endif // HV_SMTP_H_