So I'm following a tutorial to send email in python, the problem is that it was written for python 2 not python 3(which is what I have). So here's what I'm trying to get an answer what is the module for email in python 3? the specific module I'm trying to get is is:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMETex
I also have a feelling that when I get down to this module there will be an error (haven't got there yet because of
the module above giving error
import smtp
Here is the script:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMETex
fromaddr = ("XXXXX#mchsi.com")
toaddr = ("XXXX#mchsi.com")
msg = MIMEMultipart
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = ("test")
body = ("This is a test sending email through python")
msg.attach(MIMEText(body, ('plain')))
import smptlib
server = smptlib.SMPT('mail.mchsi.com, 456')
server.login("XXXXX#mchsi.com", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
text = msg.as_string()
sender.sendmail(fromaddr, toaddr, text)
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
By the way here are some mistakes in your code:
fromaddr = "XXXXX#mchsi.com" # redundant parentheses
toaddr = "XXXX#mchsi.com" # redundant parentheses
msg = MIMEMultipart() # not redundant this time :)
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "test" # redundant parentheses
body = "This is a test sending email through python" # redundant parentheses
msg.attach(MIMEText(body, 'plain')) # redundant parentheses
import smtplib # SMTP! NOT SMPT!!
server = smtplib.SMTP('mail.mchsi.com', 456) # `port` is an integer
server.login("XXXXX#mchsi.com", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX") # on most SMTP servers you should remove domain name(`#mchsi.com`) here
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text) # it's not `sender`
Related
I am trying to automate sending emails to a list of clients using a python script. It worked perfectly before they made it so you can't use the less secure app on 30th May 2022. I added the 2FA password and approved my computer. I am now receiving the error
server.send_message(message)
AttributeError: SMTP instance has no attribute 'send_message' when I try to run my code. Any ideas on how to fix this? I will attach the code below and have of course taken out sensitive information.
Python Code
MY_ADDRESS = "****#gmail.com" # Email Address
MY_PASSWORD = "****Password****" # Emails 2FA Pass
RECIPIENT_ADDRESS = ['****#gmail.com', '****#gmail.com'] # Recipient Address
HOST_ADDRESS = 'smtp.gmail.com'
HOST_PORT = 587
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
# Connection with the server
server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
server.starttls()
server.login(MY_ADDRESS, MY_PASSWORD)
# Creation of the MIMEMultipart Object
message = MIMEMultipart()
# Setup of MIMEMultipart Object Header
message['From'] = MY_ADDRESS
message['To'] = ", ".join(RECIPIENT_ADDRESS)
message['Subject'] = "Test Email - July"
# Creation of a MIMEText Part
textPart = MIMEText("Hello **** & ****,\n\nAttached below is your test bill for the month of July 2022. \n\nBest,\Your Management", 'plain')
# Creation of a MIMEApplication Part
filename = "Test Bill - Billy.pdf"
filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
filePart["Content-Disposition"] = 'attachment; filename="%s' % filename
# Parts attachment
message.attach(textPart)
message.attach(filePart)
# Send Email and close connection
server.send_message(message)
server.quit()
Try the following code:
import smtplib
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
body_part = MIMEText('Write you text here.')
user = 'xxxx#gmail.com'
password = 'xxxx'
msg['Subject'] = 'You subject'
msg['From'] = formataddr(('yyyyy', 'xxxx#gmail.com'))
msg['To'] = 'yyyy#gmail.com'
msg.attach(body_part)
smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_obj.login(user, password)
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_obj.quit()
Update: How to attach a file to your email:
Just add the following lines to the script above.
from email.mime.application import MIMEApplication
path = './../' #The path of your file.
with open(path + filename,'rb') as file:
msg.attach(MIMEApplication(file.read(), Name='filename'))
The email sends and shows that it sends to both listed recipients, but only the first listed one actually receives the message. Strange since I don't notice anything particularly wrong with how I entered the addresses in (based on other examples I came across), so I'm looking for another perspective on the issue.
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_user = 'myemail#gmail.com'
email_send = 'otheremail1#gmail.com, otheremail2#gmail.com'
subject = 'Test'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = """Hello,
This is a test.
Thanks!"""
msg.attach(MIMEText(body,'plain'))
filename='dataset.csv'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.office365.com',587)
server.starttls()
server.login(email_user,'password')
server.sendmail(email_user,email_send,text)
server.quit()
server.sendmail(email_user,email_send.split(','),text) and remove the space
Basically you only sent the first one and need to pass the other one.
I'm writing a little script to send emails and I would like to track who opened the email with the python module pytracking like so:
import smtplib
import pytracking
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "info#XXXXXXXXXX.com"
toaddr = "XXXXXXXXa#gmail.com"
msg = MIMEMultipart()
#Not sure if this is the right location for the pixel
pixel= (pixel_byte_string, mime_type) = pytracking.get_open_tracking_pixel()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "grande"
body = "bla bla bla"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('mail.XXXXXXXX.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('info#XXXXXXXX.com','XXXXXXXX')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print('DONE')
I read the full documentation of this module here , but I can not find from where to actually check if the pixel has been opend or which command shell I run to do so.
Can anyone advise how to check if the pixel has been opened?
Thanks
So I was working on an mail script to send mail to a list of users from text file. It works fine, iterating through the list, each user receives an email - except one detail, the subject is dropped from all emails except the last one sent by the script. What's happening here?
#!/usr/bin/env python
import sys
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
targets = open('targets.txt', "rb")
def mailme():
for line in targets:
msg = MIMEMultipart('alternative')
m_source = '<sender#address.com>'
m_target = '%s' % line
smtp_server = 'mail.server.com'
smtp_port = '25'
msg['From'] = m_source
msg['To'] = m_target
msg['Subject'] = "Subject"
smtp = SMTP()
smtp.set_debuglevel(0)
smtp.connect(smtp_server, smtp_port)
message_text = ('Some Text for %s' % (m_target)
)
message_html = ("""<body>
<h1>Some HTML for %s</h1>
</body>"""
) % (m_target)
txt = MIMEText(message_text, 'plain')
web = MIMEText(message_html, 'html')
msg.attach(txt)
msg.attach(web)
smtp.sendmail(m_source, m_target, msg.as_string())
smtp.quit()
if __name__ == "__main__":
mailme()
sys.exit(0)
I suggested OP to put msg['Subject'] before msg['From'] and it solves the problem. I suggested because the python MIME email examples/tutorials all have msg['Subject'] comes before msg['From'] and msg['To'].
I thought it is a template to follow. If anyone knows of why this approach solves the problem please shed some light on this.
I am trying to add multiple messages to email body but some how not able to do so.
#!/usr/intel/pkgs/python/2.7.2/bin/python
import sys
import argparse
import os
import time
import commands
import subprocess
import mmap
import sys
import argparse
import os.path
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
monty_python = "monty_python.txt"
if(os.path.getsize(monty_python) > 0) is True:
text = "\n\n Cool !! some info found"
part1 = MIMEText(text,'plain')
with open(monty_python , 'r') as input_data:
thestring = input_data.read()
montystring = thestring[thestring.find('---------Monty Python-----'):]
e_mailthis = montystring[montystring.find('\n')+1:]
monty_log = MIMEText(e_mailthis,'plain')
print monty_log
else:
text = "No info found"
part1 = MIMEText(text,'plain')
msg.attach(part1)
msg.attach(monty_log)
print ("msg is %s")%(msg)
me = "my_email"
you = "receiptant's email"
msg['Subject'] = 'Monty Python info'
msg['From'] = "me"
msg['To'] = "you"
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
text file monty_python.txt
---------Monty Python-----
line1
line2
line3
with above code i am able to get email message but content of monty_python.txt files which i am trying to parse comes as an attachement but i am trying to get is as a body of email.
Any suggestions/input ?
Try this, send email from xxx#gmail.com to yyy#gmail.com
import smtplib
from email.mime.text import MIMEText
def send_email(content):
msg = MIMEText(content)
msg['Subject'] = 'Title'
msg['From'] = "xxx#gmail.com"
msg['To'] = "yyy#gmail.com"
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login("xxx#gmail.com", "password")
s.sendmail("xxx#gmail.com", "yyy#gmail.com", msg.as_string())
s.close()