0

The program works great but when the emails are sent they do not contain my Gmail signature. Is there anyway to do this using SMTP Lib. Here is my code

# Imports
import pandas as pd
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys
import os
import time
from rich.progress import track
import keyboard
import tkinter as tk
from tkinter import filedialog
from os import system
from colored import fg, attr

# User Input Process
sender_address = str(input("Please enter your email address!: "))
automail_art()
sender_pass = str(input(
    "Please enter your Google App Email password (No data is stored anywhere!): "))
automail_art()
user_sub = input("What would you like the subject line to say?: ")
automail_art()
user_greeting = input(
    "What would you like the greeting to be? (Hello, Greetings, etc.) [No symbols or names! JUST ONE WORD!]: ")
automail_art()
root = tk.Tk()
root.withdraw()
input(f"{fg('green_4')}Please choose the Excel File you would like to use for name and email address data. \nAfter selecting file click back into the terminal window. Press ENTER to continue\n\n")
# This prompts user to input the file path of their CSV file.
file_path = filedialog.askopenfilename()
automail_art()
input(f"{fg('yellow')}Please choose the Text File you would like to use for the body of your email. \nAfter selecting file click back into the terminal window. Press ENTER to continue\n\n")
# This prompts user to input the file path of their Text file.
txt_doc_path = filedialog.askopenfilename()
automail_art()
# PDF Process
input("Please choose the PDF File you would like to attach to your email. \nAfter selecting file click back into the terminal window. Press ENTER to continue\n\n")
pdf_path = filedialog.askopenfilename()
automail_art()
pdf_name = input(
    "What would you like to name the PDF file? (Do not inclue .pdf): ")
automail_art()
# Amount of emails the user wishes to send
amount = int(input("How many emails would you like to send? "))
automail_art()
txt_doc = open(txt_doc_path, "r")
txt_doc_contents = txt_doc.read()
# DataFrame Variable
# Variable made for DataFrame to save time and make it more simple
df = pd.read_excel(file_path, usecols=['First', 'MainEmail'])
# Email sending process
print("\n")
# Amount is how many emails the user wants to send out. "cell_value" is which cell from the spreadsheet it is pulling data from.
for cell_value in track(range(amount), description=f"{fg('green_1')}Sending Emails :) "):
    # "name_cell" is the cell that contains the first name of the recipient
    name_cell = df["First"].values[cell_value]
    # "email_cell" is the cell that contains the email address of the recipient
    email_cell = df["MainEmail"].values[cell_value]
    receiver_address = email_cell
    email_subj = user_sub  # This variable contains the subject line
    # This is the intro sentence of the email being sent
    email_intro = user_greeting + " " + name_cell + ","
    message = MIMEMultipart()  # Variable made to save time and make it more simple

    # Variable made to save time and make it more simple
    session = smtplib.SMTP('smtp.gmail.com', 587)
    session.starttls()  # enable security
    # Login with email and password (The User's)
    session.login(sender_address, sender_pass)

    message['From'] = sender_address  # From user's email address
    message['To'] = receiver_address  # To recipient email address
    # The variable that contains the desired subject line
    message['Subject'] = email_subj
    message.attach(MIMEText(email_intro + txt_doc_contents, 'plain'))
    with open(pdf_path, "rb") as f:
        attach = MIMEApplication(f.read(), _subtype="pdf")
    attach.add_header('Content-Disposition', 'attachment',
                      filename=str(pdf_name))
    message.attach(attach)
    text = message.as_string()

    session.sendmail(sender_address, receiver_address,
                     text)  # This sends the email
input("Emails have been sent successfully! Press any key to close the program!")
# The End :)

I'm not sure how to try to fix this considering the program needs to be flexible for different gmail accounts so I can't specifically add my signature to the code if that makes any sense. This is a program I use for work and I would like for other people to be able to use it as well but as of now without the signature it's not ready to be used by others.

gcb140
  • 19
  • 2
  • 1
    I believe the signature is added by the GMail front-end, not SMTP itself. As you've got your own [MUA](https://en.wikipedia.org/wiki/Email_agent_(infrastructure)) here you need to implement that yourself. – tadman Jan 10 '23 at 00:52
  • You will need to use the appropriate API to retrieve the email signature defined on the user's Google Account, as an example [this thread](https://stackoverflow.com/questions/36613872/manage-gmail-signatures-with-python-service-account) may point you towards the right direction. – metatoaster Jan 10 '23 at 01:12

0 Answers0