Python Forum
My journey with attaching file instead of images using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My journey with attaching file instead of images using Python
#1
Hi everyone. To introduce myself... I'm new here and signed up today.

I have been learning Python for 2 weeks now, and it hasn't been an easy learning curve.

I'm learning Python to help automate office and admin tasks. Not to become a programmer, but am eager to actually GET it rather than just intellectually understand it.


Right now, I have managed to successfully piece together code which helps me attach more than one image using Python. I learned the difference between sending 1 and more than one image...

My problem is this:

I would like to send multiple attachments (using Microsoft Word & Excel files) using Python.

My limited understanding is that files and images in Python each have different requirements in terms of code.

I humbly admit I had to refer to tutorials and Python documentation, in order to help me piece together certain missing information (I find it hard to remember and apply code if it is not immediately useful or the examples are not related to my own project).

I would like to ask you, can you please look through my code and help me via provided examples, what code would be most useful, where and what imports I need to do that may be different to attaching images?

I know there is documentation online about this, it's simply that I have found this provided code works for me and I understand it and want to stick to it as a beginner, and the vast majority of times I try to replicate code and piece it from multiple sources, it most often doesn't work and I end up in circles. (I don't know if that's most people or just me).

I would appreciate examples and how I can learn to do this for myself.

Enough chatter, here is the code I made below (Any requested info, I will provide. Thank you!):




import os
import smtplib
from email.message import EmailMessage

#This helps us upload more than one image via Python
import imghdr



#Notice we are using Windows "Enviornment Variables", in order to hide our sensitive data
Email_User = os.environ.get("Andree_Email")

Email_Password = os.environ.get("Andree_Pass")

msg = EmailMessage()
msg['Subject'] = "Send candy"
msg['From'] = Email_User
msg['To'] = Email_User
msg.set_content("Can you send me some candy from Florida?")


files = ["c:\\Users\\Matti\\Desktop\\Bot External API.png","c:\\Users\\Matti\\Desktop\\Parameters.png"]

for file in files:

    with open(file, "rb") as f:
        file_data = f.read()
        file_type = imghdr.what(f.name)
        file_name = f.name

    msg.add_attachment(file_data, maintype = "image", subtype = file_type, filename = file_name)



#587 is standard internet server connection, but 465 is with SSL encrypted connection
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
    smtp.login(Email_User, Email_Password)



    smtp.send_message(msg)
Reply
#2
Below is exceprt of code I have used to send email with an attachment (several years ago). Hope it helps.

from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate

smtp = smtplib.SMTP_SSL('your_server', 465)
smtp.login('login', 'paswd')
msg = MIMEMultipart()
msg['Date'] = formatdate(localtime=True)
msg.attach(MIMEText(letter, "plain", "utf-8"))
msg["Subject"] = subject
msg["To"] = tomail
msg["From"] = frommail
        
with open('your_image.jpg', "rb") as fil:
    part = MIMEApplication(fil.read(), Name='your_image.jpg')

part['Content-Disposition'] = 'attachment; filename="%s"' % ('your_image.jpg',)
msg.attach(part)
smtp.sendmail('[email protected]', [tomail], msg.as_string())
smtp.quit()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send Error when attaching files JamesAinsworth 1 588 Aug-07-2023, 11:10 PM
Last Post: deanhystad
  Creating file with images BobSmoss 1 1,382 Jan-08-2022, 08:46 PM
Last Post: snippsat
  Problems with inserting images into an Excel File FightingFarmer 2 3,404 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  how to read multispectral images on python noorasim 0 1,777 Feb-28-2021, 03:54 PM
Last Post: noorasim
  Unable to capture all images of a multipage TIFF file in a merge bendersbender 0 2,211 Nov-19-2020, 03:09 PM
Last Post: bendersbender
  How do I insert images in Python using gspread (or any other package)? ivansing23 0 2,263 Jul-27-2020, 01:26 PM
Last Post: ivansing23
  A more efficient way of comparing two images in a python vukan 0 2,019 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to compare two different size images in python and find corresponding pixel value squidsirymchenry 1 4,573 Feb-03-2020, 06:48 AM
Last Post: michael1789
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,637 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax
  Compare two images with Python is possible? Delemir78 3 4,793 Dec-17-2019, 09:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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