25 #include <config-enterprise.h>
28 #include "kdepim-version.h"
30 #include <Akonadi/Collection>
32 #include <KCalCore/Attendee>
33 #include <KCalCore/Incidence>
34 #include <KCalCore/IncidenceBase>
36 #include <KCalUtils/IncidenceFormatter>
38 #include <KMime/Message>
40 #include <KPIMIdentities/Identity>
42 #include <KPIMUtils/Email>
44 #include <Mailtransport/MessageQueueJob>
45 #include <Mailtransport/Transport>
46 #include <Mailtransport/TransportManager>
50 #include <KProtocolManager>
51 #include <KSystemTimeZone>
64 const KPIMIdentities::Identity &identity,
65 bool bccMe,
const QString &attachment,
66 const QString &mailTransport )
68 KCalCore::Attendee::List attendees = incidence->attendees();
69 if ( attendees.isEmpty() ) {
70 kWarning() <<
"There are no attendees to e-mail";
74 const QString from = incidence->organizer()->fullName();
75 const QString organizerEmail = incidence->organizer()->email();
79 const int numberOfAttendees( attendees.count() );
80 for (
int i=0; i<numberOfAttendees; ++i ) {
81 KCalCore::Attendee::Ptr a = attendees.at(i);
83 const QString email = a->email();
84 if ( email.isEmpty() ) {
91 if ( organizerEmail == email ) {
96 QString tname, temail;
97 const QString username = KPIMUtils::quoteNameIfNecessary( a->name() );
100 KPIMUtils::extractEmailAddressAndName( username, temail, tname );
101 tname += QLatin1String(
" <" ) + email + QLatin1Char(
'>' );
104 if ( a->role() == KCalCore::Attendee::OptParticipant ||
105 a->role() == KCalCore::Attendee::NonParticipant ) {
111 if( toList.isEmpty() && ccList.isEmpty() ) {
113 kWarning() <<
"There are really no attendees to e-mail";
117 if ( !toList.isEmpty() ) {
118 to = toList.join( QLatin1String(
", " ) );
121 if ( !ccList.isEmpty() ) {
122 cc = ccList.join( QLatin1String(
", " ) );
126 if ( incidence->type() != KCalCore::Incidence::TypeFreeBusy ) {
127 KCalCore::Incidence::Ptr inc = incidence.staticCast<KCalCore::Incidence>();
128 subject = inc->summary();
130 subject = i18n(
"Free Busy Object" );
134 KCalUtils::IncidenceFormatter::mailBodyStr( incidence, KSystemTimeZones::local() );
136 return send( identity, from, to, cc, subject, body,
false,
137 bccMe, attachment, mailTransport );
141 const KPIMIdentities::Identity &identity,
142 const QString &from,
bool bccMe,
143 const QString &attachment,
144 const QString &sub,
const QString &mailTransport )
146 const QString to = incidence->organizer()->fullName();
147 QString subject = sub;
149 if ( incidence->type() != KCalCore::Incidence::TypeFreeBusy ) {
150 KCalCore::Incidence::Ptr inc = incidence.staticCast<KCalCore::Incidence>();
151 if ( subject.isEmpty() ) {
152 subject = inc->summary();
155 subject = i18n(
"Free Busy Message" );
158 QString body = KCalUtils::IncidenceFormatter::mailBodyStr( incidence, KSystemTimeZones::local() );
160 return send( identity, from, to, QString(), subject, body,
false,
161 bccMe, attachment, mailTransport );
165 const KPIMIdentities::Identity &identity,
166 const QString &from,
bool bccMe,
167 const QString &recipients,
const QString &attachment,
168 const QString &mailTransport )
172 if ( incidence->type() != KCalCore::Incidence::TypeFreeBusy ) {
173 KCalCore::Incidence::Ptr inc = incidence.staticCast<KCalCore::Incidence>() ;
174 subject = inc->summary();
176 subject = i18n(
"Free Busy Message" );
179 KCalUtils::IncidenceFormatter::mailBodyStr( incidence, KSystemTimeZones::local() );
181 return send( identity, from, recipients, QString(), subject, body,
false,
182 bccMe, attachment, mailTransport );
187 const QStringList splittedEmail = KPIMUtils::splitAddressList( email );
188 QStringList normalizedEmail;
189 Q_FOREACH(
const QString& email, splittedEmail )
191 const QString str = KPIMUtils::extractEmailAddress( KPIMUtils::normalizeAddressesAndEncodeIdn( email ) );
192 normalizedEmail << str;
194 return normalizedEmail;
198 const QString &from,
const QString &_to,
199 const QString &cc,
const QString &subject,
200 const QString &body,
bool hidden,
bool bccMe,
201 const QString &attachment,
const QString &mailTransport )
205 if ( !MailTransport::TransportManager::self()->showTransportCreationDialog(
206 0, MailTransport::TransportManager::IfNoTransportExists ) ) {
213 if ( to.isEmpty() ) {
216 kDebug() <<
"\nFrom:" << from
219 <<
"\nSubject:" << subject <<
"\nBody: \n" << body
220 <<
"\nAttachment:\n" << attachment
221 <<
"\nmailTransport: " << mailTransport;
226 MailTransport::Transport *transport =
227 MailTransport::TransportManager::self()->transportByName( mailTransport );
231 MailTransport::TransportManager::self()->transportByName(
232 MailTransport::TransportManager::self()->defaultTransportName() );
238 kError() <<
"Error fetching transport; mailTransport"
239 << mailTransport << MailTransport::TransportManager::self()->defaultTransportName();
243 const int transportId = transport->id();
246 KConfig config( QLatin1String(
"kmail2rc") );
248 KConfigGroup configGroup( &config, QLatin1String(
"Invitations" ) );
249 const bool outlookConformInvitation = configGroup.readEntry(
"LegacyBodyInvites",
250 #ifdef KDEPIM_ENTERPRISE_BUILD
260 KMime::Message::Ptr message = KMime::Message::Ptr(
new KMime::Message );
261 message->contentTransferEncoding()->clear();
264 message->userAgent()->fromUnicodeString(
265 KProtocolManager::userAgentForApplication(
266 QLatin1String(
"KOrganizer" ), QLatin1String( KDEPIM_VERSION ) ),
"utf-8" );
267 message->from()->fromUnicodeString( from,
"utf-8" );
268 message->to()->fromUnicodeString( to,
"utf-8" );
269 message->cc()->fromUnicodeString( cc,
"utf-8" );
271 message->bcc()->fromUnicodeString( from,
"utf-8" );
273 message->date()->setDateTime( KDateTime::currentLocalDateTime() );
274 message->subject()->fromUnicodeString( subject,
"utf-8" );
276 if ( outlookConformInvitation ) {
277 message->contentType()->setMimeType(
"text/calendar" );
278 message->contentType()->setCharset(
"utf-8" );
279 message->contentType()->setName( QLatin1String(
"cal.ics" ),
"utf-8" );
280 message->contentType()->setParameter( QLatin1String(
"method" ), QLatin1String(
"request" ) );
282 if ( !attachment.isEmpty() ) {
283 KMime::Headers::ContentDisposition *disposition =
284 new KMime::Headers::ContentDisposition( message.get() );
285 disposition->setDisposition( KMime::Headers::CDinline );
286 message->setHeader( disposition );
287 message->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
288 message->setBody( KMime::CRLFtoLF( attachment.toUtf8() ) );
295 KMime::Headers::ContentType *ct = message->contentType();
296 ct->setMimeType(
"multipart/mixed" );
297 ct->setBoundary( KMime::multiPartBoundary() );
298 ct->setCategory( KMime::Headers::CCcontainer );
301 KMime::Content *bodyMessage =
new KMime::Content;
302 KMime::Headers::ContentDisposition *bodyDisposition =
303 new KMime::Headers::ContentDisposition( bodyMessage );
304 bodyDisposition->setDisposition( KMime::Headers::CDinline );
305 bodyMessage->contentType()->setMimeType(
"text/plain" );
306 bodyMessage->contentType()->setCharset(
"utf-8" );
307 bodyMessage->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
308 bodyMessage->setBody( KMime::CRLFtoLF( body.toUtf8() ) );
309 message->addContent( bodyMessage );
312 if ( !attachment.isEmpty() ) {
313 KMime::Content *attachMessage =
new KMime::Content;
314 KMime::Headers::ContentDisposition *attachDisposition =
315 new KMime::Headers::ContentDisposition( attachMessage );
316 attachDisposition->setDisposition( KMime::Headers::CDattachment );
317 attachMessage->contentType()->setMimeType(
"text/calendar" );
318 attachMessage->contentType()->setCharset(
"utf-8" );
319 attachMessage->contentType()->setName( QLatin1String(
"cal.ics" ),
"utf-8" );
320 attachMessage->contentType()->setParameter( QLatin1String(
"method" ),
321 QLatin1String(
"request" ) );
322 attachMessage->setHeader( attachDisposition );
323 attachMessage->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
324 attachMessage->setBody( KMime::CRLFtoLF( attachment.toUtf8() ) );
325 message->addContent( attachMessage );
333 MailTransport::MessageQueueJob *qjob =
new MailTransport::MessageQueueJob(
this );
334 qjob->transportAttribute().setTransportId( transportId );
336 if (identity.disabledFcc()) {
337 qjob->sentBehaviourAttribute().setSentBehaviour( MailTransport::SentBehaviourAttribute::Delete );
339 const Akonadi::Collection sentCollection( identity.fcc().toLongLong() );
340 if (sentCollection.isValid()) {
341 qjob->sentBehaviourAttribute().setSentBehaviour( MailTransport::SentBehaviourAttribute::MoveToCollection );
342 qjob->sentBehaviourAttribute().setMoveToCollection( sentCollection );
344 qjob->sentBehaviourAttribute().setSentBehaviour(
345 MailTransport::SentBehaviourAttribute::MoveToDefaultSentCollection );
349 if ( transport && transport->specifySenderOverwriteAddress() ) {
350 qjob->addressAttribute().setFrom(
351 KPIMUtils::extractEmailAddress(
352 KPIMUtils::normalizeAddressesAndEncodeIdn( transport->senderOverwriteAddress() ) ) );
354 qjob->addressAttribute().setFrom(
355 KPIMUtils::extractEmailAddress(
356 KPIMUtils::normalizeAddressesAndEncodeIdn( from ) ) );
359 if( !to.isEmpty() ) {
362 if( !cc.isEmpty() ) {
368 qjob->setMessage( message );
369 if ( !qjob->exec() ) {
370 kWarning() <<
"Error queuing message in outbox:" << qjob->errorText();
375 kDebug() <<
"Send mail finished. Time elapsed in ms:" << timer.elapsed();
bool mailOrganizer(const KCalCore::IncidenceBase::Ptr &, const KPIMIdentities::Identity &identity, const QString &from, bool bccMe, const QString &attachment=QString(), const QString &sub=QString(), const QString &mailTransport=QString())
bool send(const KPIMIdentities::Identity &identity, const QString &from, const QString &to, const QString &cc, const QString &subject, const QString &body, bool hidden=false, bool bccMe=false, const QString &attachment=QString(), const QString &mailTransport=QString())
Sends mail with specified from, to and subject field and body as text.
bool mailAttendees(const KCalCore::IncidenceBase::Ptr &, const KPIMIdentities::Identity &identity, bool bccMe, const QString &attachment=QString(), const QString &mailTransport=QString())
bool mailTo(const KCalCore::IncidenceBase::Ptr &, const KPIMIdentities::Identity &identity, const QString &from, bool bccMe, const QString &recipients, const QString &attachment=QString(), const QString &mailTransport=QString())
QStringList extractEmailAndNormalize(const QString &email)