1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013, 2015-2016 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "WindowsCredentialsStore.h"
29 CSendMail::CSendMail(const CString
& To
, const CString
& CC
, bool bAttachment
)
30 : m_sSenderName(g_Git
.GetUserName())
31 , m_sSenderMail(g_Git
.GetUserEmail())
34 , m_bAttachment(bAttachment
)
38 CSendMail::~CSendMail(void)
42 int CSendMail::SendMail(const CTGitPath
&item
, CGitProgressList
* instance
, const CString
& FromName
, const CString
& FromMail
, const CString
& To
, const CString
& CC
, const CString
&subject
, const CString
& body
, CStringArray
&attachments
)
48 if (instance
->IsCancelled() == TRUE
)
50 instance
->ReportUserCanceled();
54 instance
->AddNotify(new CGitProgressList::NotificationData(item
, IDS_SVNACTION_SENDMAIL_START
), CColors::Modified
);
57 if (SendMail(FromName
, FromMail
, To
, CC
, subject
, body
, attachments
, &error
) == 0)
60 instance
->ReportError(error
);
62 if (instance
->IsCancelled() == FALSE
) // do not retry/sleep if the user already canceled
68 temp
.LoadString(IDS_SVNACTION_SENDMAIL_RETRY
);
69 instance
->ReportNotification(temp
);
77 int CSendMail::SendMail(const CString
& FromName
, const CString
& FromMail
, const CString
& To
, const CString
& CC
, const CString
& subject
, const CString
& body
, CStringArray
&attachments
, CString
*errortext
)
79 if (CRegDWORD(L
"Software\\TortoiseGit\\TortoiseProc\\SendMail\\DeliveryType", SEND_MAIL_MAPI
) == SEND_MAIL_MAPI
)
82 BOOL bMAPIInit
= mapiSender
.MAPIInitialize();
86 *errortext
= mapiSender
.GetLastErrorMsg();
90 mapiSender
.SetShowComposeDialog(TRUE
);
91 mapiSender
.SetFrom(FromMail
, FromName
);
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();
106 *errortext
= mapiSender
.GetLastErrorMsg();
113 sender
.Format(L
"%s <%s>", (LPCTSTR
)CHwSMTP::GetEncodedHeader(FromName
), (LPCTSTR
)FromMail
);
116 if (CRegDWORD(L
"Software\\TortoiseGit\\TortoiseProc\\SendMail\\DeliveryType", SEND_MAIL_SMTP_CONFIGURED
) == SEND_MAIL_SMTP_CONFIGURED
)
118 CString
recipients(To
);
120 recipients
+= L
";" + CC
;
121 CString username
, password
;
122 CWindowsCredentialsStore::GetCredential(L
"TortoiseGit:SMTP-Credentials", username
, password
);
123 if (mail
.SendEmail((CString
)CRegString(L
"Software\\TortoiseGit\\TortoiseProc\\SendMail\\Address", L
""), username
, password
, (BOOL
)CRegDWORD(L
"Software\\TortoiseGit\\TortoiseProc\\SendMail\\AuthenticationRequired", FALSE
), sender
, recipients
, subject
, body
, nullptr, &attachments
, CC
, (DWORD
)CRegDWORD(L
"Software\\TortoiseGit\\TortoiseProc\\SendMail\\Port", 25), sender
, To
, (DWORD
)CRegDWORD(L
"Software\\TortoiseGit\\TortoiseProc\\SendMail\\Encryption", 0)) == TRUE
)
128 *errortext
= mail
.GetLastErrorText();
132 else if (mail
.SendSpeedEmail(sender
, To
, subject
, body
, nullptr, &attachments
, CC
, sender
))
137 *errortext
= mail
.GetLastErrorText();
143 CSendMailCombineable::CSendMailCombineable(const CString
& To
, const CString
& CC
, const CString
& subject
, bool bAttachment
, bool bCombine
)
144 : CSendMail(To
, CC
, bAttachment
)
145 , m_sSubject(subject
)
146 , m_bCombine(bCombine
)
150 CSendMailCombineable::~CSendMailCombineable()
154 int CSendMailCombineable::Send(const CTGitPathList
& list
, CGitProgressList
* instance
)
157 return SendAsCombinedMail(list
, instance
);
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
))
167 instance
->SetItemProgress(list
.GetCount() + 1);
173 int GetFileContents(CString
&filename
, CString
&content
)
176 if (file
.Open(filename
, CFile::modeRead
))
179 while (file
.ReadString(str
))
191 int CSendMailCombineable::SendAsSingleMail(const CTGitPath
& path
, CGitProgressList
* instance
)
195 CString
pathfile(path
.GetWinPathString());
198 CStringArray attachments
;
200 attachments
.Add(pathfile
);
201 else if (GetFileContents(pathfile
, body
))
203 instance
->ReportError(L
"Could not open " + pathfile
);
207 return SendMail(path
, instance
, m_sSenderName
, m_sSenderMail
, m_sTo
, m_sCC
, m_sSubject
, body
, attachments
);
210 int CSendMailCombineable::SendAsCombinedMail(const CTGitPathList
&list
, CGitProgressList
* instance
)
214 CStringArray attachments
;
216 for (int i
= 0; i
< list
.GetCount(); ++i
)
219 attachments
.Add(list
[i
].GetWinPathString());
222 CString
filename(list
[i
].GetWinPathString());
223 body
+= filename
+ L
":\n";
224 if (GetFileContents(filename
, body
))
226 instance
->ReportError(L
"Could not open " + filename
);
232 return SendMail(CTGitPath(), instance
, m_sSenderName
, m_sSenderMail
, m_sTo
, m_sCC
, m_sSubject
, body
, attachments
);