设为首页 收藏本站
查看: 737|回复: 0

[经验分享] libcurl sendmail

[复制链接]

尚未签到

发表于 2015-11-25 10:19:48 | 显示全部楼层 |阅读模式
  先上源码
  H
[cpp] view plaincopy

  • /*  
  • * File:   CSendMail.h
  • * Author: jaylong35
  • *
  • * Created on January 16, 2012, 6:14 PM
  • */  
  •   
  • #ifndef CSENDMAIL_H  
  • #define CSENDMAIL_H  
  •   
  • #include <string>  
  • #include <list>  
  • #include <vector>  
  • #include <curl/curl.h>  
  •   
  • #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000  
  •   
  •   
  • class CSendMail {  
  • public:  
  •     CSendMail();  
  •     CSendMail(  //create sendmail object with paremeter;  
  •                 const std::string & strUser,  
  •                 const std::string & strPsw,   
  •                 const std::string & strSmtpServer,   
  •                 int iPort,   
  •                 const std::string & strMailFrom  
  •             );  
  •     CSendMail(const CSendMail& orig);  
  •     virtual ~CSendMail();  
  • private:  
  •     std::string m_strUser;          //邮箱用户名  
  •     std::string m_strPsw;           //邮箱密码  
  •     std::string m_strSmtpServer;        //邮箱SMTP服务器  
  •     int         m_iPort;            //邮箱SMTP服务器端口  
  •     std::list<std::string> m_RecipientList;   //接收者邮件list  
  •     std::string m_strMailFrom;          //发送者邮箱  
  •     std::vector<std::string> m_MailContent;   //发送的内容队列,包括头和内容项  
  •     int         m_iMailContentPos;      //用于发送数据时记录发送到第几个content  
  •       
  • private:  
  •     //发送内容回调函数  
  •     static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp);  
  •     //获取当前时间  
  •     static struct timeval tvnow(void);  
  •     //两个时间差  
  •     static long tvdiff(struct timeval newer, struct timeval older);  
  •     //创建邮件内容  
  •     bool ConstructHead(const std::string & strSubject/*邮件主题*/, const std::string & strContent/*邮件内容*/);  
  •       
  • public:  
  •       
  •     bool SendMail(const std::string & strSubject, const char * pMailBody, int len);  
  •     bool SendMail(const std::string & strSubject, const std::string & strMailBody);  
  •     bool SendMail(  //create sendmail object with paremeter;  
  •                     const std::string & strUser,  
  •                     const std::string & strPsw,   
  •                     const std::string & strSmtpServer,   
  •                     int iPort,   
  •                     std::list<std::string> & recipientList,  
  •                     const std::string & strMailFrom,  
  •                     const std::string & strSubject,   
  •                     const char * pMailBody,   
  •                     int len  
  •                 );  
  •     bool SendMail(  //create sendmail object with paremeter;  
  •                     const std::string & strUser,  
  •                     const std::string & strPsw,   
  •                     const std::string & strSmtpServer,   
  •                     int iPort,   
  •                     const std::string & strMailTo,  
  •                     const std::string & strMailFrom,  
  •                     const std::string & strSubject,   
  •                     const char * pMailBody,   
  •                     int len  
  •                 );  
  •     bool SendMail(  //create sendmail object with paremeter;  
  •                     const std::string & strUser,  
  •                     const std::string & strPsw,   
  •                     const std::string & strSmtpServer,   
  •                     int iPort,   
  •                     std::list<std::string> & recipientList,  
  •                     const std::string & strMailFrom,  
  •                     const std::string & strSubject,   
  •                     const std::string & strMailBody  
  •                 );  
  •     bool SendMail(  //create sendmail object with paremeter;  
  •                     const std::string & strUser,  
  •                     const std::string & strPsw,   
  •                     const std::string & strSmtpServer,   
  •                     int iPort,   
  •                     const std::string & strMailTo,  
  •                     const std::string & strMailFrom,  
  •                     const std::string & strSubject,   
  •                     const std::string & strMailBody  
  •                 );  
  •       
  •     void SetUser(const std::string & strUser) { m_strUser = strUser; }  
  •     std::string & GetUser() { return m_strUser; }  
  •       
  •     void SetPsw(const std::string & strPsw) { m_strPsw = strPsw; }  
  •     std::string & GetPsw() { return m_strPsw; }  
  •       
  •     void SetSmtpServer(const std::string & strSmtpServer) { m_strSmtpServer = strSmtpServer; }  
  •     std::string & GetSmtpServer() { return m_strSmtpServer; }  
  •       
  •     void SetPort(int iPort) { m_iPort = iPort; }  
  •     int GetPort() { return m_iPort; }  
  •       
  •     void SetMailFrom(const std::string & strMailFrom) { m_strMailFrom = strMailFrom; }  
  •     std::string & GetMailFrom() { return m_strMailFrom; }  
  •       
  •     //添加接收者邮箱  
  •     void AddRecipient(const std::string & strMailTo) { m_RecipientList.push_back(strMailTo); }  
  •     void AddRecipient(std::list<std::string> recipientList)   
  •     {   
  •         std::copy(recipientList.begin(), recipientList.end(), m_RecipientList.begin());  
  •     }  
  •     void ClearRecipient() { m_RecipientList.clear(); }  
  •       
  • };  
  •   
  •   
  • #endif  /* CSENDMAIL_H */  
  

  cpp
