Skip to main content

发送邮件库

Project description

使用方式

发布地址:

github地址 https://github.com/majormj/snemail

gitee 地址 https://gitee.com/manjim/snemail

测试环境

目前测试支持的python版本3.6,和python 3.11 如果登录失败遇到错误 con 不存在,可尝试修改python版本的判断范围尝试(ssl的校验方式)

def mail_login(self,user="",password=""):
    """登录邮箱"""

    self.emailCome = user       # 邮件的时间发送人(发邮件的时候调用)
    self.loginStatus = False    # 登录结果状态
    try:
        # 1、链接邮箱服务器(带 SSL 启用安全机制) # 端口号:465/25
        # 1.1 判断python版本
        vamax = sys.version_info.major   #  大版本号例如3.6.8  则表示3
        vsmin = sys.version_info.minor   #  小版本号例如3.6.8  则表示6

        if vamax == 3 and vsmin < 10:  #  python 版本号等于3,小版本号小于10

            self.con = smtplib.SMTP_SSL(self.server,self.port) 

        else:
            """ Python 3.10 及以上版本使用"""
            import ssl
            ctx = ssl.create_default_context()
            ctx.set_ciphers('DEFAULT')
            self.con = smtplib.SMTP_SSL(self.server,port=self.port, context = ctx)

        # 2、登录邮箱 # 链接对象.login(账号、密码)
        self.con.login(user,password) 
        self.loginStatus = True
        return '邮箱登录成功'
    except SMTPAuthenticationError as e:
        return '登录失败,请检查账户密码是否正确,或者尝试授权码登录%s'%repr(e)
    except Exception as error:
        return '邮箱登录遇到错误:%s'%repr(error)

依赖说明

包依赖 smtplib 模块,和 email 模块,这两个库在python11中是标准库。如果在其他python中不存在折两个库 直接 pip install smtplib 可能安装失败,可以试下安装 PyEmail,但是这个没试过不确定能不能用

pip install  PyEmail

安装包

# 安装库到最新版本
pip install --upgrade snemail

使用方式

# 引入包
from snemail import SnEmail

# 邮件服务器
server  = 'smtp.sina.net'

# 邮件端口
port    = 465

if __name__ == '__main__':
    # 初始化
    sn = SnEmail(server,port)

    # 设置邮件主题
    sn.mail_title(title="测试邮件")

    # 设置邮件正文内容,支持 文本 和thml格式 ,
    """
    添加邮件内容 参数说明
    content : 邮件内容
    ctype   : 邮件类型 plain 文本类型;html 为HTML类型;base64(二进制文件,也就是附件)
    encode  : 编码方式,默认 'utf-8'
    """
    sn.mail_content("文本内容1")
    sn.mail_content("文本内容2")


    # 将图片以内容形式添加到邮件正文,图片地址字符串类型
    sn.mail_image(image_path="图片地址1")
    sn.mail_image(image_path="图片地址2")

    # 添加邮件附件(中英文文件名都支持),传入参数 files 必须是列表
    sn.mail_file([r"C:\Users\manji\Downloads\全量商品列表.xlsx"])

    # 登录邮箱
    print(sn.login(user="",password=""))

    # 发送邮件,扩展参数可查看函数提示
    """
    发送邮件
    emailTo       收件人列表
    emailCC       抄送人列表
    emailNoSend   不发送的对象(会显示,实际不发送)
    emailComeShow 显示的发件人(和实际发件人可能不是同一个)
    """
    print(sn.mail_send(emailTo=['']))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snemail-1.1.16.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snemail-1.1.16-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file snemail-1.1.16.tar.gz.

File metadata

  • Download URL: snemail-1.1.16.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for snemail-1.1.16.tar.gz
Algorithm Hash digest
SHA256 0bc4ca78267f0d821b4408215d2be3ee7bb8133854f44aac39bab76561bbd9a6
MD5 4c8d0fd30f0d34676e3bd8dd2c174c3b
BLAKE2b-256 207a6abd81b6cd770554acef5939540922548a53bbcb62d9d0a5603a4d6ddc8f

See more details on using hashes here.

File details

Details for the file snemail-1.1.16-py3-none-any.whl.

File metadata

  • Download URL: snemail-1.1.16-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for snemail-1.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 c9347dd58d18509f7760a247ea17b1c51002e61747b01c63a7bc15e10af99a9a
MD5 1684e165d3842b2c5ef025faa5f46455
BLAKE2b-256 c9d81c557979f61ff282a7be3e907b054fc0e210fd9fdf20d429bcd98c9d239c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page