### Command-Line Batch Conversion with mapitool Source: https://context7.com/mysociety/ruby-msg/llms.txt Provides examples of using the `mapitool` command-line utility for converting MSG and PST files to standard formats. It covers single file conversion, batch conversion of multiple MSG files, PST to individual EML files, and PST conversion with path filtering. This tool offers a convenient way to process files without direct Ruby scripting. ```bash # Convert single MSG to EML mapitool -si email.msg > email.eml # Convert multiple MSG files to mbox mapitool -s *.msg > mail.mbox # Convert PST to individual EML files mapitool --enable-pst --individual -o output_dir archive.pst # Convert PST with path filter mapitool --enable-pst --filter-path "Inbox/Work" archive.pst > work_emails.mbox ``` -------------------------------- ### Open and Traverse PST Archive in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Demonstrates how to open a PST file and navigate its hierarchical structure, accessing folders and messages. This code iterates through all items, printing subject, sender, and path for messages, and folder display names and content counts for folders. Requires the 'mapi/pst' library. ```ruby require 'mapi/pst' # Open PST file File.open('outlook.pst', 'rb') do |io| pst = Mapi::Pst.new(io) # Get root item root = pst.root puts root.props.display_name # => "Personal Folders" # Iterate through all items recursively pst.each do |item| next unless item.type == :message puts "Subject: #{item.props.subject}" puts "From: #{item.props.sender_name}" puts "Path: #{item.path}" puts "---" end # => Subject: Project Update # From: Smith, Alice # Path: Inbox/Work/Projects # --- # Access folder structure root.children.each do |folder| if folder.type == :folder puts "Folder: #{folder.props.display_name}" puts "Message count: #{folder.props.content_count}" end end end ``` -------------------------------- ### mapitool - Command-Line Batch Conversion Source: https://context7.com/mysociety/ruby-msg/llms.txt Converts MSG and PST files to standard formats using the command-line tool, supporting both individual and batch conversion modes. ```APIDOC ## Command-Line Batch Conversion with mapitool ### Description Converts MSG and PST files to standard formats using the command-line tool, supporting both individual and batch conversion modes. ### Method Command-line execution ### Endpoint N/A (Command-line tool) ### Parameters - **-si** (Flag) - Single input mode for MSG files. - **--enable-pst** (Flag) - Enables processing of PST files. - **--individual** (Flag) - Converts PST to individual files (e.g., EML). - **-o output_dir** (Option) - Specifies the output directory for individual file conversions. - **--filter-path "path"** (Option) - Filters messages by a specific folder path within the PST. ### Request Example ```bash # Convert single MSG to EML mapitool -si email.msg > email.eml # Convert multiple MSG files to mbox mapitool -s *.msg > mail.mbox # Convert PST to individual EML files mapitool --enable-pst --individual -o output_dir archive.pst # Convert PST with path filter mapitool --enable-pst --filter-path "Inbox/Work" archive.pst > work_emails.mbox ``` ### Response #### Success Response (Standard Output/Files) - **stdout** - Converted email content (e.g., EML, mbox format). - **Files** - Individual files created in the specified output directory (e.g., .eml files). #### Response Example (See Request Example for expected output redirection and file creation) ``` -------------------------------- ### Open and Traverse PST Archive Source: https://context7.com/mysociety/ruby-msg/llms.txt Opens a PST (Personal Storage Table) file and provides hierarchical access to all folders and messages within the archive. ```APIDOC ## Open and Traverse PST Archive ### Description Opens a PST (Personal Storage Table) file and provides hierarchical access to all folders and messages within the archive. ### Method N/A (Library Function) ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```ruby require 'mapi/pst' # Open PST file File.open('outlook.pst', 'rb') do |io| pst = Mapi::Pst.new(io) # Get root item root = pst.root puts root.props.display_name # => "Personal Folders" # Iterate through all items recursively pst.each do |item| next unless item.type == :message puts "Subject: #{item.props.subject}" puts "From: #{item.props.sender_name}" puts "Path: #{item.path}" puts "---" end # => Subject: Project Update # From: Smith, Alice # Path: Inbox/Work/Projects # --- # Access folder structure root.children.each do |folder| if folder.type == :folder puts "Folder: #{folder.props.display_name}" puts "Message count: #{folder.props.content_count}" end end end ``` ### Response #### Success Response (Traversed Data) - **pst** (Object) - An object representing the PST archive with methods to access root, folders, and messages. - **item** (Object) - An object representing a message or folder within the PST. #### Response Example (See Request Example for output of traversing the PST file) ``` -------------------------------- ### Open and Read MSG File in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Demonstrates how to open an Outlook MSG file using Ruby-Msg and access its properties, attachments, and recipients through a structured object model. Requires the 'mapi/msg' library. ```ruby require 'mapi/msg' # Open MSG file and access message data msg = Mapi::Msg.open('some_email.msg') # Access message properties puts msg.properties.subject # => "Meeting Tomorrow" puts msg.properties.normalized_subject # => "Meeting Tomorrow" puts msg.properties.creation_time # => # # Access recipients msg.recipients.each do |recipient| puts recipient.properties.display_name puts recipient.properties.email_address end # => "Marley, Bob" # => "bob.marley@gmail.com" # Access attachments msg.attachments.each do |attachment| puts attachment.properties.filename puts attachment.properties.size end # => "document.pdf" # => 245680 # Close the file msg.close ``` -------------------------------- ### Process PST Folder Hierarchy in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Navigates through the folder structure of a PST file, processing both folders and messages. It includes functions to recursively traverse the hierarchy, display folder names and message subjects, and find specific folders by their path. Requires the 'mapi/pst' gem. ```ruby require 'mapi/pst' File.open('archive.pst', 'rb') do |io| pst = Mapi::Pst.new(io) root = pst.root # Define recursive folder processor def process_folder(folder, indent = 0) prefix = " " * indent if folder.type == :folder puts "#{prefix}[Folder] #{folder.props.display_name}" puts "#{prefix} Messages: #{folder.props.content_count || 0}" puts "#{prefix} Has subfolders: #{folder.props.subfolders || false}" end # Process children recursively folder.children.each do |child| process_folder(child, indent + 1) end # Process messages in folder folder.children.each do |item| if item.type == :message puts "#{prefix} [Msg] #{item.props.subject}" end end end # Start traversal from root process_folder(root) # Find specific folder by path def find_folder(root, path) parts = path.split('/') current = root parts.each do |part| current = current.children.find do |child| child.props.display_name == part && child.type == :folder end return nil unless current end current end # Access specific folder inbox = find_folder(root, "Personal Folders/Inbox") if inbox puts "Inbox message count: #{inbox.props.content_count}" end end ``` -------------------------------- ### Handle RTF Body Content from MSG Files (Ruby) Source: https://context7.com/mysociety/ruby-msg/llms.txt Demonstrates how to process RTF (Rich Text Format) content within an MSG file. This includes potential decompression and extraction of HTML if the RTF body is encapsulated within a specific format. Requires the 'mapi/msg' and 'mapi/rtf' gems. ```ruby require 'mapi/msg' require 'mapi/rtf' msg = Mapi::Msg.open('rtf_message.msg') # Further processing of msg.properties.body_rtf would go here, # potentially using Mapi::Rtf.decode or similar methods # depending on the exact RTF encapsulation. ``` -------------------------------- ### Open and Read MSG File Source: https://context7.com/mysociety/ruby-msg/llms.txt Opens an Outlook MSG file and provides access to message properties, attachments, and recipients through a structured object model. ```APIDOC ## Open and Read MSG File ### Description Opens an Outlook MSG file and provides access to message properties, attachments, and recipients through a structured object model. ### Method N/A (Library Function) ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```ruby require 'mapi/msg' # Open MSG file and access message data msg = Mapi::Msg.open('some_email.msg') # Access message properties puts msg.properties.subject # => "Meeting Tomorrow" puts msg.properties.normalized_subject # => "Meeting Tomorrow" puts msg.properties.creation_time # => # # Access recipients msg.recipients.each do |recipient| puts recipient.properties.display_name puts recipient.properties.email_address end # => "Marley, Bob" # => "bob.marley@gmail.com" # Access attachments msg.attachments.each do |attachment| puts attachment.properties.filename puts attachment.properties.size end # => "document.pdf" # => 245680 # Close the file msg.close ``` ### Response #### Success Response (Object Model) - **msg** (Object) - An object representing the MSG file with access to properties, recipients, and attachments. #### Response Example (See Request Example for output of property access) ``` -------------------------------- ### Convert MSG to MIME/RFC822 Email in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Shows how to convert an Outlook MSG file to a standard RFC2822 email format with MIME structure, including multipart handling and attachments. The converted email can then be serialized or saved to a .eml file. Requires the 'mapi/msg' library. ```ruby require 'mapi/msg' # Open and convert MSG to MIME msg = Mapi::Msg.open('email_with_attachments.msg') # Convert to MIME object mime = msg.to_mime # Display MIME structure puts mime.to_tree # => - # # |- # # | |- # # | \- # # |- # # \- # # Serialize to RFC2822 format email_string = mime.to_s # Save to .eml file File.open('output.eml', 'w') { |f| f.write(email_string) } msg.close ``` -------------------------------- ### Decompress and Extract RTF/HTML from MSG Source: https://context7.com/mysociety/ruby-msg/llms.txt This snippet demonstrates how to check for compressed RTF data within an MSG file, decompress it, and then extract any encapsulated HTML content. It utilizes the Mapi::Rtf module for decompression and extraction. ```ruby if msg.properties.rtf_compressed rtf_data = msg.properties.rtf_compressed.read # Decompress RTF rtf_decompressed = Mapi::Rtf.decompress(rtf_data) puts "RTF content:" puts rtf_decompressed # Extract HTML if encapsulated in RTF if html = Mapi::Rtf.extract_html(rtf_decompressed) puts "Extracted HTML:" puts html end end # Alternatively, use high-level conversion mime = msg.to_mime # MIME conversion handles RTF automatically # Plain text part will contain RTF-to-text conversion # HTML part will contain extracted HTML if available msg.close ``` -------------------------------- ### Access MSG Message Properties and Metadata in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Retrieves detailed MAPI properties from MSG files, including standard headers, sender information, timestamps, and message class. It demonstrates accessing various properties and checking for attachments and recipients. Dependencies include the 'mapi/msg' gem. ```ruby require 'mapi/msg' msg = Mapi::Msg.open('message.msg') # Access standard properties props = msg.properties puts props.subject puts props.body puts props.body_html # Get sender information puts props.sender_name puts props.sender_email_address puts props.sent_representing_name # Get timestamp information puts props.creation_time puts props.last_modification_time puts props.client_submit_time # Get message class and importance puts props.message_class # => "IPM.Note" puts props.importance # => 1 (normal) puts props.priority # => 1 (normal) # Access transport headers if available if props.transport_message_headers puts props.transport_message_headers # => Full RFC2822 headers from transport end # Check for attachments and recipients puts "Attachments: #{msg.attachments.length}" puts "Recipients: #{msg.recipients.length}" msg.close ``` -------------------------------- ### Convert MSG to vCard Contact in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Illustrates converting an Outlook contact MSG file (IPM.Contact) into vCard format, suitable for importing into contact management systems. The vCard string can be serialized or saved to a .vcf file. Requires the 'mapi/msg' library. ```ruby require 'mapi/msg' # Open contact MSG file contact = Mapi::Msg.open('john_doe.msg') # Check message type puts contact.mime_type # => "text/x-vcard" # Convert to vCard vcard = contact.to_vcard # Serialize vCard vcard_string = vcard.to_s puts vcard_string # => BEGIN:VCARD # VERSION:3.0 # FN:John Doe # EMAIL:john.doe@example.com # TEL;TYPE=WORK:555-1234 # END:VCARD # Save to file File.open('john_doe.vcf', 'w') { |f| f.write(vcard_string) } contact.close ``` -------------------------------- ### Process Recipients in MSG Files (Ruby) Source: https://context7.com/mysociety/ruby-msg/llms.txt This code snippet shows how to open an MSG file, iterate through its recipients, and extract properties like recipient type, display name, email address, and other details such as office location and business phone number. It also demonstrates how to group recipients by type (To, CC, BCC). ```ruby require 'mapi/msg' msg = Mapi::Msg.open('message.msg') # Access all recipients msg.recipients.each do |recipient| props = recipient.properties # Check recipient type # 1 = To, 2 = CC, 3 = BCC type_str = case props.recipient_type when 1 then "To" when 2 then "CC" when 3 then "BCC" else "Unknown" end puts "#{type_str}: #{props.display_name}" puts " Email: #{props.email_address}" puts " Address type: #{props.addrtype}" # Additional recipient properties puts " Office location: #{props.office_location}" if props.office_location puts " Business phone: #{props.business_telephone_number}" if props.business_telephone_number end # Build recipient summary to_recipients = msg.recipients.select { |r| r.properties.recipient_type == 1 } cc_recipients = msg.recipients.select { |r| r.properties.recipient_type == 2 } puts "To: #{to_recipients.map { |r| r.properties.display_name }.join('; ')}" puts "CC: #{cc_recipients.map { |r| r.properties.display_name }.join('; ')}" msg.close ``` -------------------------------- ### Handle Embedded Messages and Attachments in MSG Files (Ruby) Source: https://context7.com/mysociety/ruby-msg/llms.txt Processes nested MSG files and various attachment types found within an MSG file. It demonstrates how to identify embedded messages, extract their subject and sender, convert them to MIME format, and handle regular file attachments by extracting their data. Requires the 'mapi/msg' gem. ```ruby require 'mapi/msg' msg = Mapi::Msg.open('email_with_embedded.msg') msg.attachments.each do |attachment| # Check attachment type puts "Filename: #{attachment.properties.filename}" puts "Attach method: #{attachment.properties.attach_method}" # Handle embedded message if attachment.embedded_msg embedded = attachment.embedded_msg puts "Embedded subject: #{embedded.properties.subject}" puts "Embedded from: #{embedded.properties.sender_name}" # Convert embedded message to MIME embedded_mime = embedded.to_mime # Can also access embedded attachments recursively embedded.attachments.each do |nested_attach| puts "Nested attachment: #{nested_attach.properties.filename}" end end # Handle regular file attachments if attachment.properties.attach_data data = attachment.properties.attach_data if data.respond_to?(:read) File.open("extracted_#{attachment.properties.filename}", 'wb') do |f| f.write(data.read) end end end end msg.close ``` -------------------------------- ### Convert MSG Contacts to vCard Format in Ruby Source: https://context7.com/mysociety/ruby-msg/llms.txt Extracts contact properties from an MSG file and converts them into the vCard format. It demonstrates accessing common contact fields like name, email, phone numbers, and addresses, then utilizes the `to_vcard` method for conversion. Requires the 'mapi/msg' gem. ```ruby require 'mapi/msg' contact = Mapi::Msg.open('contact.msg') # Access contact properties props = contact.properties puts props.display_name puts props.email_address puts props.business_telephone_number puts props.home_telephone_number puts props.mobile_telephone_number puts props.business_address_street puts props.business_address_city puts props.business_address_postal_code puts props.company_name puts props.title puts props.department # Convert to vCard vcard = contact.to_vcard vcard_text = vcard.to_s # Parse and use vCard data puts vcard_text # => BEGIN:VCARD # VERSION:3.0 # FN:Jane Smith # ORG:Acme Corp # TITLE:Senior Engineer # EMAIL:jane.smith@acme.com # TEL;TYPE=WORK:555-0100 # TEL;TYPE=CELL:555-0200 # ADR;TYPE=WORK:;;123 Business St;Metropolis;ST;12345;USA # END:VCARD contact.close ``` -------------------------------- ### Convert MSG to MIME/RFC822 Email Source: https://context7.com/mysociety/ruby-msg/llms.txt Converts an Outlook MSG file to a standard RFC2822 email format with proper MIME structure including multipart handling and attachments. ```APIDOC ## Convert MSG to MIME/RFC822 Email ### Description Converts an Outlook MSG file to a standard RFC2822 email format with proper MIME structure including multipart handling and attachments. ### Method N/A (Library Function) ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```ruby require 'mapi/msg' # Open and convert MSG to MIME msg = Mapi::Msg.open('email_with_attachments.msg') # Convert to MIME object mime = msg.to_mime # Display MIME structure puts mime.to_tree # => - # # |- # # | |- # # | \- # # |- # # \- # # Serialize to RFC2822 format email_string = mime.to_s # Save to .eml file File.open('output.eml', 'w') { |f| f.write(email_string) } msg.close ``` ### Response #### Success Response (String) - **email_string** (String) - The email content serialized in RFC2822 format. #### Response Example (See Request Example for output of `mime.to_tree` and the saved file content) ``` -------------------------------- ### Convert MSG to vCard Contact Source: https://context7.com/mysociety/ruby-msg/llms.txt Converts an Outlook contact MSG file (IPM.Contact) to vCard format for importing into standards-based contact management systems. ```APIDOC ## Convert MSG to vCard Contact ### Description Converts an Outlook contact MSG file (IPM.Contact) to vCard format for importing into standards-based contact management systems. ### Method N/A (Library Function) ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```ruby require 'mapi/msg' # Open contact MSG file contact = Mapi::Msg.open('john_doe.msg') # Check message type puts contact.mime_type # => "text/x-vcard" # Convert to vCard vcard = contact.to_vcard # Serialize vCard vcard_string = vcard.to_s puts vcard_string # => BEGIN:VCARD # VERSION:3.0 # FN:John Doe # EMAIL:john.doe@example.com # TEL;TYPE=WORK:555-1234 # END:VCARD # Save to file File.open('john_doe.vcf', 'w') { |f| f.write(vcard_string) } contact.close ``` ### Response #### Success Response (String) - **vcard_string** (String) - The contact information serialized in vCard format. #### Response Example (See Request Example for output of `vcard.to_s` and the saved file content) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.