SMTP Client

This commit is contained in:
Tina_Azure 2023-04-07 22:29:37 +02:00
parent caca7139cb
commit 80f377c048
2 changed files with 60 additions and 2 deletions

View File

@ -1,5 +1,5 @@
.POSIX:
INC=-I/home/user/project/resources/boost_1_81_0 -L/home/user/project/resources/boost_1_81_0/stage/lib
INC=-I/home/user/project/resources/boost_1_81_0 -I/home/user/project/resources/CPP-SMTPClient-library/src -I/usr/include -L/usr/lib -L/home/user/project/resources/CPP-SMTPClient-library -L/home/user/project/resources/boost_1_81_0/stage/lib
SRCFILES = src/main.cpp
db:
@ -17,7 +17,7 @@ all:
g++ $(INC) -o cavecomm $(SRCFILES) -lpqxx -lfmt -pthread
build:
g++ $(INC) -o cavecomm $(SRCFILES) -lpqxx -lfmt -pthread
g++ $(INC) -o cavecomm $(SRCFILES) -lpqxx -lfmt -l:libsmtpclient.a -lcrypto -lssl -pthread
release:
mkdir bin

View File

@ -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;
}
}