I have the following code:
import smtplib, ssl
def send_email(temperature):
port = 465 # For SSL
password = "my_password"
sender_email = "my_sender#gmail.com"
receiver_email = "my_receiver#x.y"
message = """\
Subject: Temperature is %0.2f degrees C
""" % temperature
server = smtplib.SMTP_SSL("smtp.gmail.com", port)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
if __name__ == "__main__":
send_email(7.7)
After the first run, the first message was received OK.
Next messages were in a spam folder without sender address, subject and body. I tried to mark it
as not spam, but it didn't help.
The message headers have the correct sender address, subject and body.
Can I correct it somehow?
I solved it as follows:
import smtplib, ssl
from email.mime.text import MIMEText
def send_email(temperature):
port = 465 # For SSL
password = "my_password"
sender_email = "my_sender_address#gmail.com"
receiver_email = "my_receiver_address.x.y"
message = MIMEText("Temperature is %0.2f degrees" % temperature)
message['Subject'] = "%0.2f degrees" % temperature
message['From'] = sender_email
message['To'] = receiver_email
server = smtplib.SMTP_SSL("smtp.gmail.com", port)
server.login(sender_email, password)
server.sendmail(sender_email, [receiver_email], message.as_string())
server.quit()
if __name__ == "__main__":
send_email(7.7)
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?
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..")
Trying to make the script below work for office365. Sends out an email but I cannot get the script to recognize the actual email text body (only the Subject line being sent). Below script worked for gmail. Any ideas where I need to modify?
Thanks!
import smtplib, ssl
port = 587
smtp_server = "smtp.office365.com"
sender_email = "me#email.com"
receiver_email = {'User1': 'user1#email.com'}
password = "password"
subject = input('Enter the subject line: ')
message = input('Enter the message: ')
email = """\
Subject: %s
%s
""" % (subject, message)
for key, value in receiver_email.items():
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, value, email)
server.quit()
was missing a "\n" in the email object. It now works.
email = """\
Subject: %s\n
%s
""" % (subject, message)
I am trying to write a module as part of my code to send out email. I have this code down which does not throw any exception but it does not deliver email as I expect. Can anyone help me point out any issue this code might have? Thanks in advance!
""" before sending email with this code
I start smtp server:
python -m smtpd -n -c DebuggingServer localhost:1025
"""
#!/usr/bin/python -tt
from email.mime.text import MIMEText
from datetime import date
import smtplib
SMTP_SERVER = "localhost"
SMTP_PORT = 1025
EMAIL_TO = ["user385#dispostable.com"]
EMAIL_FROM = "user383#testdomain.com"
EMAIL_SUBJECT = "*Email Test*"
DATE_FORMAT = "%d/%m/%Y"
EMAIL_SPACE = ", "
DATA='Test email sending feature in Python'
def send_email():
msg = MIMEText(DATA)
msg['Subject'] = EMAIL_SUBJECT + " %s" %(date.today().strftime(DATE_FORMAT))
msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
msg['From'] = EMAIL_FROM
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
mail.quit()
if __name__=='__main__':
try:
send_email()
except Exception as e:
import traceback;traceback.print_exc()
Thanks
hi can you remove the try except and run it again?
since youre using a gmail. can you try this one
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("user383#gmail.com", "your-password-here")
msg = "YOUR MESSAGE!"
server.sendmail("user383#gmail.com", "user385#dispostable.com", msg)
server.quit()
I can make smtplib send to other email addresses, but for some reason it is not delivering to my phone.
import smtplib
msg = 'test'
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login("<username>","<password>")
server.sendmail(username, "<number>#vtext.com", msg)
server.quit()
The message sends successfully when the address is a gmail account, and sending a message to the phone using the native gmail interface works perfectly. What is different with SMS message numbers?
Note: using set_debuglevel() I can tell that smtplib believes the message to be successful, so I am fairly confident the discrepancy has something to do with the behavior of vtext numbers.
The email is being rejected because it doesn't look an email (there aren't any To From or Subject fields)
This works:
import smtplib
username = "account#gmail.com"
password = "password"
vtext = "1112223333#vtext.com"
message = "this is the message to be sent"
msg = """From: %s
To: %s
Subject: text-message
%s""" % (username, vtext, message)
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(username,password)
server.sendmail(username, vtext, msg)
server.quit()
The accepted answer didn't work for me with Python 3.3.3. I had to use MIMEText also:
import smtplib
from email.mime.text import MIMEText
username = "account#gmail.com"
password = "password"
vtext = "1112223333#vtext.com"
message = "this is the message to be sent"
msg = MIMEText("""From: %s
To: %s
Subject: text-message
%s""" % (username, vtext, message))
server = smtplib.SMTP('smtp.gmail.com',587)
# server.starttls()
server.login(username,password)
server.sendmail(username, vtext, msg.as_string())
server.quit()