简体   繁体   English

Python:创建用户并向用户发送包含帐户详细信息的电子邮件

[英]Python: Create a user and send email with account details to the user

Here is a script I have written which will create a new user account. 这是我编写的脚本,它将创建一个新的用户帐户。 I am trying to get help in adding a bit more to it. 我正在寻求帮助,以增加更多的东西。

I want to have it also send an email to the new user that is created. 我还希望它还向创建的新用户发送电子邮件。 Ideally, the program will ask the user creating the new account, what their email is, and then it will use the user and password variables and send an email to that new user so they will know how to log in. What would be the best way to do this, thanks for any advice. 理想情况下,该程序将询问创建新帐户的用户,他们的电子邮件是什么,然后它将使用用户和密码变量,然后向该新用户发送电子邮件,以便他们知道如何登录。方式,谢谢您的任何建议。

#! /usr/bin/python

import commands, os, string
import sys
import fileinput


def print_menu():       ## Your menu design here
    print 20 * "-" , "Perform Below Steps to Create a New TSM Account." , 20 * "-"
print "1. Create User Account"
print 67 * "-"

loop=True      

while loop:          ## While loop which will keep going until loop = False
    print_menu()    ## Displays menu
    choice = input("Enter your choice [1-5]: ")

    if choice==1:
        user = raw_input("Enter the Username to be created: " )
        password = raw_input( "Enter the password for the user: "  )
        SRnumber = raw_input( "Enter the Service Request Number: ")

        user = user + " "

        output = os.system('create user' + user)
        output = os.system('set password' + password)

You can easily send mails with gmail and smtplib (you maybe need to install it first). 您可以轻松地使用gmail和smtplib发送邮件(您可能需要先安装它)。 This way you can send any message you want. 这样,您可以发送所需的任何消息。

import smtplib

toaddrs = raw_input('what is your e mail?')
fromaddr = 'youremail@email.com'

msg = 'the message you want to send'
server.starttls()
server.login(fromaddr, "your gmail password")
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

You will have to allow less secure apps in your gmail settings. 您必须在Gmail设置中允许安全性较低的应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM