EMAIL users

470
1
Jump to solution
03-01-2018 11:44 AM
jaykapalczynski
Frequent Contributor

I am trying to email via python.  It did work at sometime but now its not working.  It is being flagged as SPAM leaving my external servers.  Below you see whats causing this.  I was told to include the FROM and TO in the header and this should elevate the issue of beign flagged as SPAM

I am unsure where to include this in my code below....ANYONE have any ideas on how to add that?  See below Code snip

Content analysis details:   (6.7 points, 6.0 required)

 pts rule name              description
---- ---------------------- --------------------------------------------------
-0.0 T_RP_MATCHES_RCVD      Envelope sender domain matches handover relay
                            domain
-0.0 SPF_PASS               SPF: sender matches SPF record
 1.2 MISSING_HEADERS        Missing To: header
 0.0 HTML_MESSAGE           BODY: HTML included in message
 1.1 MIME_HTML_ONLY         BODY: Message only has text/html MIME parts
 0.1 MISSING_MID            Missing Message-Id: header
 1.0 MISSING_FROM           Missing From: header
 1.4 MISSING_DATE           Missing Date: header
 1.9 TO_NO_BRKTS_HTML_ONLY  To: lacks brackets and HTML only

import arcpy
import os, string
import smtplib, shutil
import email, time
import sys, csv
from email.MIMEMultipart import MIMEMultipart
from email.mime.text import MIMEText
      
FROMADDR = ["Bxxxs@xxx.com"]
TOADDRS  = ["xxx@xxxx.gov"]

# REGION 1 =======
with open('z_Region1.csv', 'rb') as f:
    R1reader = csv.reader(f)
    R1content = list(R1reader)
R1got = tuple(map(Thread_compile_string, R1content))
# print str(R1got).strip('[]')
# Analyze the Tuple to determine if there are any values
# If so add them to the email body, if not do not add them but add string with date
if len(R1got) != 0:
    print('found!')
    cols1 = ["<tr>{0}</tr>".format( "<tr></tr>".join(t)) for t in R1got]
    rows1 = "<tr>{0}</tr>".format( "<tr></tr>".join(cols1))
    htmlR1 = """<HTML>
        <head></head>
        <body>
        <h4>Facilities Reported for Region 1 for {0}</h4>
            <table>  
                {1}
            </table>
        </body>  
        </HTML>""".format(CurrentDate, rows1)
if flagR1 == True:  
    # What do you want the attached file to be named?
    filename = "z_Region1.csv"
    f = file(filename)
    #=== BUILD EMAIL ===
    def msgEmail():
        msg = MIMEMultipart()
        # Attach the records in the body of the email
        part4 = MIMEText(htmlR1, 'html')
        msg.attach(part4)
        attachment = MIMEText(f.read())
        attachment.add_header('Content-Disposition', 'attachment', filename=filename)                 
        msg.attach(attachment)   
        msg['Subject'] = "Region 1 Report " + CurrentDate
        server = smtplib.SMTP('smtp1.xxxx.com',25)
        server.set_debuglevel(1)
        server.ehlo()
        server.sendmail(FROMADDR, TOADDRS, msg.as_string())
        server.quit()
    #=== PROCESS EMAIL ===
    getMsgP = msgEmail()
    print getMsgP
    #=== CLOSE THE FILE BEING ATTACHED ===
    f.close()
    print ("Region 1 Email Sent")
elif flagR1 == False:  
    print "R1 None No Rows found"  
    def msgEmailR1():
        msg = MIMEMultipart()
        part5 = MIMEText(htmlConformation, 'html')
        msg.attach(part5)           
        #msg.attach(attachment) 
        msg['Subject'] = "Region 1 Report - no Records processed " + CurrentDate
        server = smtplib.SMTP('smtp1.xxxxx.com',25)
        server.set_debuglevel(1)
        server.ehlo()
        server.sendmail(FROMADDR, TOADDRS, msg.as_string())      
        server.quit()
    getMsgP = msgEmailR1()
    print getMsgP
    print ("Conformation Email Email Sent")
else:  
    print "R1 Something Else is going on"  
#=======================================================
0 Kudos
1 Solution

Accepted Solutions
jaykapalczynski
Frequent Contributor

Think I got it...under the msg['Subject'] line .....  simply added

msg['To'] = "xxxx@xxxxx.com"
msg['From'] = "xxxxx@xxxxx.com"
msg['Date'] = CurrentDate

    #=== BUILD EMAIL ===
    def msgEmail():
        msg = MIMEMultipart()
        # Attach the records in the body of the email
        part4 = MIMEText(htmlR1, 'html')
        msg.attach(part4)
        msg['To'] = "xxxx@xxxxx.com" 
        msg['From'] = "xxxx@xxxxx.com"      
        msg['Date'] = CurrentDate
        attachment = MIMEText(f.read())
        attachment.add_header('Content-Disposition', 'attachment', filename=filename)                 
        msg.attach(attachment)  
        msg['Subject'] = "Region 1 Report " + CurrentDate
        server = smtplib.SMTP('smtp1.xxxx.com',25)
        server.set_debuglevel(1)
        server.ehlo()
        server.sendmail(FROMADDR, R1TOADDRS, msg.as_string())
        server.quit()

View solution in original post

1 Reply
jaykapalczynski
Frequent Contributor

Think I got it...under the msg['Subject'] line .....  simply added

msg['To'] = "xxxx@xxxxx.com"
msg['From'] = "xxxxx@xxxxx.com"
msg['Date'] = CurrentDate

    #=== BUILD EMAIL ===
    def msgEmail():
        msg = MIMEMultipart()
        # Attach the records in the body of the email
        part4 = MIMEText(htmlR1, 'html')
        msg.attach(part4)
        msg['To'] = "xxxx@xxxxx.com" 
        msg['From'] = "xxxx@xxxxx.com"      
        msg['Date'] = CurrentDate
        attachment = MIMEText(f.read())
        attachment.add_header('Content-Disposition', 'attachment', filename=filename)                 
        msg.attach(attachment)  
        msg['Subject'] = "Region 1 Report " + CurrentDate
        server = smtplib.SMTP('smtp1.xxxx.com',25)
        server.set_debuglevel(1)
        server.ehlo()
        server.sendmail(FROMADDR, R1TOADDRS, msg.as_string())
        server.quit()