Not able to send the mail without password authentication in python - python-3.x

I am not able to send the mail without password authentication in python.Its printing the last message but mail is not sent.Below is the code that i am using.Anyone please help i am stuck because of this. Thanks in advance.
import smtplib
fromaddr = 'xyz#gmail.com'
toaddrs = ['abc#gmail.com']
msg = '''
From: {fromaddr}
To: {toaddr}
Subject: 'testing'
This is a test
.
'''
msg = msg.format(fromaddr =fromaddr, toaddr = toaddrs[0])
server = smtplib.SMTP("gmail-smtp-in.l.google.com:25")
server.starttls()
server.ehlo("example.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit/close()
print("mail sent")

Related

Sending email via smtp only works if sender and recipient are the same v.v

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
user = 'user#icloud.com'
password = 'apppassword'
host = 'smtp.mail.me.com'
text = 'E-Mail Body'
subject = 'This is a Subject'
to_emails = ['user2#icloud.com']
from_email = 'John Doe <user#icloud.com>'
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = 'user2#icloud.com'
msg['Subject'] = subject
body = MIMEText(text, 'plain')
msg.attach(body)
msg_str = msg.as_string()
server = smtplib.SMTP(host, port=587)
server.starttls()
server.ehlo()
server.login(user, password)
server.sendmail(from_email, to_emails, msg_str)
server.quit()
I am trying to program an email client in python.
At the moment I fail to send via iCloud.
The email only arrives if I send the email to the iCloud mail that sends it. So to myself.
So you could say it's the loneliest email client in the world right now.
But all kidding aside, I don't get it. Is it me? Is it iCloud?

How to send an image by gmail 2022

I have a python script that detects motion in teh camera and then sends an email to the supplied email.
The issue is that Gmail has changed the way it allows apps to send external emails.
How can I change my code to properly send an email with an attached image?
Here is my code:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# Email you want to send the update from (only works with gmail)
fromEmail = 'some_email#gmail.com'
# You can generate an app password here to avoid storing your password in plain text
# https://support.google.com/accounts/answer/185833?hl=en
fromEmailPassword = 'somepassword'
# Email you want to send the update to
toEmail = 'some_email#gmail.com'
smtp_port = 587 # Standard secure SMTP port
smtp_server = "smtp.gmail.com" # Google SMTP Server
TIE_server = smtplib.SMTP(smtp_server, smtp_port)
def sendEmail(image):
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Security Update'
msgRoot['From'] = fromEmail
msgRoot['To'] = toEmail
msgRoot.preamble = 'camera update'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('Smart security cam found object')
msgAlternative.attach(msgText)
msgText = MIMEText('<img src="cid:image1">', 'html')
msgAlternative.attach(msgText)
msgImage = MIMEImage(image)
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
zsmtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.starttls()
smtp.login(fromEmail, fromEmailPassword)
smtp.sendmail(fromEmail, toEmail, msgRoot.as_string())
smtp.quit()
Thanks.

How to send an email without login to server in Python

I want to send an email without login to server in Python. I am using Python 3.6.
I tried some code but received an error. Here is my Code :
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer)
server.set_debuglevel(1)
server.sendmail(fromAddr, toAddr, text)
server.quit()
I expect the mail should be sent without asking user id and password but getting an error :
"smtplib.SMTPSenderRefused: (530, b'5.7.1 Client was not authenticated', 'from#Address.com')"
I am using like this. It's work to me in my private SMTP server.
import smtplib
host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "testpython#test.com"
TO = "bla#test.com"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)
server.quit()
print ("Email Send")
import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='To address'
mail.Subject='Message subject'
mail.Body='Message body'
mail.HTMLBody='<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment="Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
The code below worked for me.
First, I opened/enabled Port 25 through Network Team and used it in the program.
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()
First, you have to have a SMTP server to send an email. When you don't have one, usually outlook's server is used. But outlook only accepts authenticated users, so if you don't want to login into the server, you have to pick a server that doesn't need authentication.
A second approach is to setup an internal SMTP server. After you setup the internal SMTP server, you can use the "localhost" as the server to send the email. Like this:
import smtplib
receiver = 'someonesEmail#hisDomain.com'
sender = 'yourEmail#yourDomain.com'
smtp = smtplib.SMTP('localhost')
subject = 'test'
body = 'testing plain text message'
msg = 'subject: ' + subject + ' \n\n' + body
smtp.sendmail('sender', receiver, msg)

Error in email-sending program in Python with smtplib

Here is my code:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("MyEmail", "pass")
msg = "YOUR MESSAGE!"
server.sendmail("MyEmail", "MyEmail", msg)
server.quit()
The error message is the following:
Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14
What is the solution to my problem?
You need permissions, go to this link https://www.google.com/settings/security/lesssecureapps and allow the access

sending mail from python in 2018

I know there are a lots of how to send mail questions before, but does sending mail from python scripts not work anymore? I've tried to use the SMTP lib with the following script:
import smtplib
fromaddr = 'mymail#gmail.com'
toaddr = 'someones#gmail.com'
msg = "\r\n".join([
"From: mymail#gmail.com",
"To: someonesmail#gmail.com",
"Subject: Just a message",
"",
'Why, oh why?'
])
username = 'mymail#gmail.com'
pwd = 'mypassword'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, pwd)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
But it says to login via a web browser, and this post says that google now uses an api to send mails. So, having confusion about the possibility to send mails via scripts without using any of google's api, Is it possible?

Resources