一个 Django 应用程序,使用户能够从 Gmail 地址授予对 Gmail 应用程序的访问权限,并代表这些地址以编程方式发送电子邮件。
项目描述
MyCustomMailer 是一个 Django 应用程序,用于通过简单的前端流程对 Gmail 进行身份验证,然后以编程方式用于发送电子邮件。
先决条件
在 console.cloud.google.com 上创建应用程序,启用 Gmail API 并获取 credentials.json
通过 Google 访问本教程https://developers.google.com/workspace/guides/create-project
按照步骤在云中创建应用程序
启用 Gmail API
将 localhost 或生产链接(或两者)添加为授权重定向 URI。这是 setting.py 中使用的两个变量(参见步骤 4)。
最后下载 client_secret.json 并将其放在项目中的某个位置,您稍后将需要它。
快速开始
将“mcmailer”添加到您的 INSTALLED_APPS 设置中,如下所示:
INSTALLED_APPS = [ ... 'mcmailer', ]
在项目 urls.py 中包含 polls URLconf,如下所示:
path('mcmailer/', include('mcmailer.urls')),
运行python manage.py migrate创建 mcmailer 模型。
在 settings.py 中,您必须声明以下四个设置:
JSON_PATH = "/my/path/to/client_secret.json" LOCAL_HOST_REDIRECT_URI = "http://localhost:8000/mcmailer/g-auth-endpoint/" PRODUCTION_REDIRECT_URI = "https://example.com/mcmailer/g-auth-endpoint/"
JSON_PATH 设置是“credentials.json”所在的路径。
LOCAL_HOST_REDIRECT_URI 设置是您将在 console.cloud.google.com 中的凭据中放入的授权重定向 URI 之一(或唯一一个)。如果 DEBUG 等于 True,将使用此 URI。URI 必须指向特定视图,因此只需更改 /mcmailer/g-auth-endpoint/ 之前的部分(如果需要)。示例:LOCAL_HOST_REDIRECT_URI = “ http://localhost:8000/mcmailer/g-auth-endpoint/ ”
PRODUCTION_REDIRECT_URI 设置是您将在 console.cloud.google.com 中的凭据中放入的授权重定向 URI 之一(或唯一一个)。如果 DEBUG 等于 False,将使用此 URI。URI 必须指向特定视图,因此只需更改 /mcmailer/g-auth-endpoint/ 之前的部分(如果需要)。示例:LOCAL_HOST_REDIRECT_URI = “ https://example.com/mcmailer/g-auth-endpoint/ ”
启动开发服务器并访问http://127.0.0.1:8000/mcmailer/connect/输入您想要访问您的应用程序的电子邮件,然后单击提交。
9. 现在您已成功授予至少一个 Gmail 对您的应用程序的访问权限,您可以使用包的 send_mc_email() 函数从该电子邮件发送电子邮件:
from mcmailer.utils.sendgmail import send_mc_email, load_credentials authorized_gmail_address = 'someaddress@gmail.com' success, credentials = load_credentials(authorized_gmail_address) send_mc_email( credentials, from_address=authorized_gmail_address, to_addresses=[ 'toaddress2@gmail.com', 'toaddress3@gmail.com', 'toaddress4@gmail.com' ], from_official_name='MY OFFICIAL NAME', subject='My Subject', msg_plain='My Plain Email Body', msg_html='My HTML Email Body' )
现在,任何拥有 Gmail 的用户都可以按照授权流程 ( http://127.0.0.1:8000/mcmailer/connect/ ) 授予对您的应用程序的“gmail.send”访问权限,然后您将能够代表发送电子邮件他们。
如果您的应用不需要在生产环境中对新用户进行身份验证,并且您只需对 Gmail 进行身份验证以通过编程方式发送电子邮件,那么您只需在本地对 Gmail 进行身份验证一次,然后从 settings.py 中删除包变量和包的 URL 路径网址.py。只需将“mcmailer”留在您的 INSTALLED_APPS 中即可以编程方式发送电子邮件。
项目详情
django- mcmailer -1.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b555e0379d258de13ca8697a74fa5a8c02152d03f104f94071e0cca390ee2730 |
|
MD5 | 9c2c840f25719cf8ea06021698570f19 |
|
布莱克2-256 | 9630812b0bdc8bbd07daaf561d48f91c1daecdbf0f46901c20c820ed3c4bb03a |