You have to learn Python secretly and stun everyone (Day 6)

Article directory

  • foreword
  • mass mailing
  • code reference
  • Pseudocode 1: Send an empty shell email
  • Pseudocode 2: Constructing email content
  • group email
  • Improve code reusability and usability
  • confession balloon
  • I drew a circle, welcome everyone to our small circle

foreword

Early review: You have to secretly learn Python, and then stun everyone (fifth day)

本系列文默认各位有一定的C或C++基础,因为我是学了点C++的皮毛之后入手的Python。
本系列文默认各位会百度,学习‘模块’这个模块的话,还是建议大家有自己的编辑器和编译器的,上一篇已经给大家做了推荐啦?

然后呢,本系列的目录嘛,说实话我个人比较倾向于那两本 Primer Plus,所以就跟着它们的目录结构吧。

本系列也会着重培养各位的自主动手能力,毕竟我不可能把所有知识点都给你讲到,所以自己解决需求的能力就尤为重要,所以我在文中埋得坑请不要把它们看成坑,那是我留给你们的锻炼机会,请各显神通,自行解决。
1234567

Well, this is a "playful article", let's see how many fun projects we will do today? Sit back and open your compiler, whether it is an online compiler or a PC compiler

img

If you encounter difficulties in learning and want to find a python learning and communication environment, you can scan the QR code of CSDN official certification below on WeChat to join us ==

This article is over, the next one will start crawling

mass mailing

A friend asked me to write the function of mass mailing, so come here? (Manually fill in the emoticon package here)

I have been studying for a few days, so I should be very familiar with the process of this method. What should I do in the first step?

  1. Find the module or package used by mass mailing
  2. Familiar with or understand the use of the module or package
  3. How do you know? Are there examples in the manual?
  4. Modify and modify, such a function will come out

Well, after clarifying the steps, let's start: Python3.9 library function support

···

img
img

code reference

I'm not in a good mood today, so let's directly refer to the code, let's talk about the code

Pseudocode 1: Send an empty shell email

Let's take QQ mailbox as an example (because I only have QQ mailbox)

Before doing this, we have to do one thing first: open the third-party client authorization of QQ mailbox. Our QQ mailbox does not mean that any software can be used to send emails, which requires authorization. How do you do it?

  1. Login QQ mailbox
  2. Settings->Account->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV Service->Activate, activate the first two
  3. Now is 2020-10-29, if the interface is revised in the future, please search for the string of English above to keep pace with the times

After completing these steps, we will get some authorization codes, remember to use the latest authorization code.

# smtplib 用于邮件的发信动作
import smtplib	#引入smtplib模块

# 发信方的信息:发信邮箱,QQ邮箱授权码
from_addr = '[email protected]'	
password = '你的授权码'

# 收信方邮箱
to_addr = '[email protected]'

# 发信服务器
smtp_server = 'smtp.qq.com'	#目前就是这个了,你也不用去百度啦,我当时百度了十分钟,最后确定了就是它。。。

# 开启发信服务,这里使用的是加密传输
server = smtplib.SMTP_SSL()
server.connect(smtp_server,465)	#这个465是端口号,如果不是465就换587试试,实在不行就只能公共端口号25了

# 登录发信邮箱
server.login(from_addr, password)

# 发送邮件
server.sendmail(from_addr, to_addr, msg.as_string())

# 关闭服务器
server.quit()
12345678910111213141516171819202122232425

This is a piece of pseudocode. Remember, this cannot be run. You need to complete the missing information in it. Of course, we are not in a hurry to run it. Wait until I finish.

Pseudocode 2: Constructing email content

The pseudo-code above is the shell for sending emails, and the specific content of the emails has to be realized by the email package.

img

Here we will re-introduce the import statement.

For the email package, there is a difference between the import of this package and the import of modules. I remember that I used import to import which module and then it failed (don’t need to go back and look for it, I am amnesia, I don’t know if it has been published), and now there are results.

This is about the difference between "module" and "package". A module is generally a file, while a package is a directory. A package can contain many modules. It can be said that a package is composed of "module packaging".

In a place you can't see, there is an init.py file that controls all this secretly (of course, there are ways to see it), and init.py controls the import behavior of the package. If this file is empty, then we can't do anything if we just import the package. So direct import email will not work.

Therefore, we need to use the from ... import ... statement to import [needed object] from [a certain file] in the email package directory. For example, import the MIMEText method from the text file under the email package.

MIMEText(msg,type,chartset)
# msg:文本内容,可自定义
# type:文本类型,默认为plain(纯文本)
# chartset:文本编码,中文为“utf-8”
1234

After the steps are here, you can actually send an email (in the previous pseudo-code part, you will find that the msg cannot be filled in, so: msg = MIMEText('send by python','plain','utf-8'))

Friends with strong hands-on ability can try it out by themselves, and friends with weak hands-on ability can follow me. That is, you will find that this is a headerless email.

The header (header, yes it is also called header) is this area, including subject, sender, recipient and other information:

from email.header import Header

msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
msg['Subject'] = Header('python test')
12345

Add this piece to your code, the effect is as follows:

from email.mime.text import MIMEText
from email.header import Header
import smtplib

#这里的邮箱请自己填哦
msg = MIMEText('猜猜我是谁:send by python','plain','utf-8')
smtp_server = 'smtp.qq.com'
from_addr = '[email protected]'
to_addr = '[email protected]'
#to_addr = '[email protected]'

