Sending Email Attachement Through Outlook in R with RDCOMClient - r

I'm Running a daily analysis that spits out a file I would like sent through my outlook Email. The code I used is featured here, and works wonderfully but the attachment part of it never works...
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "gkinne#horizonmedia.com"
outMail[["subject"]] = "Bruh"
outMail[["body"]] = "Tester"
outMail[["Attachments"]]$Add("L:/Document.csv")
outMail$Send()
The original is here:
Sending email in R via outlook
The code works until the attachment part, and the email even sends, just with no Attachment. It spits this error out:
<checkErrorInfo> 80020009
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Error: Exception occurred.
Any Ideas?

Reverse the slashes and escape them.
The problem is that the path is being created in R, which prefers forward slashes (since the backslash is the escape character), but it's being interpreted by Outlook, which only takes backslashes.
For example, try adding an attachment to an Outlook email by pasting a path into the insert file dialogue, but change the backslashes to forward slashes. It doesn't accept it. And that's essentially what you're trying to do.
So reverse to make them backslashes, then add extra backslashes to each one to escape them. For example:
C:\\Users\\MyFiles\\Documents\\document.txt
R will strip out the escape characters and and pass a clean path to Outlook.

The answer that helped me was provided by David Arenburg in comments:
You need to specify a full path. Is L:/Document.csv a full path? Is L
a local driver or you mapped a network driver? If later is the case
you need to specify the actual network path.
Example: \\dfwcot\Home$\lando\bb8\2015-12-24 Daily Report.xlsx

The Add method of the Attachments class accepts four arguments. I'd suggest specifying them explicitly.
The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. Make sure the file is accessible.

You need to do it like this
L:\\Document.csv
Worked for me. Use two backslashes.

I was also facing the same issue of "Error: Exception occurred".
But, in my case i was missing the naming convention of file. So, make sure that the file name must not be separated by SPACE and use delimiter as "-".

You can use the gsub() function to change "/" to double back slashes "\" in your path
Use this:
outMail[["Attachments"]]$Add(gsub("/","\\" ,"L:/Document.csv", fixed = TRUE))

The problem that our customer was facing was that they executed this operation one too many times.
In your example you are using the filename "L:/Document.csv" over and over again.
Outlook will create that attachment in one of the cache folders each time you try to generate this mail. The first one will be "Document.csv", the second one "Document (002).csv" etc...
We saw it running up to (799) and apparently that is the hard limit.
We had to clean out the temp folder in C:\Users<username>\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook<random hex value> to make it work again.
Just double click an attachment in any mail and see where that temporary file shows up and check that directory.

Related

Getting a file via an API with the file name containing a plus sign

We are currently working with an API for file upload and download.
We upload the file successfully, yet we can't download it. We have found that some of the files contain a '+' character which seems to be the issue here.
My question is, if we call the API like so :
nameoffile = "testing+test"
https://sitename/api/filesystem/nameoffile
Could there be another way of calling the API so the clients/browsers handle the name in the way we need them to?
Yes. You're talking about URLEncoding special characters: https://www.tutorialspoint.com/html/html_url_encoding.htm
In your case, a '+' character encodes to "%2B":
nameOfFile = "testing%2Btest"
url = "https://www.google.com/abc/"+nameOfFile

URL just removes parameters

I've created an API for use on my website.
The API I made strips everything using mysql_real_escape_string then puts it into the database.
But the problem I'm having is the URL that my php scripts are using to access the API is cut short sometimes...
Which I have narrowed down to one of the parameters...
When its Ford Mondeo 22' the URL that is passed to simplexml_load_file is
http://mydomain.com/api/create.xml?api_number=brho15p6z1dhqwf5tsff&env=live&number=AJ20023232&title=Ford Mondeo 22'&image=http://mydomain.com/wp-content/uploads/2012/10/914955-150x150.jpg
but the API reports back the URL accessed as
http://mydomain.com/api/create.xml?api_number=brho15p6z1dhqwf5tsff&env=live&number=AJ20023232&title=Ford
If I remove the single quote then everything works fine, any idea how to correct this I suspect there's something I've overlooked when passing variables in the URL
It is the spaces in the "Ford Mondeo 22'" value that is causing the problem. You cannot have a spaces in the URL. You need to use escape characters. The encoded version of the parameter should be
Ford%20Mondeo%2022'
%20 is the escape character for space
I.e. the whole URL should read as follows:
http://mydomain.com/api/create.xml?api_number=brho15p6z1dhqwf5tsff&env=live&number=AJ20023232&title=Ford%20Mondeo%2022'&image=http://mydomain.com/wp-content/uploads/2012/10/914955-150x150.jpg
EDIT:
Your comment indicates that you use PHP. In PHP, you can use urlencode($foo) and urldecode($foo) to switch between the normal string and the encoding string.

