Py-appscript: How can I make message with Mail.app - python

I'm trying to create mail with py-appscript (AppleScript interface for python).
I tried following code,
from appscript import *
mail = app('Mail')
msg = mail.make(new=k.outgoing_message,
with_properties={'visible':True,
'content':"hello",
'subject':"appscript",
'sender':'taichino#gmail.com'
})
but got following error messages, and I couldn't find out any information for that...
CommandError: Command failed:
OSERROR: -1701
MESSAGE: Some parameter is missing for command.
COMMAND: app(u'/Applications/Mail.app').make('outgoing_message', with_properties={'content': 'hello', 'visible': True, 'sender': 'taichino#gmail.com', 'subject': 'appscript'})
Suggestions, please?

Problem solved by myself, following code works fine.
from appscript import *
mail = app('Mail')
msg = mail.make(new=k.outgoing_message)
msg.subject.set("hello"),
msg.content.set("appscript")
msg.to_recipients.end.make(
new=k.to_recipient,
with_properties={k.address: 'taichino#gmail.com'}
)
msg.send()
Insted of setting properties in constructor, set each property separately.

Related

Plyer "NotImplementedError: No usable implementation found!"

I am tryin to send desktop notifications with python, and was trying to run a test code but this error showed up. I have already installed pip and plyer. I am using a Mac if it helps.
For reference, I was following this tutorial
Code:
from plyer import notification
notification.notify(
title = 'testing',
message = 'message',
app_icon = None,
timeout = 10,
)
Please do let me know if I did anything wrong.
As the exception says, it is not implemented.
You can see it in the code here:
self._notify(
title=title, message=message,
app_icon=app_icon, app_name=app_name,
timeout=timeout, ticker=ticker, toast=toast, hints=hints
)
# private
def _notify(self, **kwargs):
raise NotImplementedError("No usable implementation found!")

Building Nested Arrays Python

I am trying to create an array to send emails in Mailchimps API. I am following their documentation and I got the following error:
An exception occurred: {'status': 'error', 'code': -2, 'name': 'ValidationError', 'message': 'Validation error: {"message":{"to":["Please enter an array"]}}'}
So I've updated my array as follows:
message = {"from_email": "test1#email.com","from_name":"Test","message":{"to":["email":"test2#email.com"]}}
However, I'm getting an invalid syntax error under my nested array in messages and I can't quite seem to solve this. Could anyone quickly help?
Seems like you used list [] in place of {} for dictionary
..."message":{"to":["email":"test2#email.com"]}}
^ ^
change it to
message = {"from_email": "test1#email.com","from_name":"Test","message":{"to":{"email":"test2#email.com"}}}
As per the documentation of Mail-Chimp and as you tagged with Python, you may use their Python SDK and sending a mail is as simple as below -
import mailchimp_transactional as MailchimpTransactional
from mailchimp_transactional.api_client import ApiClientError
try:
mailchimp = MailchimpTransactional.Client("YOUR_API_KEY")
response = client.messages.send({"message": {}})
print(response)
except ApiClientError as error:
print("An exception occurred: {}".format(error.text))
For Installation, use pip install mailchimp_transactional

HTTPERROR 400 Removing a message label gmail api python

I'm using this slice of code to try to remove the label "INBOX" from a message, but i'm getting error "no label to remove or specify"
message = service.users().messages().modify(userId='me', id=id, body='INBOX').execute();
I think your body is wrong the body is a json object probably something like this
msg_labels = {'removeLabelIds': ['INBOX'], 'addLabelIds': []}
message = service.users().messages().modify(userId=user_id, id=msg_id,
body=msg_labels).execute()
You might want to check the documented example my python is very basic messages.modify python

Python imaplib uid fetch command error

I tried to fetch a message content in the following way
result, data = m.uid('fetch', num, "( FLAGS BODY.PEEK[HEADER.FIELDS (SUBJECT FROM DATE)] BODYSTRUCTURE)")
It worked well when I was connecting to a private mail server "mail.example.com"
But it returns exception when I used "imap.gmail.com"
error: UID command error: BAD ['Could not parse command']
I think gmail doesnot support detailed search like HEADER.FIELDS....
So I tried the following option for gmail server and it worked really well
result, data = m.uid('fetch', num, "(FLAGS BODY.PEEK[HEADER] BODYSTRUCTURE)")

rpython and imaplib sending a search query to imap

I'm trying to import an email with a particular search string from the server. I'm using the following code.
library(rPython)
python.exec("import imaplib
import email")
python.exec('
m=imaplib.IMAP4("imap.myserver.com","143")
m.login(r"myid","mypass")
m.select()')
python.exec('r,data=m.search(None,\'HEADER From "*cds*"\')')
a = python.get("data")
but it seems to not like the escaped apostrophes. I get the message
Error in python.exec("r,data=m.search(None,'HEADER From \"*cds*\"')") :
SEARCH command error: BAD ['unable to handle request: Invalid search parameters: "HEADER FROM \\"*CDS*\\""']
Can anyone help me figure out how to get this right? Thanks

Categories