[cpp] view plaincopy

  • /*  
  • * File:   CSendMail.cpp
  • * Author: root
  • *  
  • * Created on January 16, 2012, 6:14 PM
  • */  
  •   
  • #include &quot;CSendMail.h&quot;  
  •   
  • CSendMail::CSendMail()   
  • {  
  •     m_strUser = &quot;&quot;;  
  •     m_strPsw = &quot;&quot;;  
  •     m_strSmtpServer = &quot;&quot;;  
  •     m_iPort = -1;  
  •     m_RecipientList.clear();  
  •     m_strMailFrom = &quot;&quot;;  
  •     m_MailContent.clear();  
  •     m_iMailContentPos = 0;  
  • }  
  •   
  • CSendMail::CSendMail(  //create sendmail object with paremeter;  
  •                 const std::string & strUser,  
  •                 const std::string & strPsw,   
  •                 const std::string & strSmtpServer,   
  •                 int iPort,   
  •                 const std::string & strMailFrom  
  •             )  
  • {  
  •     m_strUser = strUser;  
  •     m_strPsw = strPsw;  
  •     m_strSmtpServer = strSmtpServer;  
  •     m_iPort = iPort;  
  •     m_RecipientList.clear();  
  •     m_strMailFrom = strMailFrom;  
  •     m_MailContent.clear();  
  •     m_iMailContentPos = 0;  
  • }  
  •   
  • CSendMail::CSendMail(const CSendMail& orig) {  
  • }  
  •   
  • CSendMail::~CSendMail() {  
  • }  
  •   
  • size_t CSendMail::read_callback(void* ptr, size_t size, size_t nmemb, void* userp)  
  • {  
  •     CSendMail * pSm = (CSendMail *)userp;  
  •   
  •     if(size*nmemb < 1)  
  •         return 0;  
  •     if(pSm->m_iMailContentPos < pSm->m_MailContent.size())  
  •     {  
  •         size_t len;  
  •         len = pSm->m_MailContent[pSm->m_iMailContentPos].length();  
  •   
  •         memcpy(ptr, pSm->m_MailContent[pSm->m_iMailContentPos].c_str(), pSm->m_MailContent[pSm->m_iMailContentPos].length());  
  •         pSm->m_iMailContentPos&#43;&#43;; /* advance pointer */  
  •         return len;  
  •     }  
  •     return 0;  
  • }  
  •   
  • struct timeval CSendMail::tvnow()  
  • {  
  •   /*
  •   ** time() returns the value of time in seconds since the Epoch.
  •   */   
  •     struct timeval now;  
  •     now.tv_sec = (long)time(NULL);  
  •     now.tv_usec = 0;  
  •     return now;  
  • }  
  •   
  • long CSendMail::tvdiff(timeval newer, timeval older)  
  • {  
  •     return (newer.tv_sec-older.tv_sec)*1000&#43;  
  •         (newer.tv_usec-older.tv_usec)/1000;  
  • }  
  •   
  • bool CSendMail::ConstructHead(const std::string & strSubject, const std::string & strContent)  
  • {  
  •     m_MailContent.push_back(&quot;MIME-Versioin: 1.0\n&quot;);  
  •     std::string strTemp = &quot;To: &quot;;  
  •     for(std::list<std::string >::iterator it = m_RecipientList.begin(); it != m_RecipientList.end();)  
  •     {  
  •         strTemp &#43;= *it;  
  •         it&#43;&#43;;  
  •         if(it != m_RecipientList.end())  
  •                 strTemp &#43;= &quot;,&quot;;  
  •     }  
  •     strTemp &#43;= &quot;\n&quot;;  
  •     m_MailContent.push_back(strTemp);  
  •     if(strSubject != &quot;&quot;)  
  •     {  
  •         strTemp = &quot;Subject: &quot;;  
  •         strTemp &#43;= strSubject;  
  •         strTemp &#43;= &quot;\n&quot;;  
  •         m_MailContent.push_back(strTemp);  
  •     }  
  •     m_MailContent.push_back(&quot;Content-Transfer-Encoding: 8bit\n&quot;);  
  •     m_MailContent.push_back(&quot;Content-Type: text/html; \n Charset=\&quot;UTF-8\&quot;\n\n&quot;);  
  •     if(strContent != &quot;&quot;)  
  •     {  
  •         m_MailContent.push_back(strContent);  
  •     }  
  •       
  •     return true;  
  • }  
  •   
  • bool CSendMail::SendMail(const std::string& strSubject, const std::string& strMailBody)   
  • {  
  •     m_MailContent.clear();  
  •     m_iMailContentPos = 0;  
  •     ConstructHead(strSubject, strMailBody);  
  •     bool bRet = true;  
  •     CURL *curl;  
  •     CURLM *mcurl;  
  •     int still_running = 1;  
  •     struct timeval mp_start;  
  •     char mp_timedout = 0;  
  •     struct curl_slist* rcpt_list = NULL;  
  •   
  •     curl_global_init(CURL_GLOBAL_DEFAULT);  
  •   
  •     curl = curl_easy_init();  
  •     if (!curl)  
  •     {  
  •         printf(&quot;Init curl failed!\n&quot;);  
  •         return false;  
  •     }  
  •   
  •     mcurl = curl_multi_init();  
  •     if (!mcurl)  
  •     {  
  •         printf(&quot;Init mcurl failed!\n&quot;);  
  •         return false;  
  •     }  
  •     for(std::list<std::string >::iterator it = m_RecipientList.begin(); it != m_RecipientList.end();it&#43;&#43;)  
  •     {  
  •         rcpt_list = curl_slist_append(rcpt_list, it->c_str());  
  •     }  
  •       
  •     if(m_strSmtpServer == &quot;&quot; || m_iPort <= 0)  
  •     {  
  •         printf(&quot;smtp server couldn't be empty, or port must be large than 0!\n&quot;);  
  •          
  •         curl_slist_free_all(rcpt_list);  
  •         curl_multi_cleanup(mcurl);  
  •         curl_easy_cleanup(curl);  
  •         curl_global_cleanup();  
  •         return false;  
  •     }  
  •     std::string strUrl = &quot;smtp://&quot; &#43; m_strSmtpServer;  
  •     strUrl &#43;= &quot;:&quot;;  
  •     char cPort[10];  
  •     memset(cPort, 0, 10);  
  •     sprintf(cPort, &quot;%d&quot;, m_iPort);  
  •     strUrl &#43;= cPort;  
  •     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
  •       
  •     if(m_strUser != &quot;&quot;)  
  •     {  
  •         curl_easy_setopt(curl, CURLOPT_USERNAME, m_strUser.c_str());  
  •     }  
  •     if(m_strPsw != &quot;&quot;)  
  •     {  
  •         curl_easy_setopt(curl, CURLOPT_PASSWORD, m_strPsw.c_str());  
  •     }  
  •       
  •     curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CSendMail::read_callback);  
  •       
  •     if(m_strMailFrom == &quot;&quot;)  
  •     {  
  •         printf(&quot;Mail from address couldn't be empty!\n&quot;);  
  •          
  •         curl_slist_free_all(rcpt_list);  
  •         curl_multi_cleanup(mcurl);  
  •         curl_easy_cleanup(curl);  
  •         curl_global_cleanup();  
  •         return false;  
  •     }  
  •     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, m_strMailFrom.c_str());  
  •     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);  
  •     curl_easy_setopt(curl, CURLOPT_USE_SSL, (long) CURLUSESSL_ALL);  
  •     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);  
  •     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);  
  •     curl_easy_setopt(curl, CURLOPT_READDATA, this);  
  •     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);  
  •     curl_easy_setopt(curl, CURLOPT_SSLVERSION, 0L);  
  •     curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);  
  •     curl_multi_add_handle(mcurl, curl);  
  •   
  •     mp_timedout = 0;  
  •     mp_start = tvnow();  
  •   
  •     /* we start some action by calling perform right away */  
  •     curl_multi_perform(mcurl, &still_running);  
  •   
  •     while (still_running) {  
  •         struct timeval timeout;  
  •         int rc; /* select() return code */  
  •   
  •         fd_set fdread;  
  •         fd_set fdwrite;  
  •         fd_set fdexcep;  
  •         int maxfd = -1;  
  •   
  •         long curl_timeo = -1;  
  •   
  •         FD_ZERO(&fdread);  
  •         FD_ZERO(&fdwrite);  
  •         FD_ZERO(&fdexcep);  
  •   
  •         /* set a suitable timeout to play around with */  
  •         timeout.tv_sec = 1;  
  •         timeout.tv_usec = 0;  
  •   
  •         curl_multi_timeout(mcurl, &curl_timeo);  
  •         if (curl_timeo >= 0) {  
  •             timeout.tv_sec = curl_timeo / 1000;  
  •             if (timeout.tv_sec > 1)  
  •                 timeout.tv_sec = 1;  
  •             else  
  •                 timeout.tv_usec = (curl_timeo % 1000) * 1000;  
  •         }  
  •   
  •         /* get file descriptors from the transfers */  
  •         curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);  
  •   
  •         /* In a real-world program you OF COURSE check the return code of the
  •            function calls.  On success, the value of maxfd is guaranteed to be
  •            greater or equal than -1.  We call select(maxfd &#43; 1, ...), specially in
  •            case of (maxfd == -1), we call select(0, ...), which is basically equal
  •            to sleep. */  
  •   
  •         rc = select(maxfd &#43; 1, &fdread, &fdwrite, &fdexcep, &timeout);  
  •   
  •         if (tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {  
  •             fprintf(stderr, &quot;ABORTING TEST, since it seems &quot;  
  •                     &quot;that it would have run forever.\n&quot;);  
  •             bRet = false;  
  •             break;  
  •         }  
  •   
  •         switch (rc) {  
  •             case -1:  
  •                 /* select error */  
  •                 printf(&quot;select error\n&quot;);  
  •                 bRet = false;  
  •                 break;  
  •             case 0: /* timeout */  
  •                 printf(&quot;time out, retry again!\n&quot;);  
  •                 curl_multi_perform(mcurl, &still_running);  
  •                 break;  
  •             default: /* action */  
  •                 curl_multi_perform(mcurl, &still_running);  
  •                 break;  
  •         }  
  •     }  
  •   
  •     curl_multi_remove_handle(mcurl, curl);  
  •     curl_slist_free_all(rcpt_list);  
  •     curl_multi_cleanup(mcurl);  
  •     curl_easy_cleanup(curl);  
  •     curl_global_cleanup();  
  •     return bRet;  
  • }  
  •   
  • bool CSendMail::SendMail(const std::string & strSubject, const char* pMailBody, int len)  
  • {  
  •     std::string strMailContent;  
  •     strMailContent.append(pMailBody, len);  
  •       
  •     return SendMail(strSubject, strMailContent);  
  • }  
  •   
  • bool CSendMail::SendMail(const std::string& strUser, const std::string& strPsw, const std::string& strSmtpServer, int iPort, std::list<std::string>& recipientList, const std::string& strMailFrom, const std::string& strSubject, const std::string& strMailBody)  
  • {  
  •     m_strUser = strUser;  
  •     m_strPsw = strPsw;  
  •     m_strSmtpServer = strSmtpServer;  
  •     m_iPort = iPort;  
  •     std::copy(recipientList.begin(), recipientList.end(), m_RecipientList.begin());  
  •     m_strMailFrom = strMailFrom;  
  •       
  •     return SendMail(strSubject, strMailBody);  
  •       
  • }  
  •   
  • bool CSendMail::SendMail(const std::string& strUser, const std::string& strPsw, const std::string& strSmtpServer, int iPort, std::list<std::string>& recipientList, const std::string& strMailFrom, const std::string& strSubject, const char* pMailBody, int len)  
  • {  
  •     std::string strMailContent;  
  •     strMailContent.append(pMailBody, len);  
  •     return SendMail(strUser, strPsw, strSmtpServer, iPort, recipientList, strMailFrom, strSubject, strMailContent);  
  • }  
  •   
  • bool CSendMail::SendMail(const std::string& strUser, const std::string& strPsw, const std::string& strSmtpServer, int iPort, const std::string& strMailTo, const std::string& strMailFrom, const std::string& strSubject, const std::string& strMailBody)  
  • {  
  •     std::list<std::string> recipientList;  
  •     recipientList.push_back(strMailTo);  
  •       
  •     return SendMail(strUser, strPsw, strSmtpServer, iPort, recipientList, strMailFrom, strSubject, strMailBody);  
  • }  
  •   
  • bool CSendMail::SendMail(const std::string& strUser, const std::string& strPsw, const std::string& strSmtpServer, int iPort, const std::string& strMailTo, const std::string& strMailFrom, const std::string& strSubject, const char* pMailBody, int len)  
  • {  
  •     std::string strMailContent;  
  •     strMailContent.append(pMailBody, len);  
  •     return SendMail(strUser, strPsw, strSmtpServer, iPort, strMailTo, strMailFrom, strSubject, strMailContent);  
  •   
  • }  

这个是通过libcurl multi接口来做的,所以使用的时候记得要libcurl库的支持。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143309-1-1.html 上篇帖子: zabbix_sendmail.py 下篇帖子: LINUX SENDMAIL服务器的搭建 (二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表