Solved

VBS Email with attachment

Posted on 2013-11-13
7
941 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 84

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 84

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 54

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OfficeMate Freezes on login or does not load after login credentials are input.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

679 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