My email is: sarahconnor#gmail.com
I would like to send an email from johnconnor#skynet.com to mine.
I don't want to login remotely to my gmail account to send an email.
This is what I need:
I'm Sarah Connor, and I want to receive in my mailbox ( sarahconnor#gmail.com ), an e-mail from johnconnor#skynet.com ...
So I've use this script to do so:
import requests
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
COMMASPACE = ', '
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
fromm = "johnconnor#skynet.com"
to = "sarahconnor#gmail.com"
msg['From'] = fromm
msg['To'] = COMMASPACE.join(to)
msg.preamble = 'Our family reunion'
requests.get("http://smtp.gmail.com", verify=False)
s = smtplib.SMTP_SSL("smtp.gmail.com", 587)
s.starttls()
s.login('sarahconnor#gmail.com', 'mypassword12345') #here I login to the SMTP server from Google to be able to send emails...
s.sendmail(fromm, to, msg.as_string())
s.close()
I have the following error:
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))
And as seen from here, it doesn't seem that I'm having any of that issues.
Does anyone knows how can I fix this?
Thanks
this sends a mail from a gmail account to any other account
import smtplib
from email.mime.text import MIMEText
class GmailHandler():
"""
IMPORTANT NOTE:
in order to access a gmail account with this handler,
your account needs 'foreign-access' enabled (follow these steps):
login to the account
go here--> https://accounts.google.com/b/0/DisplayUnlockCaptcha
press 'Continue'
Done.
"""
def __init__(self, gmail, password):
self.gmail = gmail
self.password = password
def send_mail(self, receivers, subject, text):
if not isinstance(receivers, list):
receivers = [receivers]
# Send the message via our own SMTP server, but don't include the envelope header
smtp = smtplib.SMTP("smtp.gmail.com", 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(self.gmail, self.password)
for receiver in receivers:
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = self.gmail
msg['To'] = receiver
smtp.sendmail(self.gmail, receiver, str(msg))
smtp.quit()
Use smtplib.SMTP instead of smtplib.SMTP_SSL.
Related
I'm using smtplib library to send emails from python script.
import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def sendmail(input, topic, receiver_email):
# definitions for setup
sender_email = "EMAIL"
port = 465 # For SSL
smtp_server = "SERVER"
login = "APIKEY" # Enter your login
password = 'PASSWORD'
# set message parameters
message = MIMEMultipart("alternative")
message["Subject"] = topic
message["From"] = sender_email
message["To"] = receiver_email
part1 = MIMEText(input, "plain")
message.attach(part1)
# part2 = MIMEText(html, "html")
# message.attach(part2)
# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(login, password)
server.sendmail(sender_email, receiver_email, message.as_string())
I've created email certificate. It is stored in .pfx file. Is there any library or way to sign emails with this certificate?
Using python I want to send email from my app but it shows the error
SMTP AUTH extension not supported by server
Code for the program,
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "test1#example.com"
toaddr = "test2#example.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test Mail"
body = "Test mail from python"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.example.com', 25)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Telnet Output:
ehlo test1.example.com
250-hidden
250-HELP
250-SIZE 104857600
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-STARTTLS
250 OK
I need to authenticate and send mail from app.
a connection is required before login and sendemail.
server = smtplib.SMTP('smtp.example.com', 25)
server.connect("smtp.example.com",465)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
There is no need to call smtp.connect() and smtp.ehlo(), because they are called automatically by SMTP() and smtp.starttls(). The issue is solved simply setting port to 587 instead of 28.
For client use, if you don’t have any special requirements for your security policy, it is highly recommended that you use the create_default_context() function to create your SSL context. It will load the system’s trusted CA certificates, enable certificate validation and hostname checking, and try to choose reasonably secure protocol and cipher settings.
In general, you will want to use the email package’s features to construct an email message, which you can then send via send_message().
import smtplib, ssl
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content("The body of the email is here")
msg["Subject"] = "An Email Alert"
msg["From"] = "me#example.com"
msg["To"] = "you#example.com"
context=ssl.create_default_context()
with smtplib.SMTP("smtp.example.com", port=587) as smtp:
smtp.starttls(context=context)
smtp.login(msg["From"], "p#55w0rd")
smtp.send_message(msg)
It is probably just the server I was using, but was getting the same error as the OP even after implementing the accepted solution. Turned out the server did not want a login, so after deleting the line server.login(fromaddr, "password"), the error went away and it worked.
you need to 'starttls' before login.
import smtplib
s = smtplib.SMTP('smtplib.gmail.com',587)
s.ehlo()
s.starttls()
s.login('frmaddr','password')
try:
s.sendmail('fromaddr','toaddr','message')
except:
print (failed)
The first tap is enable your gmail account to send emails by another applications, allow in this section:
https://myaccount.google.com/lesssecureapps
Then, you should can to send an email
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
message = "This is an email"
password = "yourpasswordemailsender"
msg['From'] = "emailsender#gmail.com"
msg['To'] = "emailsended#gmail.com"
msg['Subject'] = "Title of email"
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
I am trying to send email from one email to another using smtplib and ssl. It's showing delivered, but there is no email at receiver end. I am trying two different codes:
Code 1:
import smtplib, ssl
port = 465
smtp_server = "myserver.com"
sender_email = "sender#myserver.com"
receiver_email = "receiver#gmail.com"
password = "mypassword"
message = """\
Subject: Hi there
This message is sent from Python."""
try:
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
print("Sent..")
except:
print("Error!!")
code 2:
import smtplib
import os.path
from email.mime.text import MIMEText
msg = MIMEText("")
msg['Subject'] = "Hi there, This message is sent from Python."
s = smtplib.SMTP_SSL("myserver.com:465")
s.login("sender#myserver.com","mypassword")
s.sendmail("sender#myserver.com","receiver#gmail.com", msg.as_string())
s.quit()
print("done")
Can not find the problem. Any idea?
The sendmail method is a low level method that assumes that the message text is correctly formatted. And your messages do not contain the usual To: and From: headers.
The send_message method uses an EmailMessage and formats it correctly, so it can be easier to use. I would advise to change your code to:
port = 465
smtp_server = "myserver.com"
password = "mypassword"
msg = MIMEText("This message is sent from Python.")
msg['Subject'] = "Hi there"
msg['From'] = "sender#myserver.com"
msg['To'] = "receiver#gmail.com"
try:
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.send_message(msg)
print("Sent..")
Using python I want to send email from my app but it shows the error
SMTP AUTH extension not supported by server
Code for the program,
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "test1#example.com"
toaddr = "test2#example.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test Mail"
body = "Test mail from python"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.example.com', 25)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Telnet Output:
ehlo test1.example.com
250-hidden
250-HELP
250-SIZE 104857600
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-STARTTLS
250 OK
I need to authenticate and send mail from app.
a connection is required before login and sendemail.
server = smtplib.SMTP('smtp.example.com', 25)
server.connect("smtp.example.com",465)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
There is no need to call smtp.connect() and smtp.ehlo(), because they are called automatically by SMTP() and smtp.starttls(). The issue is solved simply setting port to 587 instead of 28.
For client use, if you don’t have any special requirements for your security policy, it is highly recommended that you use the create_default_context() function to create your SSL context. It will load the system’s trusted CA certificates, enable certificate validation and hostname checking, and try to choose reasonably secure protocol and cipher settings.
In general, you will want to use the email package’s features to construct an email message, which you can then send via send_message().
import smtplib, ssl
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content("The body of the email is here")
msg["Subject"] = "An Email Alert"
msg["From"] = "me#example.com"
msg["To"] = "you#example.com"
context=ssl.create_default_context()
with smtplib.SMTP("smtp.example.com", port=587) as smtp:
smtp.starttls(context=context)
smtp.login(msg["From"], "p#55w0rd")
smtp.send_message(msg)
It is probably just the server I was using, but was getting the same error as the OP even after implementing the accepted solution. Turned out the server did not want a login, so after deleting the line server.login(fromaddr, "password"), the error went away and it worked.
you need to 'starttls' before login.
import smtplib
s = smtplib.SMTP('smtplib.gmail.com',587)
s.ehlo()
s.starttls()
s.login('frmaddr','password')
try:
s.sendmail('fromaddr','toaddr','message')
except:
print (failed)
The first tap is enable your gmail account to send emails by another applications, allow in this section:
https://myaccount.google.com/lesssecureapps
Then, you should can to send an email
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
message = "This is an email"
password = "yourpasswordemailsender"
msg['From'] = "emailsender#gmail.com"
msg['To'] = "emailsended#gmail.com"
msg['Subject'] = "Title of email"
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
I'm trying to make an email script in python. Here's what I have (from pythonlibrary.org):
#! /usr/bin/env python
import smtplib
import string
SUBJECT = "An email!"
TO = "me#icloud.com"
FROM = "me#gmail.com"
text = "This text is the contents of an email!"
BODY = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"",
text
), "\r\n")
server = smtplib.SMTP('smtp.gmail.com')
server.login('me#gmail.com', 'mypassword') # Not very secure, I know, but this email is dedicated to this script
server.sendmail(FROM, [TO], BODY)
server.quit()
I get smtplib.SMTPException: SMTP AUTH extension not supported by server. Is this is so, then why does smtp.gmail.com respond at all? Is this a problem with Gmail, or my script, or something else?
Error message:
Traceback (most recent call last):
File "/Users/student/Desktop/mail.py", line 18, in <module>
server.login('*******#gmail.com', '**************')
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 552, in login
smtplib.SMTPException: SMTP AUTH extension not supported by server.
You need to contact the gmail mail server on the submission port (587), not the default 25:
server = smtplib.SMTP('smtp.gmail.com', 587)
You also need to use server.starttls() before logging in (so that your password is not sent in the clear!). This is from a script I have and it works for me:
server = smtplib.SMTP()
server.connect("smtp.gmail.com", "submission")
server.starttls()
server.ehlo()
server.login(user, password)
Here's how to send e-mail using gmail in Python:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from email.header import Header
from email.mime.text import MIMEText
from getpass import getpass
from smtplib import SMTP_SSL
login, password = 'user#gmail.com', getpass('Gmail password:')
# create message
msg = MIMEText('message body…', _charset='utf-8')
msg['Subject'] = Header('subject…', 'utf-8')
msg['From'] = login
msg['To'] = login
# send it via gmail
s = SMTP_SSL('smtp.gmail.com', 465, timeout=10)
s.set_debuglevel(1)
try:
s.login(login, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())
finally:
s.quit()
I found I had to do an ehlo() and a starttls() before sending mail via gmail:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(SERVER_EMAIL,EMAIL_HOST_PASSWORD)
It shouldn't make a difference with the login, but I use a MIMEMultipart from email.mime.multipart for the body, with something like:
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = mFrom
msg['To'] = mTo
if textBody:
part1 = MIMEText(textBody, 'plain')
msg.attach(part1)
if htmlBody:
part2 = MIMEText(htmlBody, 'html')
msg.attach(part2)
BODY = msg.as_string()