|
#include <fstream>
|
|
|
|
#include <Wt/Mail/Client.h>
|
|
#include <Wt/Mail/Message.h>
|
|
|
|
using namespace std;
|
|
using namespace Wt;
|
|
|
|
std::string SendSMTPEMail(DBAccess::Database *db, std::string const &smtpDomain, std::string const &smtpHost, int port, std::string const &sourceEMail, std::string const &pwd, std::string const &sourceEMailName, std::string const &usrMailBox, std::string const &subject, std::string const &msg, std::string const &attachment)
|
|
{
|
|
|
|
Mail::Message mailMessage;
|
|
bool okToGo = true;
|
|
filebuf sourceFile;
|
|
if (attachment.length()) {
|
|
try {
|
|
if (sourceFile.open(attachment.c_str(), ios::in | ios::binary)) {
|
|
string mimeType(GetFileMimeType(db, attachment));
|
|
mailMessage.addAttachment(mimeType, attachment, &istream(&sourceFile));
|
|
}
|
|
else
|
|
okToGo = false;
|
|
}
|
|
catch (...) {
|
|
okToGo = false;
|
|
}
|
|
}
|
|
string errorMsg;
|
|
if (okToGo) {
|
|
mailMessage.setFrom(Mail::Mailbox(sourceEMail, sourceEMailName));
|
|
mailMessage.setDate(WLocalDateTime::currentServerDateTime());
|
|
mailMessage.setSubject(subject);
|
|
mailMessage.addRecipient(Mail::RecipientType::To, Mail::Mailbox(usrMailBox));
|
|
mailMessage.setBody(msg);
|
|
Mail::Client mailClient(smtpDomain);
|
|
mailClient.setSslCertificateVerificationEnabled(false);
|
|
mailClient.enableAuthentication(sourceEMail, pwd, Mail::AuthenticationMethod::Login);
|
|
mailClient.setTransportEncryption(Mail::TransportEncryption::StartTLS);
|
|
if (mailClient.connect(smtpHost, port)) {
|
|
if (mailClient.send(mailMessage))
|
|
errorMsg = "AllOk";
|
|
else
|
|
errorMsg = WString::tr("bad.email.send").arg(usrMailBox).toUTF8();
|
|
else
|
|
errorMsg = WString::tr("bad.host.connect").toUTF8();
|
|
}
|
|
else
|
|
errorMsg = WString::tr("bad.email.attachment").arg(usrMailBox).arg(attachment).toUTF8();
|
|
sourceFile.close();
|
|
return(errorMsg);
|
|
|
|
}
|