import smtplib
from celery import Celery
from email.mime.text import MIMEText
from KerbalStuff.config import _cfg, _cfgi, _cfgb

app = Celery("tasks", broker="redis://localhost:6379/0")

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in range(0, len(l), n):
        yield l[i:i+n]

@app.task
def send_mail(sender, recipients, subject, message, important=False):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(host=_cfg("smtp-host"), port=_cfgi("smtp-port"))
    message = MIMEText(message)
    if important:
        message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = subject
    message['From'] = sender
    for group in chunks(recipients, 100):
        message['To'] = ";".join(group)
        print("Sending email from {} to {} recipients".format(sender, len(group)))
        smtp.sendmail(sender, group, message.as_string())
    smtp.quit()

import smtplib
from celery import Celery
from email.mime.text import MIMEText
from KerbalStuff.config import _cfg, _cfgi, _cfgb

app = Celery("tasks", broker="redis://localhost:6379/0")

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in range(0, len(l), n):
        yield l[i:i+n]

@app.task
def send_mail(sender, recipients, subject, message, important=False):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(host=_cfg("smtp-host"), port=_cfgi("smtp-port"))
    message = MIMEText(message)
    if important:
        message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = subject
    message['From'] = sender
    for group in chunks(recipients, 100):
        message['To'] = "undisclosed-recipients:;"
        print("Sending email from {} to {} recipients".format(sender, len(group)))
        smtp.sendmail(sender, group, message.as_string())
    smtp.quit()

Remove addresses from "To" field, BCC instead.
[proposal] Remove addresses from "To" field, BCC instead.

Closing privacy issue by hiding the "To" field altogether. Just commented out the "To" line. For issue #68
  import smtplib
  from celery import Celery
  from email.mime.text import MIMEText
  from KerbalStuff.config import _cfg, _cfgi, _cfgb
  
  app = Celery("tasks", broker="redis://localhost:6379/0")
  
  def chunks(l, n):
      """ Yield successive n-sized chunks from l.
      """
      for i in range(0, len(l), n):
          yield l[i:i+n]
  
  @app.task
  def send_mail(sender, recipients, subject, message, important=False):
      if _cfg("smtp-host") == "":
          return
      smtp = smtplib.SMTP(host=_cfg("smtp-host"), port=_cfgi("smtp-port"))
      message = MIMEText(message)
      if important:
          message['X-MC-Important'] = "true"
      message['X-MC-PreserveRecipients'] = "false"
      message['Subject'] = subject
      message['From'] = sender
      for group in chunks(recipients, 100):
-         message['To'] = ";".join(group)
+         message['To'] = "undisclosed-recipients:;"
          print("Sending email from {} to {} recipients".format(sender, len(group)))
          smtp.sendmail(sender, group, message.as_string())
      smtp.quit()
  
Remove addresses from "To" field, BCC instead.
## Code Before:
import smtplib
from celery import Celery
from email.mime.text import MIMEText
from KerbalStuff.config import _cfg, _cfgi, _cfgb

app = Celery("tasks", broker="redis://localhost:6379/0")

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in range(0, len(l), n):
        yield l[i:i+n]

@app.task
def send_mail(sender, recipients, subject, message, important=False):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(host=_cfg("smtp-host"), port=_cfgi("smtp-port"))
    message = MIMEText(message)
    if important:
        message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = subject
    message['From'] = sender
    for group in chunks(recipients, 100):
        message['To'] = ";".join(group)
        print("Sending email from {} to {} recipients".format(sender, len(group)))
        smtp.sendmail(sender, group, message.as_string())
    smtp.quit()

## Instruction:
Remove addresses from "To" field, BCC instead.
## Code After:
import smtplib
from celery import Celery
from email.mime.text import MIMEText
from KerbalStuff.config import _cfg, _cfgi, _cfgb

app = Celery("tasks", broker="redis://localhost:6379/0")

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in range(0, len(l), n):
        yield l[i:i+n]

@app.task
def send_mail(sender, recipients, subject, message, important=False):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(host=_cfg("smtp-host"), port=_cfgi("smtp-port"))
    message = MIMEText(message)
    if important:
        message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = subject
    message['From'] = sender
    for group in chunks(recipients, 100):
        message['To'] = "undisclosed-recipients:;"
        print("Sending email from {} to {} recipients".format(sender, len(group)))
        smtp.sendmail(sender, group, message.as_string())
    smtp.quit()
