this is the solution for sending gmail using python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['From'] = 'me#gmail.com'
msg['To'] = 'you#gmail.com'
msg['Subject'] = 'Hello'
message = 'This is mail from python.'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('me#gmail.com', 'password')
mailserver.sendmail('me#gmail.com','you#gmail.com',msg.as_string())
mailserver.quit()
# Note: You may have to change following settings
# disable access protection. to do this right click mcafee virus then
#open
#virus scan control and right click access protection
# in gmail allow less secure apps
#https://myaccount.google.com/security#connectedapps
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 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.
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?
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?