0

i am new to python,and below is the code which is suppose to send mail to multiple receipent but only dipeshyog94@gmail.com is getting a mail. milanthapa898@gmail.com which is second on To and alexlee94@gmail.com which is on cc is no getting the mail

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import smtplib

owner_emp_id_email = "dipeshyogi94@gmail.com,milanthapa989@gmail.com"
mymail='milanthapa898@gmail.com'
msg = MIMEMultipart()
msg['From'] = mymail
msg['To'] = owner_emp_id_email
cc_mail = "alexlee94@gmail.com"
msg['Cc'] = cc_mail
print('####44444444444444########\n')
print(owner_emp_id_email)
msg['Subject'] = 'Automated Test Mail with python'
a = 'Milan Thapa'
#body = 'Dear '+spoc_name+',\n\nYou have created new job with below Details:\n\nProject ID : '+project_ID+'\n\nProject Name : '+ibu_name+'\n\nJob Description : ' +job_description +'\n\nThanks and Regards,\n\nMilan Thapa'
html = """\
            <html>
                <head></head>
                    <body>
                        <p>'Dear <b>{}<b>

                        </p>
                    </body>
            </html>
            """.format(a)
msg.attach(MIMEText(html,'html'))
text = msg.as_string()
try:
    server = smtplib.SMTP('smtp.gmail.com:587')

except:
    server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(mymail,'password')
server.sendmail(mymail,owner_emp_id_email,text)
server.quit()

i am stuck in this couldn't send the mail to multiple users.

any help will be greatly appreciated!

thanks in advance

Milan Mangar
  • 41
  • 1
  • 9
  • 1
    If those are real email addresses, exposing them like this is almost guaranteed to increase your spam load. Maybe [edit] your question to replace them with placeholders like `mail1@example.com` – tripleee Mar 08 '19 at 06:11

3 Answers3

2

The information in the headers doesn't control where the message actually goes. The second argument to sendmail is the only place where this is controlled. This value should be a list, not a comma-separated string.

owner_emp_id_email = "dipeshyogi94@gmail.com,milanthapa989@gmail.com"
env_rcpts = owner_emp_id_email.split(",")
# ...
cc_mail = "alexlee94@gmail.com"
env_rcpts.append(cc_mail)
# ...
server.sendmail(mymail,env_rcpts,text)

You'll notice that you could also add addresses which are neither in To: or Cc: (or a number of other headers which serve the same purpose) to effectively implement Bcc:

Maybe also look at send_message which saves you from having to separately convert your message to a string you can pass to sendmail.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Maybe see also https://stackoverflow.com/questions/54412188/header-to-for-a-bulk-email-sender/54414203#54414203 which attempts to explain this in some more detail. – tripleee Oct 07 '19 at 05:39
1
msg['To'] = owner_emp_id_email

Here owner_emp_id_email is a string. Make it a list of email ids. Then it would work.

to_ids = owner_emp_id_email.split(',')
msg['To'] = to_ids
eiram_mahera
  • 950
  • 9
  • 25
0

>>> help(smtplib)

to_addrs: A list of addresses to send this mail to. A bare string will be treated as a list with 1 address.

You need to convert your CSV string to a list like mentioned in the the other answer.

With this answer I just wanted to demonstrate how can we use the python's amazing help function when stuck.

Shariq
  • 513
  • 4
  • 14
  • `help(smptlib)` brings up a copy of [the entire module documentation](https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail). Maybe not a stellar example. Maybe also show how to google? (Sorry, couldn't resist.) – tripleee Mar 08 '19 at 06:12
  • sorry I forgot to mention you will have to press Enter a few times and use your visual sensors as well as natural and subconscious task of finding similar text from a seemingly very cute documentation. Oh and for the bonus you also get to learn 4 different things, if interested. – Shariq Mar 08 '19 at 06:25