Why won't Google Calendar load my dynamically generated ICS file?

I've been given the task of creating an ICAL feed of conference calls for members of our organization. I created a handler in ASP.NET that loops through our database, gets the call data from the database, and creates output that appears valid to me based on what I've read of the ICAL format, and the examples I've seen/disassembled.
Outlook 2007 reads the resulting output and displays the calendar, no problem (screenshot here shows how it renders).
30 Boxes also has no problem with it. (see test here).
But when I try to load the same output into Google Calendar, I get the message "We could not parse the calendar at the url requested":
What's wrong with my output that's causing Google to reject it? You can see the temporary data I'm testing with at this URL: http://www.joshuacarmody.com/temp/icaltest.ics. This is a snapshot of the output from my .ASHX file, unaltered except the phone numbers and passcodes have been sanitized.
Edit with additional Info:
I just tried the following
Created a copy of my test file called "icaltest-1googevent.ics"
Deleted all the VEVENT data from the file
Exported one of my Google calendars to ICS
Copied one VEVENT from Google's exported data into my test file
Attempted to subcribe to icaltest-1googevent.ics in Google Calendar.
I still got an error message. So I'm guessing the issue isn't with my VEVENT data, but with something else about the file. Maybe there's something wrong with my VCALENDAR definition?
the severinghaus ics validator seems to think there is something funny ( a ? ) before the BEGIN CALENDAR
http://severinghaus.org/projects/icv/?url=http%3A%2F%2Fwww.joshuacarmody.com%2Ftemp%2Ficaltest.ics
In my testing google was a lot fussier/rigourous/pedantic - once you get it working with the validator and google it should work in most places.
After lots of trial-and-error, and comparing my output with Google's, I got it working. There were a few problems with my ICS file:
Unescaped characters (I didn't know I had to escape commas!)
Inconsistent line return characters. They didn't show up in my text editor, but I had to use .NET's String.remove() to remove "\r" from my output to get Google to recognize it
The file was missing VCALENDAR:END. Apparently Outlook doesn't much care. Google does.
I had not one, but three funny characters before the BEGIN:VCALENDAR, decimal codes 239, 187, 191.
I found them thanks to the severinghaus.org link above, thanks!
It turns out they're a prefix called BOM in UTF-8, you can read up on it here: http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark
Google doesn't handle this, but after stripping these three characters from the file and uploading to the server, I was able to susbribe to that calendar in Google Calendar (from URL).
I hope this helps someone passing by this page in the future...
I had similar problem until I realised that opening the generated .ics file in Notepad++ wasn't in UTF-8. I was using a method to convert my string to a byte array, but wasn't using an encoder for this, so no matter what content headers I used, the file would never be generated using UTF-8. This simple fix resolved the UTF-8 generation and Google is now happy with my feed:
var utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(myString);
myString= utf8.GetString(utfBytes, 0, utfBytes.Length);

Can i rename a file when the user does save as? HTML

I use url rewriting and one of my urls is /blah/file ILLEGAL CHARS.jpg
ILLEGAL CHARS are names not valid on windows such as * and may confuse servers even after escaping such as ? and /.
I know i still cant save filenames as / and ? but certain characters i would like to let through. How can i give a new filename when the user right clicks and selects save as?
i'm using asp.net
Adding a content-disposition attachment header to the document will allow you to specify an alternate filename. This will have the added side effect of prompting the user to download automatically if they navigate to this file.
Response.AppendHeader("content-disposition", "attachment; filename='MyFile.jpg'");
The easiest thing to do is to ensure your URLs do not have illegal characters in the first place.
If you are using URL rewriting, you should already have a way to strip/replace the illegal characters.
Otherwise, the browser is responsible for taking the URL and figuring out the filename, often with poor results.

imagemagick with foreign characters

ImageMagick doesnt seem to work with foreign characters. I use the following code
It works fine until a letter in the path or the file has a foreign character. How do i convert images to thumbs on my asp.net site? Is there a plug in or another app or version i may use?
Process app = new Process();
app.StartInfo.FileName = #"bin\convert.exe";
app.StartInfo.Arguments = string.Format(#"""{0}"" -resize ""{2}"" ""{1}""", file, newfile, param);
app.Start();
app.WaitForExit();
I would change the name of the file. You probably want to be doing some conversion of the file name anyway to help keep yourself safe from attacks embedded in a file's name. It's usually a bad idea to launch a subprocess with any string that a user can control. If you're catching uploaded files, move them to some new name before running convert.exe - like a name generated from a uuid, for instance.
A workaround is to change the filename to something ascii safe then rename/move it to the name/path you want with full unicode characters.
Answering this question may not be helpful for now but it might be useful for someone.
Converting file path to UTF-8 encoding worked for me.

Resources