I'm trying to add Cc: to my email script with Python language.
Here's my code :
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
email = "my addr"
password = "password"
send_to_email = "destination"
cc = "cc mail"
subject = "hello"
message = "hello"
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Cc"] = cc
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.office365.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
i have this error : smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [LO2P265CA0009.GBRP265.PROD.OUTLOOK.COM]')
Can someone help me with this please ?
Related
I am geeting an error while I am trying to send an automated email from python.
The error I am getting is "[Errno 97] Address family not supported by protocol"
# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# create message object instance
msg = MIMEMultipart()
password = "password"
msg['From'] = "risk#gmail.com"
msg['To'] = "game#gmail.com"
msg['Subject'] = "Photos"
# attach image to message body
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
# Login Credentials for sending the mail
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
How can I fix this problem. Any help is appreciated.
I'm unsure if you have resolved this problem, so I'm posting the code that worked for me.
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
password = "password"
msg['From'] = "risk#gmail.com"
msg['To'] = "game#gmail.com"
msg['Subject'] = "Photos"
text = "Here are the photos.\nCheers,\nLife is complex"
msg_text = MIMEText(text, 'plain')
msg.attach (msg_text)
fp = open('image.png', 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
# Login Credentials for sending the mail
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
BTW have you enabled the Gmail security setting that allows "less
secure application access' to your Gmail account? This setting allows
your python script to interact with your account.
I am trying to send an email. The code I am using is found below:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
email_user = 'email#gmail.com'
email_password = 'password'
email_send = 'receiver#gmail.com'
subject = 'subject'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Hi there, sending this email from Python!'
msg.attach(MIMEText(body,'plain'))
try:
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', port=587)
server.starttls()
server.login(email_user, email_password)
server.sendmail(email_user,email_send,text)
server.quit()
print('successfully sent the mail')
except:
print("failed to send mail")
I get an error "failed to send mail" with the error message:
SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials t10sm6451859wra.16 - gsmtp')
The error occurs on the server.login() line, I am not able to login.
I checked other post and it says, it has to do with wrong credentials but my credentials are correct. I have check and double checked.
What could be the problem with this and how do I resolve it?
Send an email using python is easy, you may have encountered mistakes in your snippet code above and I am lazy to debug your code. But here might be the code you prefer.
The smtp.gmail.com,465 make your code secure. This snippet of code will send email to recipient through spam but lot number of emails you wish to send, will not be blocked by Gmail, you also can use bot to send email through recipient via this snippet of code.
Send Multiple Recipients
In the to line, just separate recipients by comma
to = [
'<recipient_email_address1>',
'<recipient_email_address2>',
'<recipient_email_address3>']
import smtplib
def sendEmailFunc():
gmail_user = '<your_sender_email_address_here>'
gmail_password = '<credential_password_of_above_email>'
sent_from = gmail_user
to = ['<recipient_email_address>']
subject = '<write_your_email_subject_here>'
body = "<write_your_email_body_here>"
email_text = """
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print ('====Email Sent Successfully====')
except:
print ('Something went wrong...')
NOTE
Please be careful with your indentation.
Hope this helpful for you.
Thank you.
I'm trying to send an email in Python
It's working without problem with gmail with this code :
import smtplib
sender = 'xxx#xxx'
receivers = ['xxx#gmail.com']
message = "hello"
try:
smtpObj = smtplib.SMTP('smtp.gmail.com:587')
smtpObj.starttls()
smtpObj.login('xxxx#gmail.com', 'my_password')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
print("okay")
except:
print("notokay")
But when i use it with office 365, the email is send but the message is empty.
It's the same code but with 'smtp.office365.com:587' with my correct login and password.
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('hello')
msg['Subject'] = 'Urgent message'
msg['From'] = 'xxx#xxx'
msg['To'] = 'xxx#gmail.com'
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('xxx#gmail.com', 'my_password')
s.sendmail('xxx#xxx', 'xxx#gmail.com', msg.as_string())
s.quit()
Try the following, it might be because you need to create a MIMEText context for the formatting to be accepted by office365.
I have a python script to send email and it works just fine but the problem is when I check my email inbox.
I want that username to be customize username and not the whole email address.
The format you should use for the from address is:
Your Name <username#domain.com>
If you are using multipart message, and render markdown, if you want beautiful messages.
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
multipart_msg = MIMEMultipart("alternative")
multipart_msg["Subject"] = message.splitlines()[0]
multipart_msg["From"] = DISPLAY_NAME + f' <{SENDER_EMAIL}>'
multipart_msg["To"] = receiver
text = message
html = markdown.markdown(text)
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
multipart_msg.attach(part1)
multipart_msg.attach(part2)
server.sendmail(SENDER_EMAIL, receiver,
multipart_msg.as_string())
I got this code at https://en.wikibooks.org/wiki/Python_Programming/Email
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
fromaddr = "youremailid#gmail.com"
toaddr = "target#example.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = ""
body = "This is just a test email. Do not reply to this"
msg.attach(MIMEText(body, 'plain'))
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("youremailusername", "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
Format for from address is
username#server.com
Format for to address is
username#server.com
and smtplib.SMTP accepts 0 or 2 parameters.
The first parameter is type str and the second parameter is type int
I have a script that sends a .png file with SMTP. When I use a hotmail account;
smtplib.SMTP('smtp.live.com', 587)
It works without any problem. But when I use a gmail account;
smtplib.SMTP('smtp.gmail.com', 587)
An error raises: SMTPServerDisconnected: Connection unexpectedly closed
I've changed smtplib.SMTP('smtp.gmail.com', 587) to smtplib.SMTP('localhost') but didn't work. How can I fix this gmail problem?
Try this code,its working fine for me,
import smtplib
## email sending function
def email_sender(input_message, email_to, client):
''' function to send email '''
to = email_to
gmail_user = '' ## email of sender account
gmail_pwd = '' ## password of sender account
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' +'Subject:site down! \n'
input_message = input_message + client
msg = header + input_message
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
you can user smtplib and email for sending emails, this code working for me after i follow this steps.
steps are
Sign in to Gmail.
Click the gear in the top right .
Select Settings.
Click Forwarding and POP/IMAP.
Select Enable IMAP.
6.Click Save Changes
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "your email"
my_password = r"your password"
you = "to email id"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you
html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'
part2 = MIMEText(html, 'html')
msg.attach(part2)
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
s.sendmail(me, you, msg.as_string())
print s
s.quit()