Solved

VBS Email with attachment

Posted on 2013-11-13
7
918 Views
Last Modified: 2013-11-18
I want to modify this VBS script so that it sends an attachment:

The attachment can be found at C:\Attachment\attachment.pdf


MailTo = "email@email.com"
From = "email@email.com"
Subject = "This is a test"
Body = "VBSCript email"
Username = "user"
Password = "password"
Email MailTo, From, Subject, Body, Username, Password
Sub Email (MailTo, From, Subject, Body, Username, Password)
 Set objMessage = CreateObject("CDO.Message")
 With objMessage
  .Subject = Subject
  .From = From
  .To = MailTo
  .TextBody = Body
 End With
 With objMessage.Configuration.Fields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user"
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      .Update
 End With
 objMessage.Send
End Sub
0
Comment
Question by:100questions
  • 4
  • 2
7 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 39644802
Use the AddAttachment method (see line 27):
MailTo = "email@email.com"
From = "email@email.com"
Subject = "This is a test"
Body = "VBSCript email"
Username = "user"
Password = "password"
Email MailTo, From, Subject, Body, Username, Password
Sub Email (MailTo, From, Subject, Body, Username, Password)
 Set objMessage = CreateObject("CDO.Message")
 With objMessage
  .Subject = Subject
  .From = From
  .To = MailTo
  .TextBody = Body
 End With
 With objMessage.Configuration.Fields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user"
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      .Update
 End With
 objMessage.AddAttachment "C:\Attachment\attachment.pdf"
 objMessage.Send
End Sub

Open in new window

0
 

Author Comment

by:100questions
ID: 39645803
Thanks.  How do I modify this script to send to multiple email addresses, say email1@email.com, email2@email.com, email3@email.com?
Also, if I want to send any file which is found at C:\Attachment\, whether it's a pdf or txt etc.. how can I modify the script to send any file which is found in the Attachment folder?
0
 

Author Comment

by:100questions
ID: 39645890
Lastly, I would like to see a revised script which also asks the user running the script to add a date to the subject line.  
For instance if I specify the subject to be 'Latest Report', when I run the script I want a prompt to appear so that the user type in a date, say 'November 13' and then the script will concatenate the Subject as following:  Latest Report November 13.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39646189
SubjectDate = InputBox("Date to be added to the subject:", "Enter date")
MailTo = "email@email.com,email2@email.com,email3@email.com"
From = "email@email.com"
Subject = "This is a test " & SubjectDate
Body = "VBSCript email"
Username = "user"
Password = "password"
Email MailTo, From, Subject, Body, Username, Password

Sub Email (MailTo, From, Subject, Body, Username, Password)
	Set objMessage = CreateObject("CDO.Message")
	With objMessage
		.Subject = Subject
		.From = From
		.To = MailTo
		.TextBody = Body
	End With
	With objMessage.Configuration.Fields
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user"
		.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
		.Update
	End With
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder("C:\Attachment")
	Set colFiles = objFolder.Files
	For Each objFile in colFiles
		objMessage.AddAttachment objFile.Path
	Next
	objMessage.Send
End Sub

Open in new window

0
 

Author Comment

by:100questions
ID: 39652051
I would like to modify the script so that in the Subject Line it shows yesterday's date:
It should concatenate with yesterday's date .. ie   "This is the subject line: should automatically enter yesterday's date here"
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39652088
Try this, change this line:

.Subject = Subject

to

.Subject = Subject & " " & FormatDateTime(DateAdd("d", -1, Date), vbShortDate)

~bp
0
 

Author Comment

by:100questions
ID: 39657502
Thanks, this worked.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

So many times I have seen the words written in a question "if only I could show you" or " I know how hard it is for you since you can't see it" in any zone. That has inspired me to write about this tool in windows 7 called "Problem Steps Recorder…
OfficeMate Freezes on login or does not load after login credentials are input.
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now