Cannot switch sender email when using ReplyAll via Outlook in Python Win32 - outlook

I have multiple account associated to my outlook, i am trying to set the From field to this one specific email i own. Looking at https://learn.microsoft.com/en-gb/office/vba/api/outlook.mailitem I should be able to accomplish this by changing the SendUsingAccount property, however, i am running into ERROR:root:(-2147352571, 'Type mismatch.', None, 1). Does anyone know why?
pythoncom.CoInitialize()
outlook = win32.Dispatch("Outlook.Application")
selection = outlook.ActiveExplorer().Selection
count = selection.Count + 1
for i in range(1, count):
message = selection.Item(i)
reply = message.ReplyAll()
newBody = "test"
for myEmailAddress in outlook.Session.Accounts:
if "#test.com" in str(myEmailAddress):
From = myEmailAddress
break
print(From.DisplayName) #prints the email i want fine
reply.SendUsingAccount = From.DisplayName #this line is giving me the error. If I remove it , the email popups fine, but the From address is defaulting to one i dont want to use
reply.HTMLBody = newBody + reply.HTMLBody
reply.Display(False)

Application.Session.Accounts collection returns Account objects, not strings. And MailItem.SendUsingAccount property takes an Account object, not a string.
Replace the line
if "#test.com" in str(myEmailAddress):
with
if "#test.com" in str(myEmailAddress.SmtpAddress):
and
Reply.SendUsingAccount = From.DisplayName
with
Reply.SendUsingAccount = From

Related

MailItem.SentOn on an item in inbox generates Object doesn't support this property or method

I want to know the mails with a particular word in the subject that were sent in the last 5 days. Here is the code snippet.
For Each m In objInbox.items
If InStr(1,UCase(m.subject), "LEAVE;",vbTextCompare) <> 0 and m.SentOn >= now-5 then
msgbox "There is a mail sent on"&m.SentOn
End If
Next
I get an error saying that
Object doesn't support this property or method:m.SentOn
If I remove m.SentOn >= now-5 condition from IF, it works as expected.
You need to make sure the item is really a MailItem object. In VB Script, you can either use the TypeName function (check for "MailItem"), or you can use the Class property (all OOM objects expose it). For the MailItem object, it will be 43.
Not every item in a mailbox folder is necessarily a MailItem.
Try adding a check on the object type before you run the check, like this:
For Each m In objInbox.Items
If TypeName(m) = "MailItem" Then
If InStr(1,UCase(m.subject), "LEAVE;",vbTextCompare) <> 0 and m.SentOn >= now-3 Then
msgbox "There is a mail sent on" & m.SentOn
End If
End If
Next
edit: modified to vbscript specifically rather than outlook-vba

How to store array in a variable in ruby

I need some help working and new with arrays. I am trying to store a value in an email in an instance variable to be used at a later stage of my test. The email body contains the following string
Finally, your Reference Number us 7712342 - please quote this number
So what my tests is going to do is
visit the email page
grab that string and store as str_array = my Array. And I only need the number: 7712342
The problem i have is the email body has a whole bunch of text so not sure how to grab that particular text i need.
Once i have it then im good to carry on with my tests. but im stuck for now.
Hope to find some help with this.
Here is something i quickly wrote.
element :email_form, '.form-control'
element :go_button, '.input-group-btn'
elements :subject, '.all_message-min_text'
def check_email_and_store_ref
email = "test-3#mailinator.com"
email_body = "Finally, your Web Order Reference Number is 7754468 - please quote this in any communication with us until you receive your Subscriber Number."
email_subject_line = find_element_by_text(self.subject.first, email_body, {:text_element => 'header', :partial_match => true})
ref_number = email_bidy.scan(/\d+/)
Capybara.visit 'https://www.mailinator.com/'
email_form.set email
go_button.click
email_subject_line.click
puts ref_number
#I can now use the above ref number in another method within my class
end
Use String#Scan
> email_body = "Finally, your Reference Number us 7712342 - please quote this number"
> reference_no = email_body.scan(/\d+/)
#=> ["7712342"]

