Aug-13-2024, 03:07 PM
(This post was last modified: Aug-13-2024, 04:33 PM by deanhystad.)
import smtplib import email import os from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email import encoders from email.message import Message from email.mime.base import MIMEBase g_strPath = "C:\\Users\\gregaryb\\Documents\\GitHub\\boylesg.github.io\\FindATradie\\data\\" def CreateEmailImage(strImageFilename, strImageID): fileImage = open(strImageFilename, 'rb') MsgImage = MIMEImage(fileImage.read()) fileImage.close() # Define the image's ID as referenced above MsgImage.add_header("Content-ID", "<" + strImageID + ">") return MsgImage def DoSendEmail(strEmail): strFromEmail = "[email protected]" RootEmailMsg = MIMEMultipart('related') RootEmailMsg["Subject"] = "NEW SERVICE for tradies: find-a-tradie.com.au" RootEmailMsg['From'] = strFromEmail RootEmailMsg['To'] = strEmail RootEmailMsg.preamble = 'This is a multi-part message in MIME format.' EmailMsgAlternative = MIMEMultipart('alternative') strText = "This is the alternative plain text message." TextEmailMsg = MIMEText(strText) EmailMsgAlternative.attach(TextEmailMsg) strText = "<!DOCTYPE html>\n" " <html>\n" " <head>\n" " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" " <title>https://www.find-a-tradie.com.au</title>\n" " </head>\n" " <body>\n" " <p><a href=\"https://www.find-a-tradie.com.au\" class=\"moz-txt-link-freetext\">https://www.find-a-tradie.com.au</a></p>\n" " <p><img src=\"cid:home_page\" alt="" width=\"455\" height=\"183\"></p>\n" " <p>Find-a-Tradie is a new advertising web service form Australian small tradie businesses. It has been created by an Australian small business tradie FOR Australian tradies.<br></p>\n" " <p>It is ideal for small businesses that are just starting out and don't have a large advertising budget.</p>\n" " <p>Or for small businesses in trades that typically involve many small low value jobs such as gardening, lawn mowing, pet care and domestic cleaning.<br></p>\n" " <p>Unlike HiPages, ServiceSeeking and OneFlare we do not charge you to obtain customer contact details - you can obtain them for free.</p>\n" " <p>We only charge you a flat annual membership fee of $10 per month or $120 per year.</p>\n" " <p>For that you can try for as many jobs as you want - there is no limit.</p>\n" " <p>However every new tradie gets their first 6 months of membership for free, and this offer is permanent.</p>\n" " <p>So tradie can 'try before they buy'.<br></p>\n" " <p>Customers can join for FREE at all times.<br></p>\n" " <p>------------------------------------------------------------------------------------------------------------------------------------<br></p>\n" " <p> The service uses a feedback based mutual trust system similar to eBay. </p>\n" " <p><img src=\"cid:feedback\" width=\"1077\" height=\"386\"></p>\n" " <p>So tradies can ALSO check the feedback history of clients before deciding to do any jobs for them.</p>\n" " <p><img src=\"cid:feedback_history\" width=\"1081\" height=\"414\"></p>\n" " <p>As well as clients being able to check the feedback history of tradies.<br>\n" " <img src=\"cid:feedback_as\" width=\"1079\" height=\"414\"><br>\n" " <p>So give it a try - remember your first 6 months is free. Get started today! Register now!</p><br>\n" " <p><br></p>\n" " </body>\n" "</html>\n" TextEmailMsg = MIMEText(strText) EmailMsgAlternative.attach(strText) RootEmailMsg.attach(EmailMsgAlternative) RootEmailMsg.attach(CreateEmailImage(g_strPath + "home_page.jpg", "home_page")) RootEmailMsg.attach(CreateEmailImage(g_strPath + "feedback.jpg", "feedback")) RootEmailMsg.attach(CreateEmailImage(g_strPath + "customer_feedback.jpg", "customer_feedback")) RootEmailMsg.attach(CreateEmailImage(g_strPath + "tradie_feedback.jpg", "tradie_feedback")) # Send the email via our own SMTP server. SMTPObject = smtplib.SMTP("smtp-mail.outlook.com", 587) #SMTPObject.connect("smtp.office365.com", 993) SMTPObject.ehlo() SMTPObject.starttls() SMTPObject.ehlo() SMTPObject.login("[email protected]", "Pulsar112358#") SMTPObject.sendmail(strFromEmail, strEmail, RootEmailMsg.as_string()) SMTPObject.quit() strMsg = "" g_arrayEmailFiles = ["ARBORISTS.email", "CLEANERS.email", "CONCRETERS.email", "ELECTRICIANS.email", "GARDENERS.email", "PAINTERS.email", "PET CARERS.email", "PLUMBERS.email"] for strEmailFile in g_arrayEmailFiles: nFileSize = os.path.getsize(g_strPath + strEmailFile) if (nFileSize > 0): fileEmail = open(g_strPath + strEmailFile, "r") print("Processing email file " + strEmailFile + "...") while True: nLine = 0 while (True): strEmail = fileEmail.readline() nLine += 1 if (len(strEmail) > 0): print("Sending email to " + strEmail + "...") DoSendEmail(strEmail) sleep(30) else: break; file.close() print("\n---------------------------------\n")The call to RootEmailMsg.as_string() in SMTPObject.sendmail(strFromEmail, strEmail, RootEmailMsg.as_string()) causes this error:
Traceback (most recent call last):
Error: File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1534, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.3.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Users\gregaryb\Documents\GitHub\boylesg.github.io\PythonProject\.venv\Scripts\SendEmails.py", line 119, in <module>
DoSendEmail(strEmail)
File "C:\Users\gregaryb\Documents\GitHub\boylesg.github.io\PythonProject\.venv\Scripts\SendEmails.py", line 90, in DoSendEmail
SMTPObject.sendmail(strFromEmail, strEmail, RootEmailMsg.as_string())
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\message.py", line 188, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 115, in flatten
self._write(msg)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 180, in _write
self._dispatch(msg)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 217, in _dispatch
meth(msg)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 275, in _handle_multipart
g.flatten(part, unixfrom=False, linesep=self._NL)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 115, in flatten
self._write(msg)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 180, in _write
self._dispatch(msg)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 217, in _dispatch
meth(msg)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 275, in _handle_multipart
g.flatten(part, unixfrom=False, linesep=self._NL)
File "C:\Users\gregaryb\AppData\Local\Programs\Python\Python312\Lib\email\generator.py", line 106, in flatten
old_msg_policy = msg.policy
^^^^^^^^^^
AttributeError: 'str' object has no attribute 'policy'
deanhystad write Aug-13-2024, 04:33 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.