Python Forum

Full Version: Mail is not sent to multiple users
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
THe code works fine for a single email address.
But if I add multiple email addresses, then it is being received by only the first email adress and the others are not getting that.
But I can see them in the to list, in the received mail and even in the sent items of gmail.When I click on reply all manually then they are receiving.

Please suggest.

import time,datetime
import os

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import binascii
import struct

#import datetime as dt
import string

def send_gmail2(subject,attach_file=None):
    email_content = """
    Hi All,

    %s.
    This mail sent from build machine.
                    """ % ( subject)

    new_msg = {
                    "content": email_content,
                    "from_address": "[email protected]",
                    "to_address": "[email protected],[email protected]",
                    "subject": subject
              }
    process_send_gmail("upd.bin", new_msg,attach_file)





def process_send_gmail(upd_path, msg, attach_file=None):
    #account = get_cred_from_file(upd_path)
    account = get_cred()

    msg_body = MIMEMultipart()
    msg_body['From'] = msg["from_address"]
    msg_body['To'] = msg["to_address"]
    msg_body['Subject'] = msg["subject"]
    msg_body.attach(MIMEText(msg["content"]))
    if attach_file is not None:
        attach_file = attach_file.replace("\"", "").replace("\'", "")
        file_name = os.path.basename(attach_file)
        att1 = MIMEText(open(attach_file, 'rb').read(), 'base64', 'utf8')
        att1["Content-Type"] = 'application/octet-stream'
        att1["Content-Disposition"] = 'attachment; filename="%s"' % file_name
        msg_body.attach(att1)

    print "Start to send mail"
    s = smtplib.SMTP("smtp.gmail.com")

    # s.set_debuglevel(1)
    s.ehlo()
    s.starttls()
    s.login(account["user"], account["pwd"])
    s.sendmail(msg["from_address"], msg["to_address"], msg_body.as_string())
    s.quit()
    print "Mail sent"


def get_cred():
    return {"user": "[email protected]", "pwd": "****"}

send_gmail2("Ant War file creation completed with success or failure.Please check the log file: ")
I got the issue resolved.
The sendmail method of smtp object requires 3 inputs.
1. from address
2. to address list
3. message object converted as string.

The 3rd object : message contains the message and also header information where the to,cc,bcc,from is saved and same is shown when we see the message in browser or mail client.


But the original senders list (all to,cc,bcc) is merged as single list and sent with the 2nd argument and which should be converted to list.If not, only the first recipient will get the mail but the other recipients are visible as they exist in the header data.

So, below is the modified code block.

s.sendmail(msg["from_address"], msg["to_address"].split(','), msg_body.as_string())