Using JobQueue to continuously refresh a message

I'm building a Telegram bot that uses ConversationHandler to prompt the user for a few parameters and settings about how the bot should behave. This information is stored in some global variables since it needs to be available and editable by different functions inside the program. Every global variable is a dictionary in which each user is associated with its own value. Here's an example:
language = {123456: 'English', 789012: 'Italian'}
where 123456 and 789012 are user ids obtained from update.message.from_user.id inside each function.
After all the required information has been received and stored, the bot should send a message containing a text fetched from a web page; the text on the web page is constantly refreshed, so I want the message to be edited every 60 seconds and updated with the new text, until the user sends the command /stop.
The first solution that came to my mind in order to achieve this was something like
info_message = bot.sendMessage(update.message.chat_id, text = "This message will be updated...")
...
def update_message(bot, update):
while True:
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
time.sleep(60)
Of course that wouldn't work at all, and it is a really bad idea. I found out that the JobQueue extension would be what I need. However, there is something I can't figure out.
With JobQueue I would have to set up a callback function for my job. In my case, the function would be
def update_message(bot, job):
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
and it would be called every 60 seconds. However this wouldn't work either. Indeed, the update parameter is needed inside the function in order to fetch the page according to the user settings and to send the message to the correct chat_id. I'd need to pass that parameter to the function along with bot, job, but that doesn't seem to be possible.
Otherwise I would have to make update a global variable, but I thought there must be a better solution. Any thoughts? Thanks.
I had the same issue. A little digging into the docs revealed that you can pass job objects a context parameter which can then be accessed by the callback function as job.context.
context (Optional[object]) – Additional data needed for the callback function. Can be accessed through job.context in the callback. Defaults to None
global language
language = {123456: 'English', 789012: 'Italian'}
j=updater.job_queue
context={"chat_id":456754, "from_user_id":123456, "message_id":111213}
update_job = job(update_message, 60, repeat=True, context=context)
j.put(update_job, next_t=0.0)
def update_message(bot, job):
global language
context=job.context
url = "http://example.com/etc/" + language[context["from_user_id"]]
result = requests.get(url).content
bot.editMessageText(result,
chat_id = context["chat_id"],
message_id = context["message_id"])

TextBox1.Text string on end of a link

I wanted to make an application that checks if a link was blocked on steam.
I used the linkfilter page. (https://steamcommunity.com/linkfilter/?url=)
I tried doing it like this:
WebBrowser1.Url = https://steamcommunity.com/linkfilter/?url=(TextBox1.Text)
But I got two errors. Is there a way to do this?
Did you try setting your string values as actual strings?:
WebBrowser1.Url = new Uri("https://steamcommunity.com/linkfilter/?url=" + TextBox1.Text);
or:
WebBrowser1.Url = new Uri(string.Format("https://steamcommunity.com/linkfilter/?url={0}", TextBox1.Text));

Store Variable value from server to client

I am using this code to connect to server.
'Client-Side Connect Code
sockMain.RemoteHost = 192.168.1.125
sockMain.RemotePort = 12345
sockMain.Connect
'Server Side code to send message to client
sockMain.SendData txtSend.text
Now, if i write :: a="127" , and send it to client from server, then this message will be displayed in textbox on client side , now how can i use this message , a="127" to store variable value ?
You cannot literally create a new named variable within a compiled VB6 application at runtime in the manner described. You can, however, certainly extract the numeric portion of the value in the textbox and assign that value to a local variable in the application.
If you want to capture the numeric portion of the value in the textbox, and you know the text format will always be of the form "a=somenumber," you can strip off the first two characters to eliminate the "a=" portion, and then use the VB `Val' function on the remaining string to capture the numeric value:
' Generalized example for pulling integer portion from
' a textbox of the form 'a=12345'; amend as appropriate. Untested.
Dim a as integer
Dim receivedData as String
' Assuming txtBox1 as the name of the textbox in the client
receivedData = txtBox1.Text
receievedData = mid$(value,3)
a = Val(receivedData)
`

Resources