Log in on an SMTP server that requires authentication. The arguments are: - user: The user name to authenticate with. - password: The password for the authentication. Keyword arguments: - initial_response_ok: Allow sending the
(self, user, password, *, initial_response_ok=True)
| 684 | return self.password |
| 685 | |
| 686 | def login(self, user, password, *, initial_response_ok=True): |
| 687 | """Log in on an SMTP server that requires authentication. |
| 688 | |
| 689 | The arguments are: |
| 690 | - user: The user name to authenticate with. |
| 691 | - password: The password for the authentication. |
| 692 | |
| 693 | Keyword arguments: |
| 694 | - initial_response_ok: Allow sending the RFC 4954 initial-response |
| 695 | to the AUTH command, if the authentication methods supports it. |
| 696 | |
| 697 | If there has been no previous EHLO or HELO command this session, this |
| 698 | method tries ESMTP EHLO first. |
| 699 | |
| 700 | This method will return normally if the authentication was successful. |
| 701 | |
| 702 | This method may raise the following exceptions: |
| 703 | |
| 704 | SMTPHeloError The server didn't reply properly to |
| 705 | the helo greeting. |
| 706 | SMTPAuthenticationError The server didn't accept the username/ |
| 707 | password combination. |
| 708 | SMTPNotSupportedError The AUTH command is not supported by the |
| 709 | server. |
| 710 | SMTPException No suitable authentication method was |
| 711 | found. |
| 712 | """ |
| 713 | |
| 714 | self.ehlo_or_helo_if_needed() |
| 715 | if not self.has_extn("auth"): |
| 716 | raise SMTPNotSupportedError( |
| 717 | "SMTP AUTH extension not supported by server.") |
| 718 | |
| 719 | # Authentication methods the server claims to support |
| 720 | advertised_authlist = self.esmtp_features["auth"].split() |
| 721 | |
| 722 | # Authentication methods we can handle in our preferred order: |
| 723 | preferred_auths = ['CRAM-MD5', 'PLAIN', 'LOGIN'] |
| 724 | |
| 725 | # We try the supported authentications in our preferred order, if |
| 726 | # the server supports them. |
| 727 | authlist = [auth for auth in preferred_auths |
| 728 | if auth in advertised_authlist] |
| 729 | if not authlist: |
| 730 | raise SMTPException("No suitable authentication method found.") |
| 731 | |
| 732 | # Some servers advertise authentication methods they don't really |
| 733 | # support, so if authentication fails, we continue until we've tried |
| 734 | # all methods. |
| 735 | self.user, self.password = user, password |
| 736 | for authmethod in authlist: |
| 737 | method_name = 'auth_' + authmethod.lower().replace('-', '_') |
| 738 | try: |
| 739 | (code, resp) = self.auth( |
| 740 | authmethod, getattr(self, method_name), |
| 741 | initial_response_ok=initial_response_ok) |
| 742 | # 235 == 'Authentication successful' |
| 743 | # 503 == 'Error: already authenticated' |
no test coverage detected