Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find/read the Sender Email Address using R, RDCOMClient

Tags:

library("tm")
library("NLP")
library("dplyr")
library("readtext")
library("readxl")
library("foreach")
library("devtools")
library("RDCOMClient")
library("rlist")

WDF = vector()
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

folderName = "Folder Name"
fld <- outlookNameSpace$GetDefaultFolder(6)
fld = fld$folders(folderName)
Cnt = fld$Items()$Count()
emails <- fld$items
df = data.frame(sno = 1:Cnt,Text = "",stringsAsFactors=FALSE)

for(i in 1:10){
  d = as.data.frame(emails(i)$Body(), stringsAsFactors=FALSE)
  df$Text[i] = d[1]
  df$Sender[i] = emails(i)[['SenderName']]
  df$To[i] = emails(i)[['To']]
  df$sub[i] = emails(i)[['subject']]
}
emails(2)[['SenderName']] 

I'm trying to get the senders Email Address by using following code :

emails(2)[['SenderEmailAddress']]

But it ends up giving like this :

[1] "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=E4CD239AB9F44AC4AC0A4015B6F4805A-RATINGSDIRE"
like image 260
Parth Kalra Avatar asked Feb 12 '20 20:02

Parth Kalra


People also ask

Can you email r files?

In R, there is a package named mailR that allows you to send emails from R. In this tutorial, I will show you how to send email using gmail and MS Outlook. In "from =" option, you can enter email address using which you want to send email. In "to=" option, you need to specify email address of the recipient.


1 Answers

The problem is that exchange stores the sender address as either the normal smtp version of the address for external users, but for Exchange users it uses the MS Exchange address. To get the normal smtp address, you have to look up the exchange user on and get their normal smtp email address.

You may want to look at the extrospectr package on github. I haven't used it but it looks like it would give you a clean inbox like you're looking for.

If you look at the .lookup_exchange_sender function in the file read_inbox.R it shows how they handled looking up the address. First you have to look at what type of user the Sender is (which you can do by retrieving the Sender property of the MailItem, and then the AddressEntryUserType property, which has this enumeration). This ends up like emails(2)$Sender()$AddressEntryUserType().

Then then if it's an Exchange user, you would need to get the Sender property of the MailItem (which is an AddressEntry) and then use the GetExchangeUser method on the AddressEntry to return an ExchangeUser object. Once you have that you just need to access the PrimarrySMTPAddress property of the ExchangeUser. When you put it all together, it looks like this: emails(2)$Sender()$GetExchangeUser()$PrimarySMTPAddress().

Link to extrospectr on github: https://github.com/aecoleman/extrospectr

This explains the Outlook methodology for what's stored in the sender email property: SenderEmailAddress property does not contain a standard email address for internal contacts

like image 117
Roger-123 Avatar answered Oct 05 '22 20:10

Roger-123