CADとPython学習 CADからメール送付
Vectorworksからメール送付のサンプルスクリプトです。このまま実行してもエラーが出ます。mac環境でファイルパスの設定が必要です。
※テキストファイルは1行目 送信元アドレス、2行目パスワード、3行目以降空白行までが送付先のプルダウンのメールアドレスとなります。
import subprocess
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def uploadDialog(members,row): # ダイアログの設定
kOK = 1
kCANCEL = 2
kSETUPDIALOG = 12255
def create_dialog(row):
dialog = vs.CreateLayout("送信先とメッセージの入力", False, "OK", "キャンセル")
vs.CreateListBoxN(dialog, 10, 35, row, True)
vs.SetFirstLayoutItem(dialog, 10)
vs.CreateEditTextBox(dialog, 20, "件名", 35, 2)
vs.SetBelowItem(dialog, 10, 20, 0, 2)
vs.CreateEditTextBox(dialog, 30, "メッセージ", 35, 6)
vs.SetBelowItem(dialog, 20, 30, 0, 2)
return dialog
def dialog_handler(item,data):
if item == kOK:
outCount = vs.GetChoiceCount(dialog, 10) #項目数
adresses_set = set() #空の集合を用意
for index in range(outCount):
ind, text = vs.GetSelectedChoiceInfo(dialog, 10, index)
if ind >= 0:
adresses_set.add(text)
adresses = list(adresses_set)
uploadDialog.adress = adresses
uploadDialog.title = vs.GetItemText(dialog, 20)
uploadDialog.message = vs.GetItemText(dialog, 30)
elif item == kCANCEL:
pass
elif item == kSETUPDIALOG:
for member in members:
vs.AddChoice(dialog, 10, member, 0)
return item
dialog = create_dialog(row)
result = False
uploadDialog.adress = ""
uploadDialog.title = ""
uploadDialog.message = ""
mailMessage = {}
if vs.RunLayoutDialog(dialog, dialog_handler) == kOK:
result = True
mailMessage = {
"送信先": uploadDialog.adress,
"件名": uploadDialog.title,
"内容": uploadDialog.message,
}
return (
result,mailMessage
)
def read_credentials(file_path):
with open(file_path, 'r') as file:
email = file.readline().strip()
password = file.readline().strip()
addresses = []
for line in file:
stripped_line = line.strip()
if stripped_line: # 空行でない場合にリストに追加
addresses.append(stripped_line)
else: # 空行が出現した場合、ループを終了
break
addresses.reverse()
return email, password, addresses, len(addresses)
def take_screenshot_with_applescript(screenshot_path):
applescript_command = f"""
tell application "System Events"
do shell script "screencapture {screenshot_path}"
end tell
"""
subprocess.run(["osascript", "-e", applescript_command])
def send_mail_with_attachment(sender_email, password, receiver_email, subject, body, attachment_path):
try:
# MIMEメッセージを作成
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = ', '.join(receiver_emails) # 受信者のリストをカンマ区切りの文字列に変換
msg['Subject'] = subject
# メールの本文を追加
msg.attach(MIMEText(body, 'plain'))
# 添付ファイルを追加
with open(attachment_path, 'rb') as attachment:
img = MIMEImage(attachment.read())
img.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment_path))
msg.attach(img)
# GmailのSMTPサーバーを使ってメールを送信
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
text = msg.as_string()
server.sendmail(sender_email, receiver_emails, text) # すべての受信者にメールを送信
server.quit()
return "メールが正常に送信されました。"
except Exception as e:
return f"メール送信中にエラーが発生しました: {e}"
# メールアドレスとパスワードのファイルパス
credentials_file_path = "/Users/*****/Library/Application Support/Vectorworks/2021/Plug-ins/mailpass.txt"
# 認証情報をファイルから読み込む
your_email, your_password,addresses,row = read_credentials(credentials_file_path)
# スクリーンショットのパス
screenshot_path = "/Users/*****/Desktop/screenshot.png"
# スクリーンショットを撮る
take_screenshot_with_applescript(screenshot_path)
# 受信者のメールアドレス、件名、本文を設定
email, password, members,row = read_credentials(credentials_file_path)
result,mailMessage = uploadDialog(members,(row+1))
receiver_emails = mailMessage["送信先"]
subject = mailMessage["件名"]
body = mailMessage["内容"]
if result:
if receiver_emails == "":
vs.AlrtDialog("メールアドレスが入力されていません")
# メールを送信し、スクリーンショットを削除
else:
result = send_mail_with_attachment(your_email, your_password, receiver_emails, subject, body, screenshot_path)
#vs.AlrtDialog(result)
# スクリーンショットを削除
os.remove(screenshot_path)
else:
pass