我尝试制作如下所示的脚本。 该脚本在所有其他方面都完美运行,除了它不会在“收件人”字段中生成重新收件人。有什么想法吗? 还添加了生成的 QR 图像。
import qrcode
import os
import urllib.parse
# Define the mailto URL components
recipient = "[email protected]"
subject = "Fault report"
body = "The machine is broken. HEEELP!"
# Encode the subject and body
subject_encoded = urllib.parse.quote(subject)
body_encoded = urllib.parse.quote(body)
# Construct the mailto URL
mailto_url = f"mailto:{recipient}?subject={subject_encoded}&body={body_encoded}"
# Print the mailto URL for debugging
print(f"Mailto URL: {mailto_url}")
# Create QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(mailto_url)
qr.make(fit=True)
# Create an image from the QR Code instance
img = qr.make_image(fill='black', back_color='white')
# Save the image to a file
filename = "Fault_qr.png"
current_directory = os.getcwd()
file_path = os.path.join(current_directory, filename)
print(f"Current directory: {current_directory}")
print(f"Saving file to: {file_path}")
img.save(file_path)
print(f"QR code generated and saved as {filename}")
该脚本应该生成一个二维码,人们可以扫描该二维码以报告预定收件人电子邮件的错误。二维码错误报告
我认为问题是二维码生成库通常不会修改它们编码的 URL 内容。他们只是将其视为一串数据。 通过向收件人提供完整的 mailto URL,编码的 QR 码数据将包含扫描和打开包含所需收件人、主题和正文的新电子邮件所需的所有信息。
你可以试试这个代码。
import qrcode
import os
mailto_url = f"mailto:[email protected]?subject=Fault%20report&body=The%20machine%20is%20broken.%20HEEELP!"
print(f"Mailto URL: {mailto_url}")
qr = qrcode.QRCode(
version=1, # Adjust version for desired size and complexity
error_correction=qrcode.constants.ERROR_CORRECT_L, # Error correction level
box_size=10, # Size of each box in the QR code
border=4, # Width of the border around the QR code
)
qr.add_data(mailto_url) # Add the mailto URL as data
qr.make(fit=True) # Ensure data fits within the QR code
img = qr.make_image(fill_color='black', back_color='white') # Use descriptive color names
filename = "Fault_qr.png"
file_path = os.path.join(os.getcwd(), filename)
print(f"Saving QR code as: {file_path}")
img.save(file_path)
print(f"QR code generated and saved as {filename}")