Imap FetchError Yahoomail - python

I am trying to fetch email from Yahoo mail.
msg = imap.fetch(str(fromMail[-1]), "(RFC822)")
When the above code it throws the following error but the the same is working for Gmail. Any clue what need to be changed for Yahoo mail.
imaplib.IMAP4.error: FETCH command error: BAD [b'[CLIENTBUG] FETCH Command arguments invalid']

Related

How to Send a URL Link within the Text Body with the Twilio API using Python

I am trying to send a text message that contains both text and a hypeprlink but am encountering the following message from the Twilio API:
"Error - 12300 Invalid Content-Type: Attempt to retrieve MediaUrl returned an unsupported Content-Type."
Here is the code I am attempting to leverage:
import os
from twilio.rest import Client
# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body='Article: https://4r7s.short.gy/GPaoh7',
from_='123-345-5667',
to='123-345-5668',
)
When I send a message without a hyperlink it works fine (e.g. body = 'Here is the article for you to read') but when it contains a link I receive the aforementioned error. I've also tried using a shortened url of the above but that causes the same issue.
I was just able to send messages containing that exact link using my own Twilio account.
There might be an issue in that you are using phone numbers in local format, when they should be provided in e.164 format.
It's possible that your message is being blocked. Certain carriers don't like when you use link shorteners to obscure a link.
The error you are getting definitely seems weird, since you are not sending media. If you continue to have issues with this, I would contact Twilio support.

How to get messages via imap_tools excluding some UID?

Test code:
fetch_criteria = AND(seen=False), AND(NOT(uid=['61','88']))
for message in mailbox.fetch(fetch_criteria, mark_seen=False, charset='utf8', limit=10):
print(message.uid)
Server response:
SEARCH command error: BAD [b'Could not parse command']
Please help me correctly create the conditions to exclude emails with UID 66 and 88
from imap_tools import AND, NOT
mailbox.fetch(AND(NOT(uid=['39', '40'])))
mailbox.fetch(NOT(AND(uid=['39', '40'])))
Both works for me at [yandex, google, outlook, zimbra].
May be your server can not do this.
Error example at mail.ru: imap_tools.errors.MailboxSearchError: Response status "OK" expected, but "NO" received. Data: [b'[CANNOT] Unsupported search criterion: (UID 5,6)']
Lib author.

Get list of emails (Sender's Email, Subject and Body) between given dates through Gmail API in Python

I am working on a project that requires a JSON file response through Gmail API which contains a list of all the emails (Sender's Info, Subject, and Body) between given dates. Then that JSON file will be processed as needed.
I'm not sure how to generate a request that could provide me with the required JSON file using Gmail API through Python.
I'm a beginner, Please help me.
Assuming that you have gone through OAuth and built the service (if not, check the quickstart), you have to do the following:
Call users.messages.list, using the parameter q to filter your messages by date. This q uses the same syntax as in Gmail UI: see Search operators you can use with Gmail. For example, if you wanted to retrieve your messages from the first 9 months in 2020, you would do this:
user_id = "me"
searchFilter = "after:2020/01/01 before:2020/10/01"
messages = service.users().messages().list(userId=user_id, q=searchFilter).execute()
When listing messages, only the id and the threadId are populated. In order to get the full message resource for all these messages, you should loop through them and call users.messages.get for each message, using its id:
for message in messages["messages"]:
messageId = message["id"]
message = service.users().messages().get(userId=user_id, id=messageId).execute()
For each of these retrieved messages, you want to get the subject, body and sender's email. Subject and sender's email can be found on the message headers. You should loop through the headers and look for one named Subject and another one named From. See these answers for more info: Python: how to get the subject of an email from gmail API, Get sender email from gmail-api. Regarding the body, it is to be found at ["payload"]["body"]. See this answer: How to retrieve the whole message body using Gmail API (python).
Reference:
Searching for Messages
REST Resource: users.messages
list(userId=*, labelIds=None, q=None, pageToken=None, maxResults=None, includeSpamTrash=None)
get(userId=, id=, format=None, metadataHeaders=None)

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)")

sendgrid how to track an individual email

I am sending emails via sendgrid and would like to track the status of an email.
client = sendgrid.SendGridClient(username_or_apikey=my_key)
msg = sendgrid.Mail()
msg.add_to('a#foo.com')
msg.set_html('<div> hello there </div>')
msg.set_from('b#foo.com')
msg.set_subject('test sendgrid subject')
resp = client.send(msg)
The response object I am getting back is simply (200, '{"message":"success"}'). I was hoping to get back some for of email id.
I know sendgrid has webhooks, but how do I associate a sg_message_id with an email that I sent?
You can generate unique arguments on a per mail basis that go into the SMTP API JSON string and are included in status messages to webhooks.
More info at this page: https://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html

Categories