Help with retrieving any email addresses from a text file
Moderators: Dorian (MJT support), JRL
Help with retrieving any email addresses from a text file
I'm sure this has been discussed in the past I just can't find any examples.
I'm working on a script that retrieves emails from my server and have it extract the email address from the sender and reply using the the extracted address. I have the script working perfectly, all I need now is to extract the senders email address. I can read each line of the text file in Macro Scheduler but I'm stumped when it comes to retrieving the email address only. Note! The email addresses are never the same.
This is the line I want to retrieve the email addresses from.
From: "senders_name@senders_server.com"
I would appreciate if someone could post an example or point me to a discussion that covered this already. I don't need help with retrieving or sending of emails only the email extraction part.
Thank you in advance.
I'm working on a script that retrieves emails from my server and have it extract the email address from the sender and reply using the the extracted address. I have the script working perfectly, all I need now is to extract the senders email address. I can read each line of the text file in Macro Scheduler but I'm stumped when it comes to retrieving the email address only. Note! The email addresses are never the same.
This is the line I want to retrieve the email addresses from.
From: "senders_name@senders_server.com"
I would appreciate if someone could post an example or point me to a discussion that covered this already. I don't need help with retrieving or sending of emails only the email extraction part.
Thank you in advance.
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
You need to use VBScript's Regular Expressions (RegEx) to extract the email address.
VBScript has RegEx tools. No time for sample now, but search tis forum for RegEx examples.
----------------------------
If not RegEx, you can also use Position and MidStr functions.
VBScript has RegEx tools. No time for sample now, but search tis forum for RegEx examples.
----------------------------
If not RegEx, you can also use Position and MidStr functions.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
You always want the email address between the ? Here's the simple way using Pos> and MidStr>:
Get the line with ReadLn> or whatever.
Code: Select all
Let>mystring=From: "senders_name@senders_server.com" <senders_name@senders_server.com>
Pos><,mystring,1,emstart
Pos>>,mystring,1,emstop
Let>emstart=emstart+1
Let>emlen=emstop-emstart
MidStr>mystring,emstart,emlen,emaddr
MDL>emaddr
Thanks BobBob Hansen wrote:You need to use VBScript's Regular Expressions (RegEx) to extract the email address.
VBScript has RegEx tools. No time for sample now, but search tis forum for RegEx examples.
----------------------------
If not RegEx, you can also use Position and MidStr functions.
I found an example at http://www.mjtnet.com/forum/viewtopic.php?p=16467#16467
I keep getting this error:
"Microsoft VBScript runtime error :5
Invalid procedure call or argument: 'Mid'
line 12, Column 2 "
I followed all the instructions posted in that thread but to avail. There was never really a solution for the VB script error.
Here is my code. I have http://www.google.com on line 5 in the MSG1.txt to test the URL search script.
Code: Select all
//A VBScript Function to search a string for a regex pattern
//returns a list of matches separated by semicolons
VBSTART
Function regExSearch(patrn,str)
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive. Default=False
Set matches = RegEx.Execute(str)
List = ""
For each match in matches
List = List & match.value & ";"
Next
regExSearch = Mid(List,1,Len(List)-1)
End Function
VBEND
//Read the file contents into a variable
ReadFile>C:\RetrievedMail\MSG1.txt,FileData
//replace CRLF chars with VBScript equivalents
StringReplace>FileData,CR," & vbCR & ",FileData
StringReplace>FileData,LF," & vbLF & ",FileData
//Double quote any quotes for VBScript
StringReplace>FileData,","",FileData
//Perform the regex search
VBEval>regExSearch("REGEX_PATTERN","%FileData%"),URLList
//We now have a semicolon delimited list of URLs. We could explode this into an array:
Separate>URLList,;,URLS
If>URLS_COUNT>0
Let>k=1
Repeat>k
Let>ThisURL=URLS_%k%
MessageModal>ThisURL
//we could write it to a file:
WriteLn>C:\RetrievedMail\result.txt,result,ThisURL
Let>k=k+1
Until>k,URLS_COUNT
Endif
Thanks
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
I had no time to look at the code you provided but just took a quick look at your final question. Also assumes your RegEx is correct.
I think that these UNTESTED lines should work for you.
Let>Pattern=^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$
VBEval>regExSearch("%Pattern%","%FileData%"),URLList
Replace the URLList and FileData with the appropriate values.
I think that these UNTESTED lines should work for you.
Let>Pattern=^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$
VBEval>regExSearch("%Pattern%","%FileData%"),URLList
Replace the URLList and FileData with the appropriate values.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Here's a working example:
The benefit of the regular expression is that it can be set up to cope whatever the format. However, if the format is always the same you may as well use me_again's simpler solution above.
Code: Select all
//A VBScript Function to search a string for a regex pattern
//returns a list of matches separated by semicolons
VBSTART
Function regExSearch(patrn,str)
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive. Default=False
Set matches = RegEx.Execute(str)
List = ""
For each match in matches
List = List & match.value & ";"
Next
If List <> "" Then
regExSearch = Mid(List,1,Len(List)-1)
end if
End Function
VBEND
//test data:
Let>Line=From: "senders_name@senders_server.com" <senders_name@senders_server.com>
//replace CRLF chars with VBScript equivalents
StringReplace>Line,CR," & vbCR & ",Line
StringReplace>Line,LF," & vbLF & ",Line
//Double quote any quotes for VBScript
StringReplace>Line,","",Line
//Perform the regex search
Let>Pattern=[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+
VBEval>regExSearch("%Pattern%","%Line%"),Email
MessageModal>Email
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
And this one sets the Global property to true and will therefore return ALL emails in the string, and could therefore be used to return all email addresses found in your mail file:
This returns a semi-colon delimited list. Use Separate to explode to an array.
I expect you actually just want to read the file line by line and ONLY return the email address for the line beginning "From:". Otherwise how do you know which email address you want?
Therefore I'd combine my first example with a ReadLn loop which checks to see if the first five chars of the line are "From:" and if so calls the regex function to find the email address.
Code: Select all
//A VBScript Function to search a string for a regex pattern
//returns a list of matches separated by semicolons
VBSTART
Function regExSearch(patrn,str)
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.Global = True
regEx.IgnoreCase = True ' Make case insensitive. Default=False
Set matches = RegEx.Execute(str)
List = ""
For each match in matches
List = List & match.value & ";"
Next
If List <> "" Then
regExSearch = Mid(List,1,Len(List)-1)
end if
End Function
VBEND
//test data:
//Let>FileData=From: "senders_name@senders_server.com" <senders_name@senders_server.com>
ReadFile>C:\RetrievedMail\MSG1.txt,FileData
//replace CRLF chars with VBScript equivalents
StringReplace>FileData,CR," & vbCR & ",FileData
StringReplace>FileData,LF," & vbLF & ",FileData
//Double quote any quotes for VBScript
StringReplace>FileData,","",FileData
//Perform the regex search
Let>Pattern=[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+
VBEval>regExSearch("%Pattern%","%FileData%"),Email
MessageModal>Email
I expect you actually just want to read the file line by line and ONLY return the email address for the line beginning "From:". Otherwise how do you know which email address you want?
Therefore I'd combine my first example with a ReadLn loop which checks to see if the first five chars of the line are "From:" and if so calls the regex function to find the email address.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
A couple of comments.
I tested Marcus' first example and it is capturing the info between the "" quotes, but in an email header that's usually the real name, the email address is between the carets.
If the purpose is to reply to the emails then it would be better to grab the Reply-to header line rather than the From line.
I tested Marcus' first example and it is capturing the info between the "" quotes, but in an email header that's usually the real name, the email address is between the carets.
If the purpose is to reply to the emails then it would be better to grab the Reply-to header line rather than the From line.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
The first example captures only the first matching email address, which in the case of the test data was between quotes. If you replace that with a name it should find the email between chevrons which is now the only one in the line.
The second example returns all email addresses in the input data.
Feel free to tweak the regex pattern according to your needs - otherwise the code remains the same.
The second example returns all email addresses in the input data.
Feel free to tweak the regex pattern according to your needs - otherwise the code remains the same.