Python (3.x) learner, and overall noob, here.
I am writing a very basic script to allow users to send emails from the cli.
After reading up a little (here) I followed this example as a starting point.
I became curious about SMTP, and found out that "The original SMTP specification did not include a facility for authentication of senders." (Wikipedia)
Being a newbie, I was surprised to find that I could email people, using any 'from address', without any form of authentication, by specifying certain SMTP servers, that do not, in fact, require authentication. Here is a very basic version of the program:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
user_server = input("Input SMTP> ")
server = smtplib.SMTP(user_server)
server.ehlo()
#server.starttls() <-- commented out
server.ehlo()
#server.login("user#service.com","password") <-- commented out
fromaddr = input("Insert from-address> ")
toaddr = input("Insert to-address> ")
subject = input("Insert Subject> ")
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
body = "Testing"
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
I'd like my script to work whether the SMTP server of choice requires authentication or not (ie: ask user to authenticate when needed).
How could I do that? what functions/modules/parameters would be required?
Related
I am trying to send emails via Gmail using Python. However I would like to add my email signature and font size (large size) that is already specified in my Gmail settings. Below is code to send the email; what do I need to add to accomplish this requirement?
Code as follows:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
subject = "Offer"
message = "My offer is 2 dollars"
email = "sender#gmail.com"
password ='mypassword'
send_to_email = "recipient#gmail.com"
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
Plain text MIME parts (text/plain) do not have a font size; they are just ASCII text without any formatting facilities (beyond what ASCII provides, i.e. line breaks and perhaps tabs).
If you want to embed formatting, send HTML email.
Incidentally, your code looks like it's pre-Python 3.6; the email library went through a significant overhaul in that version. New code should generally target the new EmailMessage API, which is more straightforward and also more versatile than the old one, which required you to explitly set up a MIME structure in every message. See the examples in the documentation for good starting points.
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 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.
Is there are way to pick the report from specific folder and send email to mentioned recipient on a specific time schedule using python packages and code in windows 10?
I have an idea and want to have some inspirational codes if anyone can help to begin with this project.
Part 1:
script to send mails in python
you need to access send_email() function in your main python file in which you schedule the script
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def send_email(mail_body="", subject=""):
fromaddr = "sender mail address"
toaddr_list = [
//recepients address list
]
passwd = "need to create gmail app to get password"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddr_list)
if(subject):
msg['Subject'] = subject
else:
msg['Subject'] = "subject of mail."
body = mail_body
msg.attach(MIMEText(body, 'plain'))
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromaddr, passwd)
text = msg.as_string()
s.sendmail(fromaddr, toaddr_list, text)
s.quit()
Part 2: schedule script in windows 10
For scheduling script in windows you can refer this:
https://datatofish.com/python-script-windows-scheduler/
Yes, you can do that using different python packages.
Read the specific file
Send mail using built-in smtplib module
Time scheduler using schedule library Or you can use cron or celery in windows.
Read Automate the boring stuff - Chapter 16.
You will get both Code and ideas of how to implement the same
Automate the boring stuff