GithubHelp home page GithubHelp logo

BCC/CC support about mailmerge HOT 3 CLOSED

rupertlevene avatar rupertlevene commented on August 12, 2024
BCC/CC support

from mailmerge.

Comments (3)

awdeorio avatar awdeorio commented on August 12, 2024

It looks like this will require parsing the CC and BCC fields and adding those addresses to the send_email() function call.

https://www.blog.pythonlibrary.org/2013/06/26/python-102-how-to-send-an-email-using-smtplib-email/

from mailmerge.

rupertlevene avatar rupertlevene commented on August 12, 2024

This seems to work for me. It's untested in python 3 but hopefully works there too.

--- api.py	2017-07-13 17:01:50.000000000 +0100
+++ /Users/rupert/code/mailmerge/mailmerge/api.py	2017-07-15 11:08:07.000000000 +0100
@@ -11,6 +11,7 @@
 import sys
 import smtplib
 import email.parser
+import email.utils
 import configparser
 import getpass
 import csv
@@ -23,6 +24,22 @@
 CONFIG_FILENAME_DEFAULT = "mailmerge_server.conf"
 
 
+def parsemail(text):
+    """Parse message headers, then remove BCC header."""
+    message = email.parser.Parser().parsestr(text)
+    addrs = email.utils.getaddresses(message.get_all("to", [])) + \
+            email.utils.getaddresses(message.get_all("cc", [])) + \
+            email.utils.getaddresses(message.get_all("bcc", []))
+    recipients = [x[1] for x in addrs]
+    message.__delitem__("bcc")
+
+    for recipient in recipients:
+        print("RCPT TO:<" + recipient + ">")
+    print(message.as_string())
+
+    return (message,recipients)
+
+
 def sendmail(text, config_filename):
     """Send email message using Python SMTP library."""
     # Read config file from disk to get SMTP server host, port, username
@@ -46,8 +63,7 @@
                                                       sendmail.host)
         sendmail.password = getpass.getpass(prompt)
 
-    # Parse message headers
-    message = email.parser.Parser().parsestr(text)
+    (message,recipients)=parsemail(text)
 
     # Connect to SMTP server
     if sendmail.security == "SSL/TLS":
@@ -69,12 +85,12 @@
     # Send message
     try:
         # Python 3.x
-        smtp.send_message(message)
+        smtp.send_message(message, recipients)
     except AttributeError:
         # Python 2.7.x
         smtp.sendmail(
             message["from"],
-            message["to"],
+            recipients,
             message.as_string(),
             )
     smtp.close()
@@ -200,10 +216,10 @@
 
             # Fill in template fields using fields from row of CSV file
             message = template.render(**row)
-            print(message)
 
             # Send message
             if dry_run:
+                parsemail(message)
                 print(">>> sent message {} DRY RUN".format(i))
             else:
                 sendmail(message, config_filename)

from mailmerge.

awdeorio avatar awdeorio commented on August 12, 2024

Thanks for the patch! I'll integrate and test this soon.

from mailmerge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.