私は3つのメールアドレスを持つpython配列を持っています:
email_group = df.EMAIL.unique()
[email protected]
[email protected]
[email protected]
電子メールの配列をループし、最初の電子メールアドレスを「mail.To」フィールドに割り当てて送信するにはどうすればよいですか?次に、2番目の電子メールアドレスにループして送信し、最後に配列の最終アドレスを含む3番目の電子メールを送信します。
最終結果:ループを使用して、配列内の各アドレスに1通のメールを送信する必要があります。
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'POPULATED FROM ARRAY LOOP'
mail.Subject = 'Report'
mail.Body = """Report is attached."""
mail.Send()
オプション1:クラスの使用
email_addresses = ['[email protected]', '[email protected]', '[email protected]']
class EmailsSender:
def __init__(self):
self.outlook = win32.Dispatch('outlook.application')
def send_email(self, to_email_address, attachment_path):
mail = self.outlook.CreateItem(0)
mail.To = to_email_address
mail.Subject = 'Report'
mail.Body = """Report is attached."""
if attachment_path:
mail.Attachments.Add(Source=attachment_path, Type=olByValue)
mail.Send()
def send_emails(self, email_addresses, attachment_path=None):
for email in email_addresses:
self.send_email(email, attachment_path)
attachment_path = 'Enter report path here'
email_sender = EmailsSender()
email_sender.send_emails(email_addresses, attachment_path)
オプション2:関数の使用
outlook = win32.Dispatch('outlook.application')
def send_email(outlook, to_email_address, attachment_path):
mail = outlook.CreateItem(0)
mail.To = to_email_address
mail.Subject = 'Report'
mail.Body = """Report is attached."""
if attachment_path:
mail.Attachments.Add(Source=attachment_path, Type=olByValue)
mail.Send()
attachment_path = 'Enter report path here'
for email in email_addresses:
send_email(outlook, email_addresses)
オプション3:Just It
email_addresses = ['[email protected]', '[email protected]', '[email protected]']
outlook = win32.Dispatch('outlook.application')
attachment_path = 'Enter report path here'
for email in email_addresses:
mail = outlook.CreateItem(0)
mail.To = email
mail.Subject = 'Report'
mail.Body = """Report is attached."""
mail.Attachments.Add(Source=attachment_path, Type=olByValue)
mail.Send()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加