p@21: """ p@21: The MIT License p@21: p@21: Copyright (c) 2007-2010 Leah Culver, Joe Stump, Mark Paschal, Vic Fryzel p@21: p@21: Permission is hereby granted, free of charge, to any person obtaining a copy p@21: of this software and associated documentation files (the "Software"), to deal p@21: in the Software without restriction, including without limitation the rights p@21: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell p@21: copies of the Software, and to permit persons to whom the Software is p@21: furnished to do so, subject to the following conditions: p@21: p@21: The above copyright notice and this permission notice shall be included in p@21: all copies or substantial portions of the Software. p@21: p@21: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR p@21: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, p@21: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE p@21: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER p@21: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, p@21: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN p@21: THE SOFTWARE. p@21: """ p@21: p@21: import oauth2 p@21: import smtplib p@21: import base64 p@21: p@21: p@21: class SMTP(smtplib.SMTP): p@21: """SMTP wrapper for smtplib.SMTP that implements XOAUTH.""" p@21: p@21: def authenticate(self, url, consumer, token): p@21: if consumer is not None and not isinstance(consumer, oauth2.Consumer): p@21: raise ValueError("Invalid consumer.") p@21: p@21: if token is not None and not isinstance(token, oauth2.Token): p@21: raise ValueError("Invalid token.") p@21: p@21: self.docmd('AUTH', 'XOAUTH %s' % \ p@21: base64.b64encode(oauth2.build_xoauth_string(url, consumer, token)))