Also move context menu logic into separate classes
[TortoiseGit.git] / src / TortoiseProc / SendMail.cpp
blob8a83cb082bb5ff755896ba98efaba6d2f1c39d7d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - TortoiseGit
4 // Copyright (C) 2011-2013 Sven Strickroth <email@cs-ware.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "stdafx.h"
22 #include "SendMail.h"
23 #include "HwSMTP.h"
24 #include "MailMsg.h"
25 #include "Git.h"
27 class CAppUtils;
29 CSendMail::CSendMail(CString &To, CString &CC, bool bAttachment)
31 m_sSenderName = g_Git.GetUserName();
32 m_sSenderMail = g_Git.GetUserEmail();
33 m_sTo = To;
34 m_sCC = CC;
35 m_bAttachment = bAttachment;
38 CSendMail::~CSendMail(void)
42 int CSendMail::SendMail(const CTGitPath &item, CGitProgressList * instance, CString &FromName, CString &FromMail, CString &To, CString &CC, CString &subject, CString &body, CStringArray &attachments)
44 ASSERT(instance);
45 int retry = 0;
46 while (retry < 3)
48 if (instance->IsCancelled() == TRUE)
50 instance->ReportUserCanceled();
51 return -1;
54 instance->AddNotify(new CGitProgressList::NotificationData(item, IDS_SVNACTION_SENDMAIL_START), CColors::Modified);
56 CString error;
57 if (SendMail(FromName, FromMail, To, CC, subject, body, attachments, &error) == 0)
58 return 0;
60 instance->ReportError(error);
62 if (instance->IsCancelled() == FALSE) // do not retry/sleep if the user already canceled
64 ++retry;
65 if (retry < 3)
67 CString retry;
68 retry.LoadString(IDS_SVNACTION_SENDMAIL_RETRY);
69 instance->ReportNotification(retry);
70 Sleep(2000);
74 return -1;
77 int CSendMail::SendMail(CString &FromName, CString &FromMail, CString &To, CString &CC, CString &subject, CString &body, CStringArray &attachments, CString *errortext)
79 if (CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\DeliveryType"), 1) == 1)
81 CMailMsg mapiSender;
82 BOOL bMAPIInit = mapiSender.MAPIInitialize();
83 if (!bMAPIInit)
85 if (errortext)
86 *errortext = mapiSender.GetLastErrorMsg();
87 return -1;
90 mapiSender.SetShowComposeDialog(TRUE);
91 mapiSender.SetFrom(FromName);
92 mapiSender.SetTo(To);
93 if (!CC.IsEmpty())
94 mapiSender.AddCC(CC);
95 mapiSender.SetSubject(subject);
96 mapiSender.SetMessage(body);
97 for (int i = 0; i < attachments.GetSize(); ++i)
98 mapiSender.AddAttachment(attachments[i]);
100 BOOL bSend = mapiSender.Send();
101 if (bSend == TRUE)
102 return 0;
103 else
105 if (errortext)
106 *errortext = mapiSender.GetLastErrorMsg();
107 return -1;
110 else
112 CString sender;
113 sender.Format(_T("%s <%s>"), FromName, FromMail);
115 CHwSMTP mail;
116 if (CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\DeliveryType"), 2) == 2)
118 CString recipients(To);
119 if (!CC.IsEmpty())
120 recipients += L";" + CC;
121 if (mail.SendEmail((CString)CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\Address"), _T("")), (CString)CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\Username"), _T("")), (CString)CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\Password"), _T("")), (BOOL)CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\AuthenticationRequired"), FALSE), sender, recipients, subject, body, nullptr, &attachments, CC, (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\Port"), 25), To, sender, (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\SendMail\\Encryption"), 0)) == TRUE)
122 return 0;
123 else
125 if (errortext)
126 *errortext = mail.GetLastErrorText();
127 return -1;
130 else if (mail.SendSpeedEmail(sender, To, subject, body, nullptr, &attachments, CC, sender))
131 return 0;
132 else
134 if (errortext)
135 *errortext = mail.GetLastErrorText();
136 return -1;
141 CSendMailCombineable::CSendMailCombineable(CString &To, CString &CC, CString &subject, bool bAttachment, bool bCombine)
142 : CSendMail(To, CC, bAttachment)
143 , m_sSubject(subject)
144 , m_bCombine(bCombine)
148 CSendMailCombineable::~CSendMailCombineable()
152 int CSendMailCombineable::Send(CTGitPathList &list, CGitProgressList * instance)
154 if (m_bCombine)
156 return SendAsCombinedMail(list, instance);
158 else
160 instance->SetItemCountTotal(list.GetCount() + 1);
161 for (int i = 0; i < list.GetCount(); ++i)
163 instance->SetItemProgress(i);
164 if (SendAsSingleMail((CTGitPath &)list[i], instance))
165 return -1;
167 instance->SetItemProgress(list.GetCount() + 1);
170 return 0;
173 int GetFileContents(CString &filename, CString &content)
175 CStdioFile file;
176 if (file.Open(filename, CFile::modeRead))
178 CString str;
179 while (file.ReadString(str))
181 content += str;
182 str.Empty();
183 content += _T("\n");
185 return 0;
187 else
188 return -1;
191 int CSendMailCombineable::SendAsSingleMail(CTGitPath &path, CGitProgressList * instance)
193 ASSERT(instance);
195 CString pathfile(path.GetWinPathString());
197 CString body;
198 CStringArray attachments;
199 if (m_bAttachment)
200 attachments.Add(pathfile);
201 else if (GetFileContents(pathfile, body))
203 instance->ReportError(_T("Could not open ") + pathfile);
204 return -2;
207 return SendMail(path, instance, m_sSenderName, m_sSenderMail, m_sTo, m_sCC, m_sSubject, body, attachments);
210 int CSendMailCombineable::SendAsCombinedMail(CTGitPathList &list, CGitProgressList * instance)
212 ASSERT(instance);
214 CStringArray attachments;
215 CString body;
216 for (int i = 0; i < list.GetCount(); ++i)
218 if (m_bAttachment)
220 attachments.Add(list[i].GetWinPathString());
222 else
224 CString filename(list[i].GetWinPathString());
225 body += filename + _T(":\n");
226 if (GetFileContents(filename, body))
228 instance->ReportError(_T("Could not open ") + filename);
229 return -2;
231 body += _T("\n");
234 return SendMail(CTGitPath(), instance, m_sSenderName, m_sSenderMail, m_sTo, m_sCC, m_sSubject, body, attachments);