SMTP Client
This commit is contained in:
@ -4,16 +4,74 @@
|
||||
#include <vector>
|
||||
#include <pqxx/pqxx>
|
||||
#include "crow.h"
|
||||
#include "cpp/opportunisticsecuresmtpclient.hpp"
|
||||
#include "cpp/plaintextmessage.hpp"
|
||||
#include "cpp/htmlmessage.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace jed_utils::cpp;
|
||||
|
||||
/*
|
||||
* Utility Manager
|
||||
*/
|
||||
namespace Utilities {
|
||||
/*
|
||||
* Struct representing the configuration file
|
||||
*/
|
||||
struct config {
|
||||
std::string configPath = "cavecomm.conf";
|
||||
std::string emailAddress = "testuser0ed6e@waifu.club";
|
||||
std::string emailPassword = "Ve4m6GSjJ";
|
||||
std::string emailServerAddress = "mail.cock.li";
|
||||
int emailServerPort = 587;
|
||||
std::string emailAddressDisplay = "Cavecomm Automated Management System";
|
||||
|
||||
/*
|
||||
* Parses the config file and fills the struct.
|
||||
*/
|
||||
void readConfigFile(){
|
||||
|
||||
}
|
||||
};
|
||||
/*
|
||||
* Takes String and cuts from the start-up to the length of valueName
|
||||
*/
|
||||
std::string extractSingleValueFromRequestBody(std::string responseBody, std::string valueName){
|
||||
return responseBody.substr(valueName.length() + 1, responseBody.length());
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends an HTML Email using CPP-SMTPClient-library
|
||||
* return 0 at success, 1 at client fail, 2 at critical fail.
|
||||
*/
|
||||
int sendEmail(config configuration, std::string destinationEmail, std::string emailSubject, std::string htmlBody){
|
||||
OpportunisticSecureSMTPClient client(configuration.emailServerAddress, configuration.emailServerPort);
|
||||
client.setCredentials(Credential(configuration.emailAddress, configuration.emailPassword));
|
||||
try {
|
||||
const MessageAddress from(configuration.emailAddress, configuration.emailAddressDisplay);
|
||||
const auto to = { MessageAddress(destinationEmail) };
|
||||
const auto subject = emailSubject;
|
||||
const auto body = htmlBody;
|
||||
const std::vector<MessageAddress> cc = {};
|
||||
const std::vector<MessageAddress> bcc = {};
|
||||
const std::vector<Attachment> attachments = {};
|
||||
HTMLMessage msg(from, to, subject, body, cc, bcc, attachments);
|
||||
|
||||
int err_no = client.sendMail(msg);
|
||||
if (err_no != 0) {
|
||||
std::cerr << client.getCommunicationLog() << '\n';
|
||||
std::string errorMessage = client.getErrorMessage(err_no);
|
||||
std::cerr << "An error occurred: " << errorMessage
|
||||
<< " (error no: " << err_no << ")" << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::cout << client.getCommunicationLog() << '\n';
|
||||
std::cout << "Operation completed!" << std::endl;
|
||||
}
|
||||
catch (std::invalid_argument &err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user