I am trying to send a gmail text to someone - python-3.x

so I tried to send it gave me this error
here is the error
Traceback (most recent call last):
File "C:\Users\23470\AppData\Local\Programs\Python\Python39\mess.py", line 6, in <module>
server = smtplib.SMTP(smtpServer,25)
File "C:\Users\23470\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 258, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (451, b'Request action aborted on MFE proxy, SMTP server is not
available.')
Here is my code
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='imranalubankudi#gmail.com'
toAddr='gmmeremnwanne#gmail.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()

Try changing port to 587. Also your smtpServer don't looks right for gmail.
server = smtplib.SMTP('smtp.gmail.com', 587)
You should also need to login to session.
Here's a simple function that sends a simple text mail through gmail.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_mail(send_from, send_to, subject, body):
#The mail addresses and password
sender_pass = 'your_gmail_password'
#Setup the MIME
message = MIMEMultipart()
message['From'] = send_from
message['To'] = send_to
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(send_from, sender_pass)
text = message.as_string()
session.sendmail(send_from, send_to, text)
session.quit()

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 do I avoid accumulating gmails with the same subject using SMTPLIB and Python

When I send files with the same subject using the code the emails accumulate in the inbox.
What I want to do is that these do not accumulate and are sent one by one with the same subject.
from email.mime.text import MIMEText
import smtplib
import smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
receiver = 'receiver'
sender = 'sender'
senders_password = 'password_application_sender'#This password is a password of application, this option activate in manager account of his gmail account
file = "path of file"
subject = 'subject' #This subject always is the same
mensaje = MIMEMultipart("alternative")
mensaje["Subject"] = subject
mensaje["From"] = sender
mensaje["To"] = receiver
html = f"""
<html>
<body>
Hi this is a email with a attachment
</body>
</html>
"""
body_html = MIMEText(html, "html")
mensaje.attach(body_html)
with open(file, "rb") as attachment:
content = MIMEBase("application","octet-stream")
content.set_payload(attachment.read())
encoders.encode_base64(content)
content.add_header(
"Content-Disposition",
f"attachment; filename= {file}",
)
mensaje.attach(content)
text = mensaje.as_string()
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender, senders_password)
server.sendmail(sender, receiver, text)
server.quit()
print("Email Send")
How I receive the email and How should it get

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 email through Python

#!/usr/bin/python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "sender mail id"
toaddr = "receiver mail id"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test"
body = "Test mail"
msg.attach(MIMEText(body, 'plain'))
filename = "foo.txt"
attachment = open(r"F:\python\foo.txt", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
this script is working fine when I running on my local system but on running on private network its giving me a error.
OSError: [Win Error 10051] A socket operation was attempted to an unreachable network.
You may want to look into if you have access from your private network to smtp.gmail.com on port 587
The private network admin may have setup the network to block outbound traffic on port 587
Try:
telnet smtp.gmail.com 587
If you get an unreachable network error its definitely a network issue.

Resources