Add Daemon Shell Context Menu
[TortoiseGit.git] / src / TortoiseProc / SendMail.cpp
blobfe66a97095c12c712e227f14f9ef7e7ddad1897e
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, bool useMAPI)
31 m_sSenderName = g_Git.GetUserName();
32 m_sSenderMail = g_Git.GetUserEmail();
33 m_sTo = To;
34 m_sCC = CC;
35 m_bUseMAPI = useMAPI;
36 m_bAttachment = bAttachment;
39 CSendMail::~CSendMail(void)
43 int CSendMail::SendMail(const CTGitPath &item, CGitProgressList * instance, CString &FromName, CString &FromMail, CString &To, CString &CC, CString &subject, CString &body, CStringArray &attachments, bool useMAPI)
45 ASSERT(instance);
46 int retry = 0;
47 while (retry < 3)
49 if (instance->IsCancelled() == TRUE)
51 instance->ReportUserCanceled();
52 return -1;
55 instance->Notify(item, git_wc_notify_sendmail);
57 CString error;
58 if (SendMail(FromName, FromMail, To, CC, subject, body, attachments, useMAPI, &error) == 0)
59 return 0;
61 instance->ReportError(error);
63 if (instance->IsCancelled() == FALSE) // do not retry/sleep if the user already canceled
65 ++retry;
66 if (retry < 3)
68 CString retry;
69 retry.LoadString(IDS_SVNACTION_SENDMAIL_RETRY);
70 instance->ReportNotification(retry);
71 Sleep(2000);
75 return -1;
78 int CSendMail::SendMail(CString &FromName, CString &FromMail, CString &To, CString &CC, CString &subject, CString &body, CStringArray &attachments, bool useMAPI, CString *errortext)
80 if (useMAPI)
82 CMailMsg mapiSender;
83 BOOL bMAPIInit = mapiSender.MAPIInitialize();
84 if (!bMAPIInit)
86 if (errortext)
87 *errortext = mapiSender.GetLastErrorMsg();
88 return -1;
91 mapiSender.SetShowComposeDialog(TRUE);
92 mapiSender.SetFrom(FromName);
93 mapiSender.SetTo(To);
94 if (!CC.IsEmpty())
95 mapiSender.AddCC(CC);
96 mapiSender.SetSubject(subject);
97 mapiSender.SetMessage(body);
98 for (int i = 0; i < attachments.GetSize(); ++i)
99 mapiSender.AddAttachment(attachments[i]);
101 BOOL bSend = mapiSender.Send();
102 if (bSend == TRUE)
103 return 0;
104 else
106 if (errortext)
107 *errortext = mapiSender.GetLastErrorMsg();
108 return -1;
111 else
113 CString sender;
114 sender.Format(_T("%s <%s>"), FromName, FromMail);
116 CHwSMTP mail;
117 if (mail.SendSpeedEmail(sender, To, subject, body, NULL, &attachments, CC, 25, sender))
118 return 0;
119 else
121 if (errortext)
122 *errortext = mail.GetLastErrorText();
123 return -1;
128 CSendMailCombineable::CSendMailCombineable(CString &To, CString &CC, CString &subject, bool bAttachment, bool bCombine, bool useMAPI)
129 : CSendMail(To, CC, bAttachment, useMAPI)
130 , m_sSubject(subject)
131 , m_bCombine(bCombine)
135 CSendMailCombineable::~CSendMailCombineable()
139 int CSendMailCombineable::Send(CTGitPathList &list, CGitProgressList * instance)
141 if (m_bCombine)
143 return SendAsCombinedMail(list, instance);
145 else
147 instance->SetItemCountTotal(list.GetCount() + 1);
148 for (int i = 0; i < list.GetCount(); ++i)
150 instance->SetItemProgress(i);
151 if (SendAsSingleMail((CTGitPath &)list[i], instance))
152 return -1;
154 instance->SetItemProgress(list.GetCount() + 1);
157 return 0;
160 int GetFileContents(CString &filename, CString &content)
162 CStdioFile file;
163 if (file.Open(filename, CFile::modeRead))
165 CString str;
166 while (file.ReadString(str))
168 content += str;
169 str.Empty();
170 content += _T("\n");
172 return 0;
174 else
175 return -1;
178 int CSendMailCombineable::SendAsSingleMail(CTGitPath &path, CGitProgressList * instance)
180 ASSERT(instance);
182 CString pathfile(path.GetWinPathString());
184 CString body;
185 CStringArray attachments;
186 if (m_bAttachment)
187 attachments.Add(pathfile);
188 else if (GetFileContents(pathfile, body))
190 instance->ReportError(_T("Could not open ") + pathfile);
191 return -2;
194 return SendMail(path, instance, m_sSenderName, m_sSenderMail, m_sTo, m_sCC, m_sSubject, body, attachments, m_bUseMAPI);
197 int CSendMailCombineable::SendAsCombinedMail(CTGitPathList &list, CGitProgressList * instance)
199 ASSERT(instance);
201 CStringArray attachments;
202 CString body;
203 for (int i = 0; i < list.GetCount(); ++i)
205 if (m_bAttachment)
207 attachments.Add(list[i].GetWinPathString());
209 else
211 CString filename(list[i].GetWinPathString());
212 body += filename + _T(":\n");
213 if (GetFileContents(filename, body))
215 instance->ReportError(_T("Could not open ") + filename);
216 return -2;
218 body += _T("\n");
221 return SendMail(CTGitPath(), instance, m_sSenderName, m_sSenderMail, m_sTo, m_sCC, m_sSubject, body, attachments, m_bUseMAPI);