Page 1 of 1 1
Topic Options
#211361 - 2016-04-21 08:08 PM How do you attach a file to the email?
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
OK - I am back!!
My next problem is getting an attachment on the email. The email sends, but no attachment.
What am I missing? Outlook is 2010.
 Code:

$rc = SetOption("NoMacrosInStrings","On")
$rc = SetOption("NoVarsInStrings","On")

; outlook variables

	$Root = CreateObject("Outlook.Application")
	$Session = $Root.GetNamespace("MAPI")
	$DefaultProfile = Outlook
	$Session.Logon($DefaultProfile)	
	$Folder = $Session.GetDefaultFolder(olFolderInbox)
	$Folder.Display

; launch outlook and add email information

			$MailItem = $Root.CreateItem(0)
? "Root Creation: "+@Error
			$MailItem.Importance = 1
			$MailItem.Subject = "Daily Report -- DO NOT REPLY!!!!"
			$MailItem.Sender = "sender@someplace.org"
			$MailItem.To = "receiver@someplace.org"
			$MailItem.Body = "Daily Report Attached!!!"
			$Attachments = "C:\Cognos\Data\reportbyday.pdf"
			$MailItem.Send
			$Root = 0


Edited by Karon (2016-04-21 08:11 PM)

Top
#211362 - 2016-04-22 04:26 AM Re: How do you attach a file to the email? [Re: Karon]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
You left another line out of your original...
_________________________
!

download KiXnet

Top
#211363 - 2016-04-22 09:10 AM Re: How do you attach a file to the email? [Re: Karon]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
You need to tell Outlook to use the attachment. At the moment you are just putting a path and filename into a variable. Sure that works fine but Outlook does not know it should attach the file to the e-mail. You need to add it to $mailitem like shown below.

 Code:
....
$MailItem.Attachments.Add("c:\somefolder\somefile.txt")
....
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#211364 - 2016-04-22 09:21 AM Re: How do you attach a file to the email? [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Oh and since a few months I'm trying to avoid sending anything with Outlook from scripts because it is VERRY frustrating (major understatement but let’s stay away from the bad words) that sometimes it works and sometimes without any changes it does not work. I like my health better then anything else so this has frustrated me enough and I'm moving all our scripts that send out e-mails over to BLAT and an internal SMTP relay. If you like Outlook that’s fine but I’m just throwing in an alternative. Outlook is nice but not for scripting IMHO.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#211366 - 2016-04-22 02:22 PM Re: How do you attach a file to the email? [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
BTW - there's a full BLAT implementation in the script vault. \:\)
_________________________
Actually I am a Rocket Scientist! \:D

Top
#211367 - 2016-04-22 02:49 PM Re: How do you attach a file to the email? [Re: Glenn Barnas]
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
Lonkero - I checked and this is what I have.... If I am missing something, please let me know what it is! Thanks ALL!!!
Top
#211368 - 2016-04-22 02:56 PM Re: How do you attach a file to the email? [Re: Karon]
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
I just ran it with the following:
 Code:

$rc = SetOption("NoMacrosInStrings","On")
$rc = SetOption("NoVarsInStrings","On")

; outlook variables

	$Root = CreateObject("Outlook.Application")
	$Session = $Root.GetNamespace("MAPI")
	$DefaultProfile = Outlook
	$Session.Logon($DefaultProfile)	
	$Folder = $Session.GetDefaultFolder(olFolderInbox)
	$Folder.Display

; launch outlook and add email information

			$MailItem = $Root.CreateItem(0)
? "Root Creation: "+@Error
			$MailItem.Importance = 1
			$MailItem.Subject = "Daily Report -- DO NOT REPLY!!!!"
			$MailItem.Sender = "sender@someplace.org"
			$MailItem.To = "receiver@someplace.org"
			$MailItem.Body = "Daily Report Attached!!!"
			$MailItem.Attachment.Add("C:\Cognos\Data\reportbyday.pdf")
			$MailItem.Send
			$Root = 0


Still no attachment....

Top
#211369 - 2016-04-22 03:01 PM Re: How do you attach a file to the email? [Re: Karon]
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
BAAA HAAA! helps if it says "Attachments" instead of "Attachment" - added the s and runs like a champ now!! OMG I think I may be well on my way to getting that 2003 server GONE!!
Great start to my day!!

Top
#211370 - 2016-04-22 03:36 PM Re: How do you attach a file to the email? [Re: Karon]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 Originally Posted By: Karon
BAAA HAAA! helps if it says "Attachments" instead of "Attachment" - added the s and runs like a champ now!! OMG I think I may be well on my way to getting that 2003 server GONE!!
Great start to my day!!


LOL we all had that at some point. It are always the small things that get overlooked
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#211371 - 2016-04-22 03:46 PM Re: How do you attach a file to the email? [Re: Mart]
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
Sometimes I just need someone to be an extra set of eyeballs!! LOL
Top
#211372 - 2016-04-22 04:28 PM Re: How do you attach a file to the email? [Re: Karon]
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
So on a Windows 7 32 bit PC running Outlook 2010, the following email script works:

 Code:
; verify the files to e-mail exists
If Exist ("C:\Cognos\Data\reportbyday.pdf")

;main program loop

$rc = SetOption("NoMacrosInStrings","On")
$rc = SetOption("NoVarsInStrings","On")

; outlook variables

	$Root = CreateObject("Outlook.Application")
	$Session = $Root.GetNamespace("MAPI")
	$DefaultProfile = Outlook
	$Session.Logon($DefaultProfile)	
	$Folder = $Session.GetDefaultFolder(olFolderInbox)
	$Folder.Display

; launch outlook and add email information

			$MailItem = $Root.CreateItem(0)
? "Root Creation: "+@Error
			$MailItem.Importance = 1
			$MailItem.Subject = "Daily Report -- DO NOT REPLY!!!!"
			$MailItem.Sender = "sender@someplace.org"
			$MailItem.To = "receiver@someplace.org"
			$MailItem.Body = "Daily Report Attached!!!"
			$MailItem.Attachments.Add("C:\Cognos\Data\reportbyday.pdf")
			$MailItem.Send
			$Root = 0
endif


Thanks to everyone for all your assistance!!

IF there are checks and balances that anyone thinks I need, just let me know!

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.074 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org