-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailer.cpp
More file actions
180 lines (146 loc) · 5.36 KB
/
Emailer.cpp
File metadata and controls
180 lines (146 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Emailer.cpp
*
* Created on: 7 March 2012
* Author: Max Foster
*/
#include <iostream>
using namespace std;
#include <QSslSocket>
#include <QFile>
#include <Qxt/QxtSmtp>
#include <Qxt/QxtMailMessage>
#include <Qxt/QxtMailAttachment>
#include "Emailer.h"
#include "Setting.h"
#include "SettingController.h"
#include "dialogs/setting/SettingForm.h"
Emailer::Emailer(const EmailDetails &emailDetails, QObject *parent)
: QObject(parent), socket(NULL), emailDetails_(emailDetails), completed(false), killNow(false),
connectionState(NONE), authenticationState(NONE), mailSendState(NONE) {}
Emailer::~Emailer()
{
// If there is still a connection, close it
if ((connectionState != NONE) && (socket != NULL)) closeConnection();
if (socket != NULL) delete socket;
}
void Emailer::send()
{
mailSendState = PENDING;
// Create a new socket, and set the username and password to the values in the settings
if (socket != NULL) delete socket;
socket = new QxtSmtp(this);
Setting usernameSetting = SettingController::getSetting(SettingForm::keyEmailUsername),
passwordSetting = SettingController::getSetting(SettingForm::keyEmailPassword),
hostSetting = SettingController::getSetting(SettingForm::keyEmailHost),
portSetting = SettingController::getSetting(SettingForm::keyEmailPort);
socket->setUsername(usernameSetting.getValue());
socket->setPassword(passwordSetting.getValue());
// Connect the Qt signals from the socket to the slots in the Emailer
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(connectionFailed(QByteArray)), this, SLOT(connectionFailed(QByteArray)));
connect(socket, SIGNAL(authenticated()), this, SLOT(authenticated()));
connect(socket, SIGNAL(authenticationFailed(QByteArray)), this, SLOT(authenticationFailed(QByteArray)));
connect(socket, SIGNAL(mailSent(int)), this, SLOT(mailSent(int)));
connect(socket, SIGNAL(mailFailed(int,int,QByteArray)), this, SLOT(mailFailed(int,int,QByteArray)));
connect(socket, SIGNAL(senderRejected(int,QString,QByteArray)), this, SLOT(senderRejected(int,QString,QByteArray)));
connect(socket, SIGNAL(finished()), this, SLOT(finished()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
// Connect to the host with the host and port values from the settings
connectionState = PENDING;
socket->connectToSecureHost(hostSetting.getValue(), atoi(portSetting.getValue()));
++emailDetails_.tries;
}
bool Emailer::pending() const
{
return (mailSendState == PENDING);
}
bool Emailer::sentSuccessfully() const
{
return (mailSendState == SUCCESS);
}
const EmailDetails & Emailer::emailDetails() const
{
return emailDetails_;
}
void Emailer::kill()
{
// Set killNow to true, indicating that when the Emailer is done, it can delete itself. If it has finished already,
// then delete it now
killNow = true;
if (completed) delete this;
}
void Emailer::connected()
{
connectionState = SUCCESS;
cout << "Connection successful" << endl;
// Connection successful, so create a new email from the email details
QxtMailMessage message;
message.addRecipient(emailDetails_.recipient.c_str());
message.setSubject(emailDetails_.subject.c_str());
message.setBody(emailDetails_.body.c_str());
// If there is an attachment, open it and put the attachment data into the email
if (emailDetails_.attachmentFileName.size() > 0)
{
QFile attachment(emailDetails_.attachmentFileName.c_str());
attachment.open(QFile::ReadOnly);
message.addAttachment(emailDetails_.attachmentFileName.c_str(), QxtMailAttachment(attachment.readAll()));
attachment.close();
}
// Send the email
socket->send(message);
}
void Emailer::connectionFailed(const QByteArray &message)
{
connectionState = mailSendState = FAILURE;
cout << "Connection failed" << endl
<< QString(message).toStdString() << endl;
}
void Emailer::authenticated()
{
authenticationState = SUCCESS;
cout << "Authentication successful" << endl;
}
void Emailer::authenticationFailed(const QByteArray &message)
{
authenticationState = mailSendState = FAILURE;
cout << "Authentication failed" << endl
<< QString(message).toStdString() << endl;
}
void Emailer::mailSent(int)
{
mailSendState = SUCCESS;
cout << "Mail sent successfully" << endl;
}
void Emailer::mailFailed(int, int, const QByteArray &message)
{
mailSendState = FAILURE;
cout << "Mail could not be sent" << endl
<< QString(message).toStdString() << endl;
}
void Emailer::senderRejected(int, const QString &address, const QByteArray &message)
{
mailSendState = FAILURE;
cout << "Sender rejected" << endl
<< address.toStdString() << endl
<< QString(message).toStdString() << endl;
}
void Emailer::finished()
{
closeConnection();
cout << "Finished" << endl;
}
void Emailer::disconnected()
{
connectionState = authenticationState = NONE;
completed = true;
cout << "Connection closed" << endl;
// Send the relevant signal depending on whether the email sent successfully or not
if (mailSendState == FAILURE) emit mailFailed();
else if (mailSendState == SUCCESS) emit mailSent();
if (killNow) delete this;
}
void Emailer::closeConnection()
{
if (socket != NULL) socket->disconnectFromHost();
}