Multipart / pgp-mime with ruby and TMail

Ever wanted to create gpg-encrypted standard-conform multipart-mime messages with TMail ?

here is how it works

TMail – multipart/gpg-encrypted


 def multipart_tmail_object(content,subject,recipient)

    if content.kind_of?(File)
      filename = File.basename(content.path)
      body = content.readlines(filename)
    elsif content.kind_of?(String)
      filename = "msg.asc"
      body = content
    else
      raise " has to be File or String"
    end

    puts " *create the base mail-container* "
    container = TMail::Mail.new
    container.date = Time.now
    container.subject = subject
    container.to = recipient
    container.set_content_type('mulitpart','encrypted',
                          'protocol' => "application/pgp-encrypted")
    container.mime_version = '1.0'
    container['User-Agent'] = "Pooble-GPGMailer"
    container.set_content_disposition('inline')
    container.body = ""
    
    puts " *create another Mail-object  that holds gpg-attachment* "
    gpg_container = TMail::Mail.new
    gpg_container.body = "Version: 1\n"
    gpg_container.set_content_type('application','pgp-encrypted')
    gpg_container.set_content_disposition('attachment')
    
    container.parts.push(gpg_container)
    
    puts  " *finally create the gpg-attachment* "
    att = TMail::Mail.new
    att.body = body
    att.set_content_type('application','octet-stream')
    att.set_content_disposition('inline',
                          'filename' => filename)
    
    container.parts.push(att)
    
    return container
  end

Kategorie: Techniktipps / DIY
Tags: , ,