classEmailServerCredentials(Block):""" Block used to manage generic email server authentication. It is recommended you use a [Google App Password](https://support.google.com/accounts/answer/185833) if you use Gmail. Attributes: username: The username to use for authentication to the server. Unnecessary if SMTP login is not required. password: The password to use for authentication to the server. Unnecessary if SMTP login is not required. smtp_server: Either the hostname of the SMTP server, or one of the keys from the built-in SMTPServer Enum members, like "gmail". smtp_type: Either "SSL", "STARTTLS", or "INSECURE". smtp_port: If provided, overrides the smtp_type's default port number. Example: Load stored email server credentials: ```python from prefect_email import EmailServerCredentials email_credentials_block = EmailServerCredentials.load("BLOCK_NAME") ``` """# noqa E501_block_type_name="Email Server Credentials"_logo_url="https://cdn.sanity.io/images/3ugk85nk/production/82bc6ed16ca42a2252a5512c72233a253b8a58eb-250x250.png"# noqa_documentation_url="https://prefecthq.github.io/prefect-email/credentials/#prefect_email.credentials.EmailServerCredentials"# noqausername:Optional[str]=Field(default=None,description=("The username to use for authentication to the server. ""Unnecessary if SMTP login is not required."),)password:SecretStr=Field(default_factory=partial(SecretStr,""),description=("The password to use for authentication to the server. ""Unnecessary if SMTP login is not required."),)smtp_server:Union[SMTPServer,str]=Field(default=SMTPServer.GMAIL,description=("Either the hostname of the SMTP server, or one of the ""keys from the built-in SMTPServer Enum members, like 'gmail'."),title="SMTP Server",)smtp_type:Union[SMTPType,str]=Field(default=SMTPType.SSL,description=("Either 'SSL', 'STARTTLS', or 'INSECURE'."),title="SMTP Type",)smtp_port:Optional[int]=Field(default=None,description=("If provided, overrides the smtp_type's default port number."),title="SMTP Port",)@validator("smtp_server",pre=True)def_cast_smtp_server(cls,value):""" Cast the smtp_server to an SMTPServer Enum member, if valid. """return_cast_to_enum(value,SMTPServer)@validator("smtp_type",pre=True)def_cast_smtp_type(cls,value):""" Cast the smtp_type to an SMTPType Enum member, if valid. """ifisinstance(value,int):returnSMTPType(value)return_cast_to_enum(value,SMTPType,restrict=True)defget_server(self)->SMTP:""" Gets an authenticated SMTP server. Returns: SMTP: An authenticated SMTP server. Example: Gets a GMail SMTP server through defaults. ```python from prefect import flow from prefect_email import EmailServerCredentials @flow def example_get_server_flow(): email_server_credentials = EmailServerCredentials( username="username@gmail.com", password="password", ) server = email_server_credentials.get_server() return server example_get_server_flow() ``` """smtp_server=self.smtp_serverifisinstance(smtp_server,SMTPServer):smtp_server=smtp_server.valuesmtp_type=self.smtp_typesmtp_port=self.smtp_portifsmtp_portisNone:smtp_port=smtp_type.valueifsmtp_type==SMTPType.INSECURE:server=SMTP(smtp_server,smtp_port)else:context=ssl.create_default_context()ifsmtp_type==SMTPType.SSL:server=SMTP_SSL(smtp_server,smtp_port,context=context)elifsmtp_type==SMTPType.STARTTLS:server=SMTP(smtp_server,smtp_port)server.starttls(context=context)ifself.usernameisnotNone:server.login(self.username,self.password.get_secret_value())returnserver
classSMTPServer(Enum):""" Server used to send email. """AOL="smtp.aol.com"ATT="smtp.mail.att.net"COMCAST="smtp.comcast.net"ICLOUD="smtp.mail.me.com"GMAIL="smtp.gmail.com"OUTLOOK="smtp-mail.outlook.com"YAHOO="smtp.mail.yahoo.com"