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,
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();
91 if ( organizerEmail == email ) {
97 const QString username = KPIMUtils::quoteNameIfNecessary( a->name() );
100 KPIMUtils::extractEmailAddressAndName( username, temail, tname );
104 if ( a->role() == KCalCore::Attendee::OptParticipant ||
105 a->role() == KCalCore::Attendee::NonParticipant ) {
113 kWarning() <<
"There are really no attendees to e-mail";
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,
146 const QString to = incidence->organizer()->fullName();
149 if ( incidence->type() != KCalCore::Incidence::TypeFreeBusy ) {
150 KCalCore::Incidence::Ptr inc = incidence.staticCast<KCalCore::Incidence>();
152 subject = inc->summary();
155 subject = i18n(
"Free Busy Message" );
158 const 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,
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 );
189 Q_FOREACH(
const QString& email, splittedEmail )
191 const QString str = KPIMUtils::extractEmailAddress( KPIMUtils::normalizeAddressesAndEncodeIdn( email ) );
192 normalizedEmail << str;
194 return normalizedEmail;
200 const QString &body,
bool hidden,
bool bccMe,
205 if ( !MailTransport::TransportManager::self()->showTransportCreationDialog(
206 0, MailTransport::TransportManager::IfNoTransportExists ) ) {
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 );
230 transport = MailTransport::TransportManager::self()->transportById(MailTransport::TransportManager::self()->defaultTransportId(),
false);
236 kError() <<
"Error fetching transport; mailTransport"
237 << mailTransport << MailTransport::TransportManager::self()->defaultTransportName();
241 const int transportId = transport->id();
246 KConfigGroup configGroup( &config,
QLatin1String(
"Invitations" ) );
247 const bool outlookConformInvitation = configGroup.readEntry(
"LegacyBodyInvites",
248 #ifdef KDEPIM_ENTERPRISE_BUILD
258 KMime::Message::Ptr message = KMime::Message::Ptr(
new KMime::Message );
259 message->contentTransferEncoding()->clear();
262 message->userAgent()->fromUnicodeString(
263 KProtocolManager::userAgentForApplication(
265 message->from()->fromUnicodeString( from,
"utf-8" );
266 message->to()->fromUnicodeString( to,
"utf-8" );
267 message->cc()->fromUnicodeString( cc,
"utf-8" );
269 message->bcc()->fromUnicodeString( from,
"utf-8" );
271 message->date()->setDateTime( KDateTime::currentLocalDateTime() );
272 message->subject()->fromUnicodeString( subject,
"utf-8" );
274 if ( outlookConformInvitation ) {
275 message->contentType()->setMimeType(
"text/calendar" );
276 message->contentType()->setCharset(
"utf-8" );
277 message->contentType()->setName(
QLatin1String(
"cal.ics" ),
"utf-8" );
281 KMime::Headers::ContentDisposition *disposition =
282 new KMime::Headers::ContentDisposition( message.get() );
283 disposition->setDisposition( KMime::Headers::CDinline );
284 message->setHeader( disposition );
285 message->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
286 message->setBody( KMime::CRLFtoLF( attachment.
toUtf8() ) );
293 KMime::Headers::ContentType *ct = message->contentType();
294 ct->setMimeType(
"multipart/mixed" );
295 ct->setBoundary( KMime::multiPartBoundary() );
296 ct->setCategory( KMime::Headers::CCcontainer );
299 KMime::Content *bodyMessage =
new KMime::Content;
300 KMime::Headers::ContentDisposition *bodyDisposition =
301 new KMime::Headers::ContentDisposition( bodyMessage );
302 bodyDisposition->setDisposition( KMime::Headers::CDinline );
303 bodyMessage->contentType()->setMimeType(
"text/plain" );
304 bodyMessage->contentType()->setCharset(
"utf-8" );
305 bodyMessage->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
306 bodyMessage->setBody( KMime::CRLFtoLF( body.
toUtf8() ) );
307 message->addContent( bodyMessage );
311 KMime::Content *attachMessage =
new KMime::Content;
312 KMime::Headers::ContentDisposition *attachDisposition =
313 new KMime::Headers::ContentDisposition( attachMessage );
314 attachDisposition->setDisposition( KMime::Headers::CDattachment );
315 attachMessage->contentType()->setMimeType(
"text/calendar" );
316 attachMessage->contentType()->setCharset(
"utf-8" );
317 attachMessage->contentType()->setName(
QLatin1String(
"cal.ics" ),
"utf-8" );
318 attachMessage->contentType()->setParameter(
QLatin1String(
"method" ),
320 attachMessage->setHeader( attachDisposition );
321 attachMessage->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
322 attachMessage->setBody( KMime::CRLFtoLF( attachment.
toUtf8() ) );
323 message->addContent( attachMessage );
331 MailTransport::MessageQueueJob *qjob =
new MailTransport::MessageQueueJob(
this );
332 qjob->transportAttribute().setTransportId( transportId );
334 if (identity.disabledFcc()) {
335 qjob->sentBehaviourAttribute().setSentBehaviour( MailTransport::SentBehaviourAttribute::Delete );
337 const Akonadi::Collection sentCollection( identity.fcc().toLongLong() );
338 if (sentCollection.isValid()) {
339 qjob->sentBehaviourAttribute().setSentBehaviour( MailTransport::SentBehaviourAttribute::MoveToCollection );
340 qjob->sentBehaviourAttribute().setMoveToCollection( sentCollection );
342 qjob->sentBehaviourAttribute().setSentBehaviour(
343 MailTransport::SentBehaviourAttribute::MoveToDefaultSentCollection );
347 if ( transport && transport->specifySenderOverwriteAddress() ) {
348 qjob->addressAttribute().setFrom(
349 KPIMUtils::extractEmailAddress(
350 KPIMUtils::normalizeAddressesAndEncodeIdn( transport->senderOverwriteAddress() ) ) );
352 qjob->addressAttribute().setFrom(
353 KPIMUtils::extractEmailAddress(
354 KPIMUtils::normalizeAddressesAndEncodeIdn( from ) ) );
366 qjob->setMessage( message );
367 if ( !qjob->exec() ) {
368 kWarning() <<
"Error queuing message in outbox:" << qjob->errorText();
373 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())
const T & at(int i) const
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.
QString join(const QString &separator) const
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)
QByteArray toUtf8() const