Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Authenticated Email (Exchange) to an Email based on the first 10 characters of File Name

Status
Not open for further replies.

thec0dy

IS-IT--Management
Apr 16, 2010
41
US
I have multiple PDF documents that will be dropped into a directory daily. I have a program right now that will extract information from the PDF and can place that information in the filename. One of those things is the fax number. I have file named "9015446622 [Name Name Name] Name (Date).pdf". I also have a powershell script, (can be VBS if you can easily find one or have one available) that will an authenticated email with an attachement to an outside email address. We would like to utilize an eFax email address to send the faxes through email. So basically the document above will be sent to 9015446622@efaxsend.com with the attached pdf. Since I have multiple PDFs in this folder I would need to be able to run this script for every filename and get the first 10 characters which is the fax number and add to the email address section. It should be a simple looping statement with a variable, I just don't know where to start on that part.


Here is a powershell code I have to send the email with the attachment.

Code:
#EMail Example
$smtp = new-object Net.Mail.SmtpClient("mailrelay.yourdomain1.com") #Dns name or IP


#Auth Stuff
$smtpuser = new-object System.Net.networkCredential 
$smtpuser.domain = "mydomain" #Only needed if you have a domain
$smtpuser.username = "myusername"
$smtpuser.password = "mypassword"
$smtp.Credentials = $smtpuser

##Need something there to input the first 10 characters from the file name and append @sendefaxnow.com##
$smtp.Send("myaccount@email.edu", "someone@gmail.com", "PowerShell Email", "This is a email from powershell") 

function do_mail ($myhtml) {
 ##Need something here to attach the current PDF in the path below.##
 $attachmentpathandfilename = "C:\windows\WindowsUpdate.log"
 $mymail = new-object Net.Mail.MailMessage
 $smtp = new-object Net.Mail.SmtpClient("mailrelay.yourdomain1.com") #Dns name or IP
 $mymail.IsBodyHTML = $true
 $mymail.From = "ServerReporting@Company.edu"
 $mymail.To.Add("sysadmins@company.edu")
 $mymail.Subject = ("My Html email example")
 $mymail.Body = $myhtml 
 $mymail.Attachments.Add($attachmentpathandfilename)
 $smtp.Send($mymail)
} #do_mail

 $head = "<style>"
 $head = $head + "BODY{background-color:#9FAEB5;}"
 $head = $head + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
 $head = $head + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#999999}"
 $head = $head + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#CCCCCC}"
 $head = $head + "</style>"
$myhtml = Get-childitem | select name, FullName, length | ConvertTo-HTML -head $head -body ("<H2>My Example Html email</H2>")

do_mail $myhtml

A VBS script would work too.

Thanks everyone for your time!
 
Here is a VBS script to do the same thing.

So currently I need to figure out how to make this look for every .PDF file in a certain directory. The PDF file name will look like this "5805559870 [Name Sending To] Subject Name (Date).pdf". For each file name I need to pull the fax number (first 10 numbers) the Name sending to and the subject if possible. Then use these values to plug them in the script below and send out emails based on that. Then it needs to attach the current file name / location to the script below so it can be sent out in the email.

Code:
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "[[[Name Sending To Here]]]] [[[Subject Name Here]]]" 
objMessage.From = """RPL"" <RPL@cdomain.com>" 
objMessage.To = "[[[FaxNumberValueHere]]]@cdomain.com" 
objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
objMessage.addattachment "c:\overletter.docx"
objMessage.addattachment "[[[current PDF document location]]]"

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "smtp server address"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")[/URL] = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusername")[/URL] = "Domain\User"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendpassword")[/URL] = "password"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25 

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpusessl")[/URL] = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")[/URL] = 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top