def cli(): import argparse, getpass, mimetypes, sys parser = argparse.ArgumentParser( description='Send email message via GMail account') parser.add_argument('--username', '-u', required=True, help='GMail Username') parser.add_argument('--password', '-p', default=None, help='GMail Password') parser.add_argument('--to', '-t', required=True, action='append', default=[], help='To (multiple allowed)') parser.add_argument('--cc', '-c', action='append', default=[], help='Cc (multiple allowed)') parser.add_argument('--subject', '-s', required=True, help='Subject') parser.add_argument('--body', '-b', help='Message Body (text)') parser.add_argument('--html', '-l', default=None, help='Message Body (html)') parser.add_argument('--attachment', '-a', action='append', default=[], help='Attachment (multiple allowed)') parser.add_argument('--debug', '-d', action='store_true', default=False, help='Debug') results = parser.parse_args() if results.password is None: results.password = getpass.getpass("Password:"******",".join(results.to), cc=",".join(results.cc), text=results.body, html=results.html, attachments=results.attachment) gmail.send(msg) gmail.close()
def test_gmail(self): gmail = GMail(os.environ["GMAIL_ACCOUNT"], os.environ["GMAIL_PASSWD"]) msg1 = Message("GMail Test Message #1", to=os.environ["GMAIL_RCPT"], text="Hello") msg2 = Message("GMail Test Message #2", to=os.environ["GMAIL_RCPT"], text="Hello") gmail.send(msg1) gmail.send(msg2) gmail.close()
def send_email(to_addr, subject, msg): mail = GMail(GMAIL_EMAIL, GMAIL_PASSWORD) mail.connect() if to_addr is list: to_addr = ', '.join(list(to_addr)) mail.send(Message(subject, to=to_addr, html=msg)) mail.close()
def sendMail(): logger.info('Sending mail to %s', config['GMail']['to']) gmail = GMail(config['GMail']['username'],config['GMail']['password']) mail = Message(subject='New IP @ KA', to=config['GMail']['to'], sender='Automatic IP updater', text='Reported from: %s\nIP: %s' % (config['General']['hostname'],ip) ) gmail.send(mail) gmail.close()
def send(self): print('Sending {}'.format(self.title)) gmail = GMail(GMAIL_ACCOUNT, GMAIL_PASSWD) msg = Message(self.title, to=MAIL_TO, text=self.text, attachments=self.attachments) gmail.send(msg) gmail.close() print('Ok. Sent Edison: {}'.format(self.title))
def test_gmail(self): gmail = GMail(os.environ['GMAIL_ACCOUNT'], os.environ['GMAIL_PASSWD']) msg1 = Message('GMail Test Message #1', to=os.environ['GMAIL_RCPT'], text='Hello') msg2 = Message('GMail Test Message #2', to=os.environ['GMAIL_RCPT'], text='Hello') gmail.send(msg1) gmail.send(msg2) gmail.close()
def test_message_dispatch(username, password, to, body, subject): with allure.step("Check password"): if password is None: raise TestInputException("Password required") with allure.step("Check body"): if body is None: raise TestInputException("Message text required") gmail = GMail(username=username, password=password) to = to.split(",") msg = Message(subject=subject, to=",".join(to), text=body) try: with allure.step("Sending message"): gmail.send(msg) except SMTPException: raise AssertionError with allure.step("Close connection with gmail"): gmail.close()
def cli(): import argparse, getpass, sys parser = argparse.ArgumentParser(description='Send email message via GMail account') parser.add_argument('--username', '-u', required=True, help='GMail Username') parser.add_argument('--password', '-p', default=None, help='GMail Password') parser.add_argument('--to', '-t', required=True, action='append', default=[], help='To (multiple allowed)') parser.add_argument('--cc', '-c', action='append', default=[], help='Cc (multiple allowed)') parser.add_argument('--subject', '-s', required=True, help='Subject') parser.add_argument('--body', '-b', help='Message Body (text)') parser.add_argument('--html', '-l', default=None, help='Message Body (html)') parser.add_argument('--attachment', '-a', action='append', default=[], help='Attachment (multiple allowed)') parser.add_argument('--debug', '-d', action='store_true', default=False, help='Debug') results = parser.parse_args() if results.password is None: results.password = getpass.getpass("Password:"******",".join(results.to), cc=",".join(results.cc), text=results.body, html=results.html, attachments=results.attachment) gmail.send(msg) gmail.close()
class GmailSender: def __init__(self, username: str, password: str, format: str = 'html', error_handler: Callable[[Recipient], None] = None, success_handler: Callable[[Recipient], None] = None, background: bool = True, parallel_threads: int = 1): self.username = username self.password = password self.gmail = GMail(self.username, self.password) self.error_handler = error_handler self.success_handler = success_handler self.background = background self.format = format self.is_connected = False def prepare(self) -> None: if not self.is_connected: self.gmail.connect() self.is_connected = True def _worker_thread(self, payload) -> None: self.prepare() try: self.gmail.send(payload.msg, None) except SMTPServerDisconnected: if self.error_handler: self.error_handler(payload.recipeient) except SMTPResponseException: if self.error_handler: self.error_handler(payload.recipient) except KeyboardInterrupt: return if self.success_handler: self.success_handler(payload.recipient) def _send_bulk_synchronous(self, payload_list) -> None: self.prepare() for payload in payload_list: self._worker_thread(payload) def _send_bulk_async(self, payload_list: Payload) -> None: pool = Pool() pool.map(self._worker_thread, payload_list) pool.close() def send_bulk(self, payload_list) -> None: if self.background: return self._send_bulk_async(payload_list) else: return self._send_bulk_synchronous(payload_list) def send(self, message: str, recipient: Recipient, subject: str, rcpt: str = None) -> None: if self.format == 'html': msg = Message(subject=subject, to=recipient.email, html=message) else: msg = Message(subject=subject, to=recipient.email, text=message) self.queue.put((msg, recipient, rcpt)) def close(self) -> None: self.gmail.close() self.is_connected = False def __del__(self): self.close() def __default_handler(self, recipient: Recipient, *args, **kwargs) -> None: logger.error("Error while sending maile to {}".format(recipient.email))
from jinja2 import Template from gmail import GMail, Message mailingList = {'Ins1':'*****@*****.**','Ins2':'*****@*****.**'} usr = GMail('*****@*****.**','xxxxx') template = Template('Hello {{ name }}!\nYour email is {{ email }}\nThis is a junk mail.\nHave a nice day.') for target in mailingList: context = template.render(name=target, email=mailingList[target]) msg = Message(to=mailingList[target],text=context,subject="Junk mail") print context usr.send(msg) usr.close()