I'm sending emails using the Sendgrid API, but every email that I send does not have the Subjet.
My code looks like this:
def send_notification(sendgrid_key, p_email):
message = Mail(
from_email=('my_email#mail.com'),
to_emails=(p_email),
subject='subject email',
)
message.template_id = 'XXXXXX'
try:
sg = SendGridAPIClient(sendgrid_key)
response = sg.send(message)
except Exception as e:
print(str(e))
Eventhought, I have set the subjet, I'm still receiving emails without subject. Moreover, I do not have any errors when my app run.
Twilio SendGrid developer evangelist here.
When you are using SendGrid's dynamic templates you actually set the subject in the template, not through the API. Take a look at this screenshot to see where you set the subject in the template editor.
If you want to set the subject dynamically too, you can add a template string in the subject and set that through dynamic template variables when you send the email. Check this article on creating a dynamic email subject line with mustache templates for more information.
Related
I would like to use Twilio Whatsapp messages with custom templates. I'm trying to send messages using a Python client. We have different types of messages and we would like to manage them from Twilio backend. We created several templates, but I have no idea how to specify a template in the code.
In the official documentation there is no mention of this. I looked into the source code of the client, here is a code from class MessageList, that I use for message creation:
def create(self, to, status_callback=values.unset, application_sid=values.unset,
max_price=values.unset, provide_feedback=values.unset,
attempt=values.unset, validity_period=values.unset,
force_delivery=values.unset, content_retention=values.unset,
address_retention=values.unset, smart_encoded=values.unset,
persistent_action=values.unset, from_=values.unset,
messaging_service_sid=values.unset, body=values.unset,
media_url=values.unset):
There is nothing similar to the template name in function parameters.
Is it possible at all to specify the template programmatically?
Twilio developer evangelist here.
When you want to send a message using a template, all you have to do is ensure that the body of the message you are sending matches the template. For example, if your templates is:
Your login code for {{1}} is {{2}}.
Then sending the message:
Your login code for Twilio is 12345.
matches that template and will send successfully. So you don't specify the template programmatically, just with your message content.
You can see more about sending template messages with the Twilio API for WhatsApp here.
from twilio.rest import Client
client = Client() # this is the Twilio sandbox testing number
from_whatsapp_number = 'whatsapp:+14155238886'
to_whatsapp_number = 'whatsapp:+15005550006'
client.messages.create(body='Ahoy,world!', from_=from_whatsapp_number, to=to_whatsapp_number)
Problem
I am using a "logic app" in Azure to create a queue of incoming mails. The way the emails are registered are using a "message id", which is described as "a unique identifier for a message". I would like to be able to fetch emails over imap using this id - is this possible?
Logic app "message id"
Example of "message id":
AQMkADAwATM3ZmYAZS0yNTYwLWNkZAAzLTAwAi0wMAoARgAAA-U4TGbG56lEtdoXy_23gW0HAKhWKDtf5AJErHyhh_b9NYQAAAIBDAAAAKhWKDtf5AJErHyhh_b9NYQAAAIFfgAAAA==
Example of logic app:
What I have tried
I have tried just to download all emails as eml, and then to read them into notepad++ to see if the "message id" even exists in the eml-files, but they don't.
# Library for downloading emails
import imaplib
# Logging in
mail = imaplib.IMAP4_SSL("outlook.office365.com",993)
mail.login(email_user, email_pass)
# Downloading emails to eml
mail.select('Inbox')
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)')
f = open('%s/%s.eml' %("/my/path/", num), 'wb')
f.write(data[0][1])
mail.close()
mail.logout()
May i know why are you trying to fetch email over IMAP . As you can fetch fetch email using message id from outlook api too. Here is the api which you can use:
GET https://outlook.office.com/api/v2.0/me/messages/{message_id}
You can find more details here:
https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations#GetMessages
Also just to update , in outlook you wont' find the message id in .eml or in body, it's available in internet header. Most clients utilities download the headers (including the Message-ID) of all messages, store them, and then post-process them. For outlook you can find it in internet header like below:
Reference: https://www.codetwo.com/kb/messageid/
Still if you want to access email using IMAP, try below thread and see if it helps:
https://www.go4expert.com/articles/accessing-email-using-imap-python-t28838/
https://social.msdn.microsoft.com/Forums/en-US/29f44441-feda-4f81-a04c-40d53b3dfdc5/how-to-access-an-email-using-messageid-in-outlook?forum=outlookdev
Hope it helps.
I am uploading a file using files.upload method and want to display username like XYZ or any other custom username (which works in chat.postMessage).
Below code is working, file is uploading but in response username returns empty and in Slack message displays with app name Demo App.
response = slack.api_call(
'files.upload',
channels='#website',
file=io.BytesIO(file.read()),
title='File from slack api',
initial_comment='Create by XYZ',
username='XYZ',
)
When posting text message it is working fine and displaying same name I mention in username
response = slack.api_call(
'chat.postMessage',
channel='#website',
text='This is from slack demo',
username='ABC'
)
This is not a feature that the this Slack API method offers. If you share a file is will always be showing the name of the actual token owner who uploaded it. Which may be a bot user or real user.
I use send_raw_email to send emails with HTML content and file attachments. How do I insert an ical/ics invite to the email?
I use icalendar to generate ics content.
This is what I came up with so far, but it shows in Gmail as a file attachment.
if calendar_reminder_date:
cal = Calendar()
cal.add('prodid', '-//My calendar product//mxm.dk//')
cal.add('version', '2.0')
cal.add('calscale', 'GREGORIAN')
cal.add('method', 'REQUEST')
event = Event()
event['dtstart'] = calendar_reminder_date.strftime("%Y%m%dT%H%M%SZ")
event['dtstamp'] = calendar_reminder_date.strftime("%Y%m%dT%H%M%SZ")
event['summary'] = 'Python meeting about calendaring'
cal.add_component(event)
attachment_part = MIMEText(cal.to_ical())
print repr(cal.to_ical())
del attachment_part['Content-Type']
attachment_part.add_header('Content-Type', 'text/calendar', name='invite.ics')
attachment_part.add_header('Content-Disposition', 'attachment', filename='invite.ics')
msg.attach(attachment_part)
Hard to tell without seeing the actual full MIME message as received on the Google side but there are definitely several things missing here:
a calendar REQUEST must have an ORGANIZER property, as well as at least one ATTENDEE property with a value corresponding to the email address of the gmail user (prefixed with a mailto:).
your content-type is missing a METHOD=REQUEST parameter and you probably do not want to set any content-disposition.
See also Multipart email with text and calendar: Outlook doesn't recognize ics
I'm sending emails using python. Currently my technique is this:
msg = email.mime.Multipart.MIMEMultipart()
msg["From"] = username
msg["To"] = recipient
msg["Subject"] = subject
mimeText = email.mime.Text.MIMEText(body, "html")
msg.attach(mimeText)
stringMsg = msg.as_string()
I would like to also add some metadata to the message - specifically, a unique identifier of the task the email is achieving, so that when it is checked later (possibly by a different service), I can avoid sending a duplicate email.
This metadata doesn't need to be completely secret or secure, just something that standard email clients don't render. Obviously there are the options of including a bogus BCC email address containing the id, or adding a hidden html node to the body.
<div style="display:none;">123456789</div>
But both of those seem quite "hacky". Is there anything like this that will get persisted and sent as part of the email, that I can check using imaplib later?
msg["secretMetadata"] = "123456789"
User defined fields are permitted and explained in RFC822. Basically you can prefix your custom field with X- and this will not conflict with any existing fields nor extension fields.
So, something like msg["X-secretMetadata"] = "123456789" should suffice.
It sounds like you might want to use X-headers. Information in the X-headers of an email message is usually used for application-specific purposes, and this information is usually not displayed by mail clients.