当前位置: 首页>>代码示例>>Python>>正文


Python SMTP.send方法代码示例

本文整理汇总了Python中smtplib.SMTP.send方法的典型用法代码示例。如果您正苦于以下问题:Python SMTP.send方法的具体用法?Python SMTP.send怎么用?Python SMTP.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在smtplib.SMTP的用法示例。


在下文中一共展示了SMTP.send方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: putcmd

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import send [as 别名]
 def putcmd(self,cmd,args=""):
     """Send a command to the server."""
     if args=="":
         str='%s%s'%(cmd,smtplib.CRLF)
     else:
         str='%s %s%s'%(cmd,args,smtplib.CRLF)
     # Don't monitor the command data
     SMTP.send(self,str)
开发者ID:fivesheep,项目名称:sendemail,代码行数:10,代码来源:gsend.py

示例2: Gmail

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import send [as 别名]
class Gmail():

    def __init__(self, username):
        # the main username
        self.username = username
        self.http = httplib2.Http()
        self.logger = logging.getLogger('Gmail')

        # Start the OAuth flow to retrieve credentials
        flow = flow_from_clientsecrets(CLIENT_SECRET_FILE,
                                       scope=OAUTH_SCOPE)

        # The storage for current user
        storage = Storage(os.path.join(STORAGE_DIR, self.username))

        # Try to retrieve credentials from storage
        # or run the flow to generate them
        self.credentials = storage.get()
        if self.credentials is None or self.credentials.invalid:
            # call a subprocess as workaround for stdin/stdout
            # blocked by the main process, this is the main function:
            def _subprocess_login():
                sys.stdout = StreamToLogger(self.logger, logging.DEBUG)
                sys.stderr = StreamToLogger(self.logger, logging.ERROR)
                queue.put(run(flow, storage, http=self.http))

            # A Queue to get the result from subprocess
            queue = Queue()
            p = Process(target=_subprocess_login)
            self.logger.debug("start login process")
            p.start()
            p.join()
            self.logger.debug("end login process")
            # Retrieve the credentials
            self.credentials = queue.get()

        # Access to the Gmail APIs
        self._gmail_API_login()

        # Use smtp as workaround to send message, GMail API
        # bugged with Content-Type: multipart/signed
        self._smtp_login()

    def _gmail_API_login(self):
        """
        Login in Gmail APIs
        """
        self._refresh_credentials()

        # Authorize the httplib2.Http object with our credentials
        authorized_http = self.credentials.authorize(self.http)

        # Build the Gmail service from discovery
        self.gmail_service = build('gmail', 'v1', http=authorized_http)
        self.messages = self.gmail_service.users().messages()
        self.drafts = self.gmail_service.users().drafts()

    def _refresh_credentials(self):
        """Refresh credentials if needed"""
        if self.credentials.access_token_expired:
            self.logger.info("credentials expired - refreshing")
            self.credentials.refresh(self.http)

    def _smtp_login(self):
        """
        Login in Gmail smtp
        """
        self._refresh_credentials()

        # initialize SMTP procedure
        self.smtp = SMTP('smtp.gmail.com', 587)

        if self.logger.getEffectiveLevel() == logging.DEBUG:
            self.smtp.set_debuglevel(True)
        self.smtp.starttls()
        self.smtp.ehlo()

        # XOATH2 authentication
        smtp_auth_string = 'user={}\1auth=Bearer {}\1\1'
        access_token = self.credentials.access_token
        smtp_auth_string = smtp_auth_string.format(self.username,
                                                   access_token)
        smpt_auth_b64 = base64.b64encode(smtp_auth_string)
        # smtp login
        self.smtp.docmd("AUTH", "XOAUTH2 {}".format(smpt_auth_b64))
        self.smtp.send("\r\n")

    def get(self, id):
        """
        Get a Message from the Gmail message id (as known as X-GM-MSGID).
        """
        self.logger.info('getting message {}'.format(id))
        # this return a message.raw in url safe base64
        message = self.messages.get(userId='me', id=id, format='raw').execute()
        # decode it
        message = base64.urlsafe_b64decode(str(message['raw']))
        self.logger.debug('message id {}\n{}'.format(id, message))
        message = email.message_from_string(message)
        return message

#.........这里部分代码省略.........
开发者ID:jubayerarefin,项目名称:goopg,代码行数:103,代码来源:__init__.py


注:本文中的smtplib.SMTP.send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。