I wanted fetch issues from from project in jira using jira api and send it to groupemail via jira automatically. I am using PYTHON. I have tried fetching data using jira api. But am stuck sending email . Could someone please help here. How can we do it either SMTP or any API.?
You can use the smtplib module in Python to send the email messages. Make sure you do have the SMTP host & SMTP port values for your account with you.
The below is a simple script to send email from your account.
import os
import smtplib
from email.message import EmailMessage
RECEIVER_EMAIL_LIST = [] #list of recipients
SMTP_SERVER = os.environ['smtp_server']
SMTP_PORT = os.environ['smtp_port']
SENDER_EMAIL = os.environ['my_email']
SENDER_PASSWORD = os.environ['my_email_password']
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
message = EmailMessage()
email_body_content = 'Your content here'
message.set_content(email_body_content)
message['Subject'] = 'Subject goes here'
message['From'] = SENDER_EMAIL
message['To'] = ', '.join(RECEIVER_EMAIL_LIST)
server.send_message(message)
For documentation, click here.
Related
I am developing a simple system for obtaining stock information and sending it to me by email. The problem is occurring with the function that should send the email.
I'm trying to use Gmail to do this and it works on my machine, but when it runs on the Google Cloud Function instance, an error is generated. The error says:
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt\n5.7.14 DI7EWTXsfkIgcqDF1iU9gzO45O2rHEDek8EzvslcMMuOLXixqGFX_YhwsIz9P9OsZkRDf\n5.7.14 F7fWu8KmILP4m54Yb925Tl93FSuXLCUiWn7REseAjcak7RpZ_2tgWliAGFufsPSW>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 b18sm13789218iln.46 - gsmtp')
And here is my simple code
import smtplib, ssl, os, sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
SMTP_SERVER = os.getenv('SMTP_SERVER', 'smtp.gmail.com')
SMTP_PORT = os.getenv('SMTP_PORT', 587)
EMAIL_USER = os.getenv('EMAIL_USER')
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
def send_mail(email_from, to, subject, body_message, from_name=None):
from_str = '{} <{}>'.format(from_name, email_from) if not from_name is None else email_from
msg = MIMEMultipart()
msg['From'] = from_str
msg['To'] = to
msg['Subject'] = subject
body_html = body_message
msg.attach(MIMEText(body_html, 'html'))
text = msg.as_string()
server = smtplib.SMTP(SMTP_SERVER,SMTP_PORT)
server.ehlo() # Can be omitted
server.starttls()
server.ehlo() # Can be omitted
server.login(EMAIL_USER, EMAIL_PASSWORD)
server.sendmail(email_from, to, text)
server.quit()
The above function works perfectly on my machine. I already enabled insecure apps on this account.
I saw a post here that was talking about something like that. At one point in the post you can find the quote below.
Still not working? If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:
https://accounts.google.com/DisplayUnlockCaptcha
So, this is the error that I'm getting but I don't know how to solve it inside of a Cloud Function.
Here is the link to the full discussion: Login credentials not working with Gmail SMTP
I have created python file that can send email using less secure apps turned on, but I need it off. How can I send email with 2FA?
# import simple mail transfer protocol library
import smtplib
# import EmailMessage method
from email.message import EmailMessage
contacts = ['<email#email.com>', '<email2#email.com>']
EMAIL_ADDRESS = '<my_gmail>'
EMAIL_PASSWORD = '<my_gmail_password>'
# Create empty Email Message object
msg = EmailMessage()
msg['Subject'] = 'Automated python email sender 5'
msg['From'] = EMAIL_ADDRESS
msg['To'] = contacts
msg.set_content('<sample_content>')
# contact manager we will make sure our connection is closed automatically without us doing it manually
# 465 is the port number for plain SMTP_SSL
# SMTP_SSL means you do not have to use ehlo() ans starttls()
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
What should I add to make 2FA work?
I believe you would need to set up an App passwords let you sign in to your Google Account from apps on devices that don't support 2-Step Verification. Learn more
Its basically something thats setup in the users Google account.
Another thing to look at would be Display captcha
if that doesn't work you might need to look into Xoauth2
if you require 2fa
If you want to support 2fa then you should not be using the SMTP server and should be going though the Gmail API and using Oauth2 which will prompt a user to connect via their google account and google will control the 2fa themselves. SMTP servers are not designed to handel 2fa
Try setting up App Password for your Gmail. and used that app password for smtp login.
This will help Set Up APP-Password
Under security you have to generate a password for your application:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('demo#gmail.com', 'password generated by google')
server.sendmail('demo#gmail.com', 'demo#icloud.com', 'Mail sent from program')
print('Mail Sent')
as you can see, you enter your email but instead of typing your own password you enter the password generated by google for your application
I want to send emails using my corporate account. However, I get the following error in Python:
SMTPAuthenticationError: 550, b'5.2.1 Mailbox cannot be accessed
If I open up my corporate account on outlook, I am able to send emails through it using Powershell script. But Python script only gives me the error above.
The following is my python code:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = "Hello, This is a simple mail. There is only text, no \
attachments are there The mail is sent using Python SMTP library"
sender_address = 'corporate_email_account'
sender_pass = 'XXXX'
receiver_address = 'corporate_email_account'
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
message.attach(MIMEText(mail_content, 'plain'))
session = smtplib.SMTP('smtp.office365.com', 587)
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
Why is Python unable to send the emails? Could this be a firewall issue or maybe Microsoft doesn't allow this behavior in python scripts, but does with Powershell scripts?
I am working on script to send emails.
Emails are supposed to be send from a shared mailbox in outlook.
This shared mailbox doesn't have a password and here is the trick: on the one hand I need the authenticate and provide a password, on the other hand I have no password to provide.
I ran the scripted with my professional outlook mailbox (email address + password) and it works.
Here is my config:
MY_ADDRESS = "---emailaddressofthesender---"
PASSWORD = ""
MESSAGE = "some message"
# set up the SMTP server
s = smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
msg = MIMEMultipart() # create a message
# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']="---emailaddresseofthereceiver---"
msg['Subject']="This is TEST"
# add in the message body
msg.attach(MIMEText(MESSAGE, 'plain'))
# send the message via the server set up earlier.
s.send_message(msg)
del msg
# Terminate the SMTP session and close the connection
s.quit()
I tried:
Read the doc smtplib. Unless mistaken, I found no mentions about
authentications without password.
PASSWORD = "": authentication error
PASSWORD = None: authentication error
s.login(MY_ADDRESS): PASSWORD argument is missing
remove s.login() method from the script: Client was not authenticated to send anonymous mail during MAIL FROM [PR0P264CA0184.FRAP264.PROD.OUTLOOK.COM]
The only working solution I have now is to send from my mailbox credentials and not from the shared mailbox. For the sake of this project, this solution is not acceptable.
Many thanks in advance for your help
Add this right before the s.send() step and it will work.
s.SentOnBehalfOfName = 'SharedFolder'
I am using the Python library smtplib to send Email messages with Office365 as the SMTP server. Everything was fine until a few days ago when my From: header seemed to not be processed. The Python code I am using:
import smtplib
from email.mime.text import MIMEText
def login():
server = smtplib.SMTP(mail.mail_server, mail.mail_port)
server.starttls()
server.login(mail.mail_username, mail.mail_password)
return server
def send(subject, body):
msg = MIMEText(body)
msg["From"] = mail.mail_from
msg["To"] = ", ".join(mail.mail_to)
msg["Subject"] = subject
server = login()
server.sendmail(mail.mail_username, mail.mail_to, msg.as_string())
server.quit()
send("test", "test")
What is strange is that if I log onto Outlook or OWA, I can see my header if I view the sent message details. I sent a test header of From: xxxx <info#...>
However on the recipients inbox message, the header is simply the Office365 User's name and the info address as shown above (From: Name <info#...>). The xxxx custom header is gone.
What can be causing my header to be dropped?
While it may not be an ideal solution, I ended up setting up a new email account with the name I wanted and used that, instead of trying to force the From: header.