Python Forum
Sending Attachments via smtplib [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Attachments via smtplib [SOLVED]
#1
Hello everybody,

I have an E-Mail script written in python which sends notifications to my personal mail account.
Now I want to extend on that by adding an attachment (an image, the format doesn't matter) but I don't know how to do so.
Below you can see my code.

#!/usr/bin/env python3

import smtplib

f1 = open("../index/mail.txt","r")
mail = f1.read() [:-1]
f1.close()

f2 = open("../index/passwd.txt","r")
passwd = f2.read() [:-1]
f2.close()

f3 = open("../index/receiver.txt","r")
receiver = f3.read() [:-1]
f3.close()

#Email Variables
SMTP_SERVER = 'smtp.gmail.com'  #Email Server (don't change!)
SMTP_PORT = 587                 #Server Port (don't change!)
GMAIL_USERNAME = mail           #change this to match your gmail account
GMAIL_PASSWORD = passwd         #change this to match your gmail password
 
class Emailer:
    def sendmail(self, recipient, subject, content):
 
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
 
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
 
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
 
        #Send Email & Exit
        msg = (headers + "\r\n\r\n" + content).encode('utf-8')
        session.sendmail(GMAIL_USERNAME, recipient, msg)
        session.quit
 
sender = Emailer()
 
sendTo = receiver
 
emailSubject = "Subject"
emailContent = "Content"
 
#Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and "emailConten$
sender.sendmail(sendTo, emailSubject, emailContent)
Reply
#2
see: https://stackoverflow.com/a/13071761
Reply
#3
If use yagmail it's easier.
Quick test works fine for me.
import yagmail

receiver = "[email protected]"
body = "Hello there from Yagmail"
filename = "test.pdf"

yag = yagmail.SMTP("[email protected]", pass-xxxx)
yag.send(
    to=receiver,
    subject="Python test with attachment",
    contents=body, 
    attachments=filename,
)
Reply
#4
(Oct-27-2021, 11:35 PM)snippsat Wrote: If use yagmail it's easier.
Quick test works fine for me.
import yagmail

receiver = "[email protected]"
body = "Hello there from Yagmail"
filename = "test.pdf"

yag = yagmail.SMTP("[email protected]", pass-xxxx)
yag.send(
    to=receiver,
    subject="Python test with attachment",
    contents=body, 
    attachments=filename,
)

That worked fine, thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code to send attachments contained on the drive. stefanoste78 1 822 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  I get attachment paperclip on email without any attachments monika_v 5 1,914 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  Unable to send email attachments cosmarchy 7 2,444 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,661 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,346 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,680 Aug-03-2020, 08:34 AM
Last Post: buran
  TimeOutError when trying to initiate smtplib.SMTP thecosmos 0 3,295 Jun-19-2020, 05:30 AM
Last Post: thecosmos
  SMTPlib help tpolim008 4 3,735 Apr-27-2020, 11:11 PM
Last Post: tpolim008
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,612 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 4,110 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020