Skip to content

Commit 130fd07

Browse files
authored
Merge pull request #87 from alex-che/feature/add-smtp
Feature/add smtp
2 parents 511241a + 1bf9d3f commit 130fd07

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

examples/README

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ usage.
3232
Example 23: Setting request options using iterators to custom container
3333
of curlpp options.
3434
Example 24: Binded method functor for DebugFunction example.
35-
35+
Example 25: Send e-mail over SMTP.
36+
3637

3738

examples/example25.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files
6+
* (curlpp), to deal in the Software without restriction,
7+
* including without limitation the rights to use, copy, modify, merge,
8+
* publish, distribute, sublicense, and/or sell copies of the Software,
9+
* and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included
13+
* in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
/**
25+
* \file
26+
* Send e-mail with SMTP.
27+
*/
28+
29+
30+
#include <cstdlib>
31+
#include <cstdio>
32+
#include <cstring>
33+
#include <sstream>
34+
35+
#include <curlpp/cURLpp.hpp>
36+
#include <curlpp/Easy.hpp>
37+
#include <curlpp/Options.hpp>
38+
#include <curlpp/Exception.hpp>
39+
40+
/*
41+
anonymous namespace to prevent name clash in case other examples using the same global entities
42+
would be compiled in the same project
43+
*/
44+
namespace
45+
{
46+
47+
// Change those to appropriate values:
48+
const std::string smtpUrl = "smtp://mail.example.com";
49+
const std::string fromAddress = "<[email protected]>";
50+
const std::string toAddress = "<[email protected]>";
51+
const std::string ccAddress = "<[email protected]>";
52+
const std::string fromMail = "Sender Person " + fromAddress;
53+
const std::string toMail = "A Receiver " + toAddress;
54+
const std::string ccMail = "John CC Smith " + ccAddress;
55+
const std::string mailDate = "Wed, 28 Aug 2019 16:26:00 +0300";
56+
const std::string mailSubject = "SMTP example message";
57+
const std::string mailText = "Hi there,\r\n\r\nThis is a message from curlpp!\r\n";
58+
59+
} // namespace
60+
61+
int main(int argc, char *argv[])
62+
{
63+
std::stringstream dataStream;
64+
dataStream
65+
<< "Date: " << mailDate << "\r\n"
66+
<< "To: " << toMail << "\r\n"
67+
<< "From: " << fromMail << "\r\n"
68+
<< "Cc: " << ccMail << "\r\n"
69+
<< "Subject: " << mailSubject << "\r\n"
70+
<< "\r\n"
71+
<< mailText;
72+
long size = dataStream.str().size();
73+
74+
std::list<std::string> recipients;
75+
recipients.push_back(toAddress);
76+
recipients.push_back(ccAddress);
77+
78+
try
79+
{
80+
curlpp::Cleanup cleaner;
81+
curlpp::Easy request;
82+
83+
using namespace curlpp::Options;
84+
request.setOpt(new Verbose(true));
85+
request.setOpt(new Url(smtpUrl));
86+
request.setOpt(new MailFrom(fromAddress));
87+
request.setOpt(new MailRcpt(recipients));
88+
request.setOpt(new ReadStream(&dataStream));
89+
request.setOpt(new InfileSize(size));
90+
request.setOpt(new Upload(true));
91+
92+
request.perform();
93+
}
94+
catch (curlpp::LogicError & e)
95+
{
96+
std::cout << e.what() << std::endl;
97+
}
98+
catch (curlpp::RuntimeError & e)
99+
{
100+
std::cout << e.what() << std::endl;
101+
}
102+
103+
return 0;
104+
}

include/curlpp/Options.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,28 @@ namespace options
258258

259259

260260

261+
/**
262+
* SMTP options.
263+
*/
264+
265+
#if LIBCURL_VERSION_NUM >= 0x071900
266+
typedef curlpp::OptionTrait<std::string, CURLOPT_MAIL_AUTH> MailAuth;
267+
#else
268+
#ifdef CURLPP_ALLOW_NOT_AVAILABLE
269+
typedef curlpp::NotAvailableOptionTrait<std::string, CURLOPT_MAIL_AUTH> MailAuth;
270+
#endif
271+
#endif
272+
#if LIBCURL_VERSION_NUM >= 0x071400
273+
typedef curlpp::OptionTrait<std::string, CURLOPT_MAIL_FROM> MailFrom;
274+
typedef curlpp::OptionTrait<std::list<std::string>, CURLOPT_MAIL_RCPT> MailRcpt;
275+
#else
276+
#ifdef CURLPP_ALLOW_NOT_AVAILABLE
277+
typedef curlpp::OptionTrait<std::string, CURLOPT_MAIL_FROM> MailFrom;
278+
typedef curlpp::OptionTrait<std::list<std::string>, CURLOPT_MAIL_RCPT> MailRcpt;
279+
#endif
280+
#endif
281+
282+
261283
/**
262284
* Protocol options.
263285
*/

0 commit comments

Comments
 (0)