#!/usr/bin/env python

# dupe
# Copyright (C) 2025 Joseph Rosevear, San Diego CA, USA.

# This file is part of SAM.handy, a library of tools for use with SAM.

# SAM.handy is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.

# SAM.handy is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.

# You should have received a copy of the GNU General Public License
# along with SAM.handy. If not, see <http://www.gnu.org/licenses/>.

# To learn more about SAM and SAM.handy, see <https://RosevearSoftware.com>.



# Joseph Rosevear 200324 I made this script by modifying an example
# script that I found here:

#    https://www.tutorialspoint.com/send-mail-with-attachment-from-your-gmail-account-using-python

# Note that original had a bug.  It had:

#    Content-Decomposition

# instead of

#    Content-Disposition

# I fixed the bug and rearranged the code a little, adding to its
# usefulness and suitability for my needs along the way.

# You may want to  Get my SAM from

#    http://sourceforge.net/projects/sam-kernel

# SAM includes  Dupe (in the "example" module) and some menus and
# helper scripts.

# You may also want to read my blog post about this script:

#    http://joeslife.org

# Joseph Rosevear 200409 I rearranged the comments.

# Joseph Rosevear 210821 I made a small change which allows me to pass
# compound To and Cc addresses.

# I changed:

#  sendto = [to, cc]
   
# to:

#  sendto = to.split(",") + cc.split(",")

# Joseph Rosevear 251215 01:20PM I changed a comment at the top.



# Usage information:

# Note that the user bears the responsibility of exporting useful
# values of epass, user, ffrom, smtp and port before using this script.

# The invocation takes four, five or six arguments:

# $1	To
# $2	Cc or "-"
# $3	file containing Subject
# $4	file containg the message Body
# $5    file--optional first attachment
# $6    file--optional second attachment

# Note that To and Cc can be single or compound.  A single address is
# what you normally use.  A compound address is two or more addresses,
# separated by commas with or without spaces.  Of course, if spaces are
# used, then you must pass the argument in quotes (or equivalent).



import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os

epass  = os.environ.get('epass')
user  = os.environ.get('user')
ffrom = os.environ.get('ffrom')
smtp   = os.environ.get('smtp')
port   = os.environ.get('port')

to           = sys.argv[1]
cc           = sys.argv[2]
subject_name = sys.argv[3] 
content_name = sys.argv[4]

subject_file = open(subject_name, 'r')
content_file = open(content_name, 'r')

subject = subject_file.read()
content = content_file.read()

subject_file.close()
content_file.close()

#Setup the MIME.
message = MIMEMultipart()
message['From'] = ffrom
message['To'] = to

# Add cc.
if (cc != '-'):

   message['Cc'] = cc
   
message['Subject'] = subject

# Define sendto.
if (cc != '-'):

   sendto = to.split(",") + cc.split(",")
      
else:
   
   sendto = to

#The body and the attachments for the mail.
message.attach(MIMEText(content, 'plain'))

# Define attach1.
if ((len(sys.argv) >= 6) and (sys.argv[5] != '-')):

   attach1=sys.argv[5]
   
else:

   attach1='-'

# Define attach2.
if ((len(sys.argv) >= 7) and (sys.argv[6] != '-')):

   attach2=sys.argv[6]
   
else:

   attach2='-'
   
# Define attachments.
attachments = [attach1, attach2]
   
# Attach files.
for AFileName in attachments:

   if (AFileName != '-'):
   
      attach_file_name = AFileName
      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-Disposition', 'attachment', filename=attach_file_name)
      message.attach(payload)

#Create SMTP session for sending the mail.
session = smtplib.SMTP(smtp, port) #Use smtp with port.
session.starttls() #Enable security.

# This logs-in.
session.login(user, epass) #Login with mail_id and password.

# This sends the message.
text = message.as_string()
session.sendmail(user, sendto, text)

session.quit()
print ('Mail sent to: '+to+', '+cc)
