Python Forum
Mail is not sent to multiple users
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mail is not sent to multiple users
#1
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: ")
Reply
#2
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mail issue Mihil 3 2,588 Dec-03-2020, 05:25 AM
Last Post: Mihil
  smtplib mail without subject anna 2 2,431 Apr-24-2019, 05:44 AM
Last Post: anna
  Mail Faiyaz 1 23,756 Sep-24-2018, 02:29 PM
Last Post: Larz60+
  Outlook mail watcher g_shanmuga 1 5,009 Mar-26-2018, 04:09 PM
Last Post: nilamo
  Netmiko - add multiple files, from different folders to mail. tomikovaknin 5 5,300 Nov-26-2017, 12:55 PM
Last Post: heiner55
  Simple e-mail sender, that automatically changes subject for every e-mail joker 2 3,124 Aug-24-2017, 09:45 AM
Last Post: joker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020