lavr / python-emails

Modern python library for emails
http://python-emails.readthedocs.org
Other
400 stars 54 forks source link

How to send email with CC #118

Closed babvin closed 6 years ago

babvin commented 6 years ago

Getting error when I add CC while sending emails.

TypeError: send() got an unexpected keyword argument 'cc'

lavr commented 6 years ago

If you want recipient to receive email, you should include that email in to parameter of send

If you want recipient's email visible in email headers, you can use cc parameter of Message constructor.

babvin commented 6 years ago

Many Thanks, Sergey, we'll try and revert.

On Sat, Aug 11, 2018 at 9:32 PM Sergey Lavrinenko notifications@github.com wrote:

If you want recipient to receive email, you should include that email in to parameter of send

If you want recipient's email visible in email headers, you can use cc parameter of Message constructor.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lavr/python-emails/issues/118#issuecomment-412284296, or mute the thread https://github.com/notifications/unsubscribe-auth/AcpLAxTi5tgfvoriPfEvI6lqlymvUrStks5uPwAdgaJpZM4V1mFG .

babvin commented 6 years ago

Unable send email to multiple recipients when passed as arguments. I tried several ways but it takes only 1 email address

import emails
import argparse

def check_arg(args=None):

    parser = argparse.ArgumentParser(description='Script to send a  Report through Email')

    parser.add_argument('-s', '--smtpServer',
                        help='smtp server ip',
                        required='True')

    parser.add_argument('-r', '--mailRecipient',
                        help='mail recipient, enter "," seperated',
                        required='True'
                        )

    results = parser.parse_args(args)

    return (results.smtpServer, results.mailRecipient)

#
smtphost, to = check_arg()
#Generate Email
message = emails.html(html="<p>Hi<br>Please find attached  Report. To see all previously run report archives, use this link >>> http://report/report/",
                       subject=" Report " ,
                       mail_from=('vmax_snapshots_report'))

#message.attach(data=open(reppath+'report_%s.xlsx'%date, 'rb'), filename='report_%s.xlsx'%date)

#logger.info('Email Message Body \n {}'.format(message))

#Send Message
#SMTP IP is hard coded since the file collation and sending email is done by the Master node

print(to)
print(type(to))
r = message.send(to=[to], smtp={'host': smtphost, 'timeout': 5})
assert r.status_code == 250

[rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r 'vinay.umesh@abcd.com','dhilen.mamtora@abcd.com' vinay.umesh@abcd.com,dhilen.mamtora@abcd.com <class 'str'> [rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r vinay.umesh@abcd.com,dhilen.mamtora@abcd.com vinay.umesh@abcd.com,dhilen.mamtora@abcd.com <class 'str'> [rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r 'vinay.umesh@abcd.com,dhilen.mamtora@abcd.com' vinay.umesh@abcd.com,dhilen.mamtora@abcd.com <class 'str'> [rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r vinay.umesh@abcd.com vinay.umesh@abcd.com <class 'str'> [rda@server ~]$ vim email_test.py [rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r 'vinay.umesh@abcd.com,dhilen.mamtora@abcd.com' vinay.umesh@abcd.com,dhilen.mamtora@abcd.com <class 'str'> [rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r vinay.umesh@abcd.com vinay.umesh@abcd.com <class 'str'> [rda@server ~]$ python3.6 email_test.py -s 5.5.5.5 -r 'vinay.umesh@abcd.com','dhilen.mamtora@abcd.com' vinay.umesh@abcd.com,dhilen.mamtora@abcd.com <class 'str'>

iudeen commented 3 years ago

@babvin The issue seemed to be in the way you parse arguments. Fixed the code, PFB.

import emails
import argparse

def check_arg(args=None):

    parser = argparse.ArgumentParser(description='Script to send a  Report through Email')

    parser.add_argument('-s', '--smtpServer',
                        help='smtp server ip',
                        required=True)

    parser.add_argument('-r', '--mailRecipient',
                        nargs='+',
                        help='mail recipient, enter "<space>" seperated',
                        required=True
                        )

    results = parser.parse_args(args)

    return (results.smtpServer, results.mailRecipient)

#
smtphost, to = check_arg()
#Generate Email
message = emails.html(html="<p>Hi<br>Please find attached  Report. To see all previously run report archives, use this link >>> http://report/report/",
                       subject=" Report " ,
                       mail_from=('vmax_snapshots_report'))

#Send Message
#SMTP IP is hard coded since the file collation and sending email is done by the Master node

print(to)
print(type(to))
r = message.send(to=[to], smtp={'host': smtphost, 'timeout': 5})
assert r.status_code == 250

Got Results:

scratch_3.py -s 5.5.5.5 -r test1@test.com, test2@test.com
['test1@test.com,', 'test2@test.com']
<class 'list'>