So basically I am trying to develop a python program that lets me send emails to my friend's email without signing in. However I get a Error that says smtplib.SMTPRecipientsRefused:
Here is my code..
import smtplib
myemail = 'myemail#gmail.com'
mypassword = 'password'
friendemail = input('Please enter the email of the person:')
with smtplib.SMTP("smtp.gmail.com", 587) as smtp:
subject = input("Subject: ")
body = input('Message:')
msg = f"subject:{subject}\n\n{body}"
smtp.starttls()
smtp.login(myemail,mypassword)
smtp.sendmail(myemail, mypassword, msg)
It would be great if anyone could help! Thank YOU!
I tried to run the code you provided, and there was a problem with the image below, right?
According to the prompt, I changed the parameters of sendmail.
smtp.sendmail(myemail, friendemail, msg)
The parameters here represent: sender's mailbox, recipient's mailbox, and email content.
After the change, I successfully ran it in vscode and sent an email to my friend's Gmail email.
Note: the email name and password should be filled in correctly and can not be omitted.
Related
i got the above error each time i try sending a mail using python. am somewhat new to this whole smtp thing. below is my code. please help.
import smtplib
my_email ="okoyekennethoptimizer#gmail.com"
password = "nmmrjdphptwfgjgb"
connection = smtplib.SMTP("smtp.gmail.com", 587)
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs="okoyek10#yahoomail.com", msg="hello")
connection.close()
please help. its been giving me tough time
Try creating an "App password" after enabling 2-step auth in your Gmail. You still need to do some lookup but generally, this should help.
The trick is in:
Enabling less secure apps.
Make sure your account has 2FA.
Generating an app password under My Google Account>>Security under Signing in to Google.
Replace the 16-digit password with the password you've placed in the password variable.
Just before connection.starttls() add connection.ehlo().
A code for example:
my_email ="okoyekennethoptimizer#gmail.com"
app_password = "16-digit-app-password"
connection = smtplib.SMTP("smtp.gmail.com", 587)
connection.ehlo()
connection.starttls()
connection.login(user=my_email, password=app_password)
Also you SHOULD create From:, To:, and Subject: message headers, separated from the message body by a blank line.
E.g.
msg = "\r\n".join([
"From: user_me#gmail.com",
"To: user_you#gmail.com",
"Subject: Any subject",
"",
"Good Evening, how are you?"
])
I am trying to learn how to send an email using python. All the tutorials on the web that I have read explain how to do it using Gmail.
But, from 30/05/2022 (despite the fact that everybody is free to do whatever he wants with his account) Google has a new policy that states:
To help keep your account secure, starting May 30, 2022, Google will no longer support the use of third-party apps or devices that only ask for your username and password for you. Sign in to your Google account.
Source: https://support.google.com/accounts/answer/6010255
And we get:
So my question is there any other way to send an email using python, (including email accounts belonging to an other company)?
Here is my function to send an email:
def send_email_fct(filename, filepath, fromaddr, mdpfrom, toaddr):
"""" filename: file name to be sent with extension
filepath: file path of the file to be sent
fromaddr: sender email address
mdpfrom: password of sender email address
toaddr: receiver email address"""
msg = MIMEMultipart() # instance of MIMEMultipart
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "data file"
body_email = "Body_of_the_mail"
msg.attach(MIMEText(body_email, 'plain'))
attachment = open(filepath, 'rb') # open the file to be sent
p = MIMEBase('application', 'octet-stream') # instance of MIMEBase
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p) # attach the instance 'p' to instance 'msg'
s = smtplib.SMTP('smtp.gmail.com', 587) # SMTP
s.starttls()
s.login(fromaddr, mdpfrom)
text = msg.as_string()
s.sendmail(from_email_addr, toaddr, text) # sending the email
s.quit() # terminating the session
And I get this error:
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials c12-20020aa7d60c000000b0042be14040c1sm2612116edr.86 - gsmtp')
To fix this problem, I think that the only line that need to be change is this one:
s = smtplib.SMTP('smtp.gmail.com', 587)
If you know by what I can change it or if you see any other error, it will help me a lot! :-)
Here is a more precise answer with all the main steps. I hope it will help other people.
Log in into your email account: https://myaccount.google.com
Then go to the security part
Be sure that you have turn on two steps verification and click on "App password"
After select email and the corresponding device
It will generate a password that will look like this; it is this password that you have to use in your python script.
This password will appear here; in this place, you will have the option to erase it (so it will be no longer be useful to connect to your email account).
Hope it will help other people!
Solved it by creating App password. You must got to Google account. Security tab, active 2 Step Verification. After this new option under "Signing in to Google" the "App passwords" option will be actived. Just create one app password and use as password to authenticate
I'm working in a company that uses Gmail to send and receive emails from a domain email. To make it clearer, let's say it's firstname#company.com.
Right now my goal is to make a simple script that will send the emails using a list from csv file. The code works fine when I use my personal Gmail email, but the problem starts when I change it to my business account - it seems that the script is ignoring the initialization step because it's not #gmail.com.
Not sure if it's needed, but right now I will be happy to at least run the "yagmail 101" code like the one below. Just for reference, I tried the smtplib as well with same result. The two-factor authentication is on, a 16-char password for Windows mail application was created.
#importing the Yagmail library
import yagmail
try:
#initializing the server connection
yag = yagmail.SMTP(user='firstname#company.com', password='mypassword')
#sending the email
yag.send(to='recipient_username#gmail.com', subject='Testing Yagmail', contents='Hurray, it worked!')
print("Email sent successfully")
except:
print("Error, email was not sent")
#importing the Yagmail library
import yagmail
# connect to smtp server.
yag_smtp_connection = yagmail.SMTP( user="user#company.com", password="mypassword",
host='yourSMTPserver')
# email subject
subject = 'Hello'
contents = ['Hello this email send via YAGMAIL']
# send the email
yag_smtp_connection.send('to#gmail.com', subject, contents)
print("Email send!")
I am creating an endpoint where a user can get sent an email with a code to reset their password with.
When sending the email, I am using the built in smtplib that comes bundled with python.
When a request is made to the endpoint, I want the sender name to appear instead of the raw email. How can I accomplish this?
Here's my code:
code = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
content = 'Subject: {}\n\n{}'.format('Your code to reset your password:', 'Your Code: {}'.format(code))
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login(os.environ.get('email'), os.environ.get('password'))
mail.sendmail(os.environ.get('email'), user.email, content)
mail.close()
When searching the web for similar questions, the only solution that came across was to add this line of code: content['From'] = "Your name "
Adding this line of code didn't send the email at all.
Does anybody know how to add the sender name to the email? Thank You.
Hello I am fairly new to python and stumbled upon this cool feauture that with only a few lines of code, python can send emails.
I am currently not sure if the code that I have below works or if its something on my end? Since I am totally new to this I have no way to test if I am on the right track or not.
When running the code, it compiles with no problem but I never receive the messages.
Also, I am sending emails to myself, so I should see them rather quickly.
Here is my Outlook code:
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'myemail#hotmail.com'
mail.Subject = 'Hello this is you!
mail.Body = 'Hello!!!!!!'
mail.HTMLBody = '<h2>This is an H2 message</h2>' #this field is optional
# To attach a file to the email (optional):
attachment = "C:/Users/OneDrive/Documents/Desktop/Social_Network_Ads.csv"
mail.Attachments.Add(attachment)
mail.Send()
Here is my Gmail Code:
import smtplib
fromaddr = 'myemail#gmail.com'
toaddrs = 'myemail#gmail.com'
msg = 'There was a terrible error that occured and I wanted you to know!'
# Credentials (if needed)
username = '###username###'
password = '###password###'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Please let me know why I am not receiving the email or why its showing as not sent?
EDIT:
I am connected to my local hotmail account and I am logged into Gmail, so I am hopping its not a connection issue.
When I go to check my sent folder nothing seems to have been sent