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

I have a python-Script which sends E-Mails to my main account which I use as a notification method for birthdays.
Now I want to expand on that by adding an attachment (image).

Also, when possible, I like to send images from a pool of 5 pictures. Every time the script is started it should send one random image but without repeating them. When the script has been executed 5 times (all 5 images have been sent) it should reset and sent the images again (but in a new random order).
How can you do so?

This is my script so far:

#!/usr/bin/env python3

import smtplib
import time

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Make Random Choices the same [SOLVED] AlphaInc 6 1,940 May-17-2022, 01:43 PM
Last Post: AlphaInc
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,171 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,394 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,727 Aug-03-2020, 08:34 AM
Last Post: buran
  TimeOutError when trying to initiate smtplib.SMTP thecosmos 0 3,345 Jun-19-2020, 05:30 AM
Last Post: thecosmos
  SMTPlib help tpolim008 4 3,840 Apr-27-2020, 11:11 PM
Last Post: tpolim008
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,664 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 4,157 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383
  smtplib mail without subject anna 2 2,480 Apr-24-2019, 05:44 AM
Last Post: anna
  Smtplib: What does context argument means? Pythenx 1 3,093 Mar-27-2019, 06:25 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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