I'm sending email through an account with the following Python code:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def sendMail(target, subject, txt):
fromaddr = 'my#test.com'
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = target
msg['Subject'] = subject
msg.attach(MIMEText("This is my text"))
server = smtplib.SMTP('node01.mailserver.com', '587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, 'mypassword')
server.sendmail(fromaddr, target, msg.as_string())
server.quit()
This works quite well and I can receive the emails.
However, the timestamp which is displayed in my email client shows the time I downloaded the mail from the server and not time the email was actually send.
Is there a way how to correctly add the sending time to the email? I would assume that the sending time is not correctly set and that's the reason why the download time is displayed?
Or do I make other mistakes?
Thanks!
This works for me:
...
import email.utils
...
msg['Date'] = email.utils.formatdate(localtime=True)
...
Related
I'm sending emails with Python, but the msg["Subject"] variable populates the body of the email instead of the subject box, and the variable body, populates nothing...
Everything else works fine, but I can't figure out why the subject is the body and the body is empty?
What have I missed?
Here's the code:
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
msg['From'] = "myemail#gmail.com"
msg['To'] = 'anemail#hotmail.com'
msg['Subject'] = "for next delivery, please supply"
body = Merged_Dp_Ind_str
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('username#gmail.com', 'password1')
server.sendmail(msg['From'], msg['To'], msg['Subject'])
server.quit()
screenshot of the inbox
Your message is fine, but you are not actually sending it; you are only sending the Subject.
server.sendmail(msg['From'], msg['To'], msg['Subject'])
You apparently mean
server.sendmail(msg['From'], msg['To'], text)
However, you should probably update your code to use the modern Python 3.6+ APIs instead.
The proper modern way to format and send the message is something like
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['From'] = "myemail#gmail.com"
msg['To'] = 'anemail#hotmail.com'
msg['Subject'] = "for next delivery, please supply"
msg.set_content(Merged_Dp_Ind_str)
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login('username#gmail.com', 'password1')
server.send_message(msg)
server.quit()
Im using python 2.7
Im trying to send emails to more than one person. Only one person receives not others.
My code is;
import smtplib
import time
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from utilities.ConfigReader import *
def sendEmailNotification(subject, body):
sender, receiver = getNotificationSettings()
smtpServer, smtpPort, timeout = getSMTPSettings()
msg = MIMEMultipart()
R = receiver.split(",")
body = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
msg.attach(body)
server = smtplib.SMTP(smtpServer, smtpPort)
server.ehlo()
try:
print receiver
print R
server.sendmail(sender, R, msg.as_string())
except smtplib.SMTPException:
time.sleep(float(timeout))
server.sendmail(sender, R, msg.as_string())
server.quit()
sendEmailNotification("Test","Test")
Here R prints;
['test#lob.com', 'ratha#lob.com']
receiver prints;
test#lob.com, ratha#lob.com
I followed following thread but didnt work to me;
How to send email to multiple recipients using python smtplib?
What im doing wrong here?
I figured out my issue. ratha#lob.com is in the list email test#lob.com . So I haven't received the email for ratha#lob.com , but received for test#lob.com . After changing two private emails, i receive in both emails. So code is working as expected.
I'm trying to send an email in Python
It's working without problem with gmail with this code :
import smtplib
sender = 'xxx#xxx'
receivers = ['xxx#gmail.com']
message = "hello"
try:
smtpObj = smtplib.SMTP('smtp.gmail.com:587')
smtpObj.starttls()
smtpObj.login('xxxx#gmail.com', 'my_password')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
print("okay")
except:
print("notokay")
But when i use it with office 365, the email is send but the message is empty.
It's the same code but with 'smtp.office365.com:587' with my correct login and password.
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('hello')
msg['Subject'] = 'Urgent message'
msg['From'] = 'xxx#xxx'
msg['To'] = 'xxx#gmail.com'
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('xxx#gmail.com', 'my_password')
s.sendmail('xxx#xxx', 'xxx#gmail.com', msg.as_string())
s.quit()
Try the following, it might be because you need to create a MIMEText context for the formatting to be accepted by office365.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send Email Attachments with python
I would like to edit the following code and send an email with an attachment. Attachment is a pdf file, it is under /home/myuser/sample.pdf, in linux environment. What should I change below?
import smtplib
fromaddr = 'myemail#gmail.com'
toaddrs = 'youremail#gmail.com'
msg = 'Hello'
# Credentials (if needed)
username = 'myemail'
password = 'yyyyyy'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
You create a message with an email package in this case -
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg.attach(MIMEText(open("/home/myuser/sample.pdf").read()))
and then send the message.
import smtplib
mailer = smtplib.SMTP()
mailer.connect()
mailer.sendmail(from_, to, msg.as_string())
mailer.close()
Several examples here - http://docs.python.org/library/email-examples.html
UPDATE
Updating the link since the above yields a 404 https://docs.python.org/2/library/email-examples.html. Thanks #Tshirtman
Update2: Simplest way to attach pdf
To attach the pdf use the pdf flag:
def send_email_pdf_figs(path_to_pdf, subject, message, destination, password_path=None):
## credits: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script
from socket import gethostname
#import email
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import json
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
with open(password_path) as f:
config = json.load(f)
server.login('me#gmail.com', config['password'])
# Craft message (obj)
msg = MIMEMultipart()
message = f'{message}\nSend from Hostname: {gethostname()}'
msg['Subject'] = subject
msg['From'] = 'me#gmail.com'
msg['To'] = destination
# Insert the text to the msg going by e-mail
msg.attach(MIMEText(message, "plain"))
# Attach the pdf to the msg going by e-mail
with open(path_to_pdf, "rb") as f:
#attach = email.mime.application.MIMEApplication(f.read(),_subtype="pdf")
attach = MIMEApplication(f.read(),_subtype="pdf")
attach.add_header('Content-Disposition','attachment',filename=str(path_to_pdf))
msg.attach(attach)
# send msg
server.send_message(msg)
inspirations/credits to: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script
The recommended way is using Python's email module in order to compose a properly
formatted MIME messages. See docs
For python 2
https://docs.python.org/2/library/email-examples.html
For python 3
https://docs.python.org/3/library/email.examples.html
I've tried sending mail with python from gmail and it works fine. But the problem is when I created the Mail class with one method to whom I send specific string from my code, it can't be send.
class Mail:
def send_mail(self, msg):
import smtplib
fromaddr = 'something#something.com'
toaddrs = 'something#gmail.com'
msg = msg + "something"
print msg
username = 'something'
password = 'something'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
This way it sends mail but the only thing in mail is "something" that I added to string, and print msg outputs the whole string plus "something". What could be the problem?
This is the whole class for now, and it's called
mail = Mail()
mail.send_mail(message)
I don't know what was the problem, but I managed to send it using MIME that's already in python
So here is the code that works:
def send_mail(self, message):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
gmailUser = 'something#gmail.com'
gmailPassword = 'something'
recipient = 'something#something.com'
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = "Subject"
msg.attach(MIMEText(message))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
The problem is most likely that the content of the message seems to requite an extra line break between the addresses and the body of the message. The solution given by iblazevic is a much more readable way of doing it anyway though.