代码之家  ›  专栏  ›  技术社区  ›  Dance Party2

Django电子邮件-定义用户名和密码

  •  0
  • Dance Party2  · 技术社区  · 7 年前

    documentation 这能做到吗?如果是,如何?

    from django.core.mail import EmailMessage
    
    email = EmailMessage(
        'Hello',
        'Body goes here',
        'from@example.com',
        ['to1@example.com', 'to2@example.com'],
        ['bcc@example.com'],
        reply_to=['another@example.com'],
        headers={'Message-ID': 'foo'},
    )
    
    message.attach_file('/images/weather_map.pdf')
    

    提前感谢!

    更新:

    我希望避免将凭据存储在任何文件中。最后,我希望代码提示输入用户名和密码作为输入变量。 更新:

    import pandas as pd
    from django.core.mail import EmailMessage
    from django.core.mail.backends.smtp import EmailBackend
    attachment_path=r'C:\path'+'\\'
    
    connection = EmailBackend(
        host='host',
        port=587,
        username='login',
        password='password'
    )
    
    email = EmailMessage(
        'Hello',
        'Body goes here',
        'example@example.com',
        ['example@example.com'],
        ['example@example.com'],
        reply_to=['example@example.com'],
        headers={'Message-ID': 'foo'},
        connection=connection
    )
    email.attach_file(attachment_path+'attachment.pdf')
    email.send()
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   Alasdair    7 年前

    您可以使用 get_connection

    from django.core.mail import get_connection
    
    connection = get_connection(
        host='...',
        port='...',
        username='...',
        ...
    )
    

    EmailMessage .

    email = EmailMessage(
        'Hello',
        'Body goes here',
        'from@example.com',
        ['to1@example.com', 'to2@example.com'],
        ['bcc@example.com'],
        reply_to=['another@example.com'],
        headers={'Message-ID': 'foo'},
        connection=connection,
    )
    
        2
  •  0
  •   joeskru    7 年前

    您可以在任何地方导入该类(例如views.py等):

    from django.core.mail import EmailMessage
    

    然后在文档中称之为。。

    following the tutorial