Exemple #1
0
QByteArray MBoxPrivate::mboxMessageSeparator( const QByteArray &msg )
{
  KMime::Message mail;
  QByteArray body, header;
  KMime::HeaderParsing::extractHeaderAndBody( KMime::CRLFtoLF( msg ), header, body );
  body.clear();
  mail.setHead( header );
  mail.parse();

  QByteArray separator = "From ";

  KMime::Headers::From *from = mail.from( false );
  if ( !from || from->addresses().isEmpty() ) {
    separator += "*****@*****.**";
  } else {
    separator += from->addresses().first() + ' ';
  }

  KMime::Headers::Date *date = mail.date( false );
  if ( !date || date->isEmpty() ) {
    separator += QDateTime::currentDateTime().toString( Qt::TextDate ).toUtf8() + '\n';
  } else {
    separator += date->as7BitString( false ) + '\n';
  }

  return separator;
}
Exemple #2
0
KMime::Message::Ptr readMimeFile( const QString &fileName )
{
    QFile file( fileName );
    file.open( QFile::ReadOnly );
    const QByteArray data = file.readAll();
    Q_ASSERT( !data.isEmpty() );
    
    KMime::Message *msg = new KMime::Message;
    msg->setContent( data );
    msg->parse();
    return KMime::Message::Ptr(msg);
}
void KOEditorAttachments::addDataAttachment( const QByteArray &data,
                                             const QString &mimeType,
                                             const QString &label )
{
  AttachmentIconItem *item = new AttachmentIconItem( 0, mAttachments );

  QString nlabel = label;
  if ( mimeType == "message/rfc822" ) {
    // mail message. try to set the label from the mail Subject:
    KMime::Message msg;
    msg.setContent( data );
    msg.parse();
    nlabel = msg.subject()->asUnicodeString();
  }

  item->setData( data );
  item->setLabel( nlabel );
  if ( mimeType.isEmpty() ) {
    item->setMimeType( KMimeType::findByContent( data )->name() );
  } else {
    item->setMimeType( mimeType );
  }
}
/******************************************************************************
* Send the email message specified in an event.
* Reply = 1 if the message was sent - 'errmsgs' may contain copy error messages.
*       = 0 if the message is queued for sending.
*       = -1 if the message was not sent - 'errmsgs' contains the error messages.
*/
int KAMail::send(JobData& jobdata, QStringList& errmsgs)
{
    QString err;
    KPIMIdentities::Identity identity;
    if (!jobdata.event.emailFromId())
        jobdata.from = Preferences::emailAddress();
    else
    {
        identity = Identities::identityManager()->identityForUoid(jobdata.event.emailFromId());
        if (identity.isNull())
        {
            kError() << "Identity" << jobdata.event.emailFromId() << "not found";
            errmsgs = errors(i18nc("@info", "Invalid 'From' email address.<nl/>Email identity <resource>%1</resource> not found", jobdata.event.emailFromId()));
            return -1;
        }
        if (identity.emailAddr().isEmpty())
        {
            kError() << "Identity" << identity.identityName() << "uoid" << identity.uoid() << ": no email address";
            errmsgs = errors(i18nc("@info", "Invalid 'From' email address.<nl/>Email identity <resource>%1</resource> has no email address", identity.identityName()));
            return -1;
        }
        jobdata.from = identity.fullEmailAddr();
    }
    if (jobdata.from.isEmpty())
    {
        switch (Preferences::emailFrom())
        {
#ifdef KMAIL_SUPPORTED
        case Preferences::MAIL_FROM_KMAIL:
            errmsgs = errors(i18nc("@info", "<para>No 'From' email address is configured (no default email identity found)</para>"
                                   "<para>Please set it in <application>KMail</application> or in the <application>KAlarm</application> Configuration dialog.</para>"));
            break;
#endif
        case Preferences::MAIL_FROM_SYS_SETTINGS:
            errmsgs = errors(i18nc("@info", "<para>No 'From' email address is configured.</para>"
                                   "<para>Please set it in the KDE System Settings or in the <application>KAlarm</application> Configuration dialog.</para>"));
            break;
        case Preferences::MAIL_FROM_ADDR:
        default:
            errmsgs = errors(i18nc("@info", "<para>No 'From' email address is configured.</para>"
                                   "<para>Please set it in the <application>KAlarm</application> Configuration dialog.</para>"));
            break;
        }
        return -1;
    }
    jobdata.bcc  = (jobdata.event.emailBcc() ? Preferences::emailBccAddress() : QString());
    kDebug() << "To:" << jobdata.event.emailAddresses(",")
             << endl << "Subject:" << jobdata.event.emailSubject();

    MailTransport::TransportManager* manager = MailTransport::TransportManager::self();
    MailTransport::Transport* transport = 0;
    if (Preferences::emailClient() == Preferences::sendmail)
    {
        kDebug() << "Sending via sendmail";
        const QList<MailTransport::Transport*> transports = manager->transports();
        for (int i = 0, count = transports.count();  i < count;  ++i)
        {
            if (transports[i]->type() == MailTransport::Transport::EnumType::Sendmail)
            {
                // Use the first sendmail transport found
                transport = transports[i];
                break;
            }
        }
        if (!transport)
        {
            QString command = KStandardDirs::findExe(QLatin1String("sendmail"),
                              QLatin1String("/sbin:/usr/sbin:/usr/lib"));
            transport = manager->createTransport();
            transport->setName(QLatin1String("sendmail"));
            transport->setType(MailTransport::Transport::EnumType::Sendmail);
            transport->setHost(command);
            transport->setRequiresAuthentication(false);
            transport->setStorePassword(false);
            manager->addTransport(transport);
            transport->writeConfig();
            kDebug() << "Creating sendmail transport, id=" << transport->id();
        }
    }
    else
    {
        kDebug() << "Sending via KDE";
        transport = manager->transportByName(identity.transport(), true);
        if (!transport)
        {
            kError() << "No mail transport found for identity" << identity.identityName() << "uoid" << identity.uoid();
            errmsgs = errors(i18nc("@info", "No mail transport configured for email identity <resource>%1</resource>", identity.identityName()));
            return -1;
        }
    }
    kDebug() << "Using transport" << transport->name() << ", id=" << transport->id();
    MailTransport::TransportJob* mailjob = manager->createTransportJob(transport->id());
    if (!mailjob)
    {
        kError() << "Failed to create mail transport job for identity" << identity.identityName() << "uoid" << identity.uoid();
        errmsgs = errors(i18nc("@info", "Unable to create mail transport job"));
        return -1;
    }
    KMime::Message message;
    initHeaders(message, jobdata);
    err = appendBodyAttachments(message, jobdata);
    if (!err.isNull())
    {
        kError() << "Error compiling message:" << err;
        errmsgs = errors(err);
        return -1;
    }
    mailjob->setSender(KPIMUtils::extractEmailAddress(jobdata.from));
    mailjob->setTo(jobdata.event.emailPureAddresses());
    if (!jobdata.bcc.isEmpty())
        mailjob->setBcc(QStringList(KPIMUtils::extractEmailAddress(jobdata.bcc)));
    mailjob->setData(message.encodedContent());
    mJobs.enqueue(mailjob);
    mJobData.enqueue(jobdata);
    if (mJobs.count() == 1)
    {
        // There are no jobs already active or queued, so send now
        connect(mailjob, SIGNAL(result(KJob*)), instance(), SLOT(slotEmailSent(KJob*)));
        mailjob->start();
    }
    return 0;
}