msg['From'] = Header('阿喵')
msg['To'] = Header(to_addr)

msg['Subject'] = Header('这是一份Python发送的邮件哦,今天刚学的哈哈哈')


server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server, 465)

#server = smtplib.SMTP()
#server.connect(smtp_server,25)

server.login(from_addr, 'XXX')#授权码要选最新的
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
1234567891011121314151617181920212223242526

In this code, you fill in the parameters and you can send it.

group email

For group posting, here are two ways, I will talk about one, mention one, and keep one:

The first way is to write to_addrs as a list:

from email.mime.text import MIMEText
from email.header import Header
import smtplib

msg = MIMEText('猜猜我是谁:send by python','plain','utf-8')
smtp_server = 'smtp.qq.com'
from_addr = '[email protected]'
to_addrs = ['[email protected]','[email protected]','[email protected]']

msg['From'] = Header('阿喵')
msg['To'] = Header(",".join(to_addrs))	
#因为server.sendmail(from_addr, to_addrs, msg.as_string())这个函数里面接收的msg参数只能是字符串(不信你把这行去掉试试),所以我们要把这个列表变成字符串

msg['Subject'] = Header('这是一份Python发送的邮件哦,今天刚学的哈哈哈')


server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server, 465)

#server = smtplib.SMTP()
#server.connect(smtp_server,25)

server.login(from_addr, '填你自己的')#授权码要选最新的
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
12345678910111213141516171819202122232425

One thing to mention: Some security optimizations can be done here. Although no one wants to use our code, good habits should be started from an early age.

When I first started to learn programming and worked on the first project, the teacher told us that the password of this project should be made into ciphertext, let the user enter it, and not let others see it. So what's wrong in this code?

1. Our accounts are all public (although the above is a pseudo code) 2. Our authorization code is also public 3. Our code reuse is low and usability is low (this involves the second method)

The solution to the first two problems is also very simple, just input. But what about that list? What should I do? while loop! !

to_addrs = []
while True:
    a=input('请输入收件人邮箱:')
    #输入收件人邮箱
    to_addrs.append(a)
    #写入列表
    b=input('是否继续输入,n退出,任意键继续:')
    #询问是否继续输入
    if b == 'n':
        break
12345678910

Improve code reusability and usability

What is code reusability? It's your code, today I want to send these five people in a group, okay, tomorrow I want to send another six people in a group? what to do? Go in and change the code. If it comes to the step of changing the code, then the reusability design of the code is too poor.

So what is code availability? May I ask if what you write is for people who can write code? That's the same question as above, what if she wants to send it to a different group of people? One day I want to send more messages to one person, and another day I want to send one less person, what should I do?

It's not easy to use, she won't.

So what to do? Remember the modules we used to operate Excel? Right, try it yourself.

confession balloon

The above is more serious, and I will take everyone to do it step by step. Next, everyone will look at a piece of code to see how much they can understand.

I drew a circle, welcome everyone to our small circle

import turtle
import time

# 画心形圆弧
def chage_angle():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2);

def move_position(x,y):
    turtle.hideturtle() # 隐藏画笔(先)
    turtle.up()  # 提笔
    turtle.goto(x,y)# 移动画笔到指定起始坐标(窗口中心为0,0)
    turtle.down()  # 下笔
    turtle.showturtle()  # 显示画笔

love = input("请输入表白语: ")
signature = input("请输入签名: ")
if love == '':
    love = "I LOVE YOU"


turtle.setup(width=800, height=500)     # 窗口(画布)大小
turtle.color('red', 'pink')     # 画笔颜色
turtle.pensize(3)       # 画笔粗细
turtle.speed(1)     # 描绘速度
# 初始化画笔起始坐标
move_position(x=0,y=-180)   # 移动画笔位置
turtle.left(140)    # 向左旋转140度

turtle.begin_fill()     # 标记背景填充位置

# 画心形直线( 左下方 )
turtle.forward(224)    # 向前移动画笔,长度为224
# 画爱心圆弧
chage_angle()      # 左侧圆弧
turtle.left(120)    # 调整画笔角度
chage_angle()      # 右侧圆弧
# 画心形直线( 右下方 )
turtle.forward(224)

turtle.end_fill()       # 标记背景填充结束位置

# 在心形中写上表白话语
move_position(0,20)      # 表白语位置
turtle.hideturtle()     # 隐藏画笔
turtle.color('#CD5C5C', 'pink')      # 字体颜色
# font:设定字体、尺寸(电脑下存在的字体都可设置)
turtle.write(love, font=('Arial', 30, 'bold'), align='center')

# 签写署名
if signature != '':
    turtle.color('red', 'pink')
    time.sleep(2)
    move_position(180, -180)
    turtle.hideturtle()  # 隐藏画笔
    turtle.write(signature, font=('Arial', 20), align="left")
    
    # 点击窗口关闭程序
window = turtle.Screen()
window.exitonclick()
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061

I'll see if I can add a video

img

That can't be helped, it's not that I don't add videos

You can try it yourself

Welcome to the Python community

If you have any difficulties in learning python and don’t understand, you can scan the CSDN official certification QR code below on WeChat to join python exchange learning, exchange questions, and help each other. There are good learning tutorials and development tools here.

insert image description here

Guess you like

Origin blog.csdn.net/libaiup/article/details/131895520