【问题标题】:Sending outlook mail to multiple users with different email content using python使用python向具有不同电子邮件内容的多个用户发送outlook邮件
【发布时间】:2023-02-08 12:17:05
【问题描述】:

我试图将 outlook 邮件自动发送给具有不同内容的多个收件人,但我一直收到以下错误。

错误:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Pritch\Desktop\email.py", line 19, in <module>
    print(df['Name'][i], df['Email_id'][i])
  File "C:\Users\Pritch\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 3804, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\Pritch\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
    raise KeyError(key) from err

代码:

import pandas as pd
import win32com.client
import time


path = r'c:\Users\Abc\Desktop\content.csv'
df = pd.read_csv(path, sep=';', encoding='cp1252')
print(df)


for i in df.index:
    outlook = win32com.client.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.Subject = 'Test Mail'
    #mail.HTMLBody = '<h3>This is HTML Body</h3>'
    print(df['Name'][i], df['Email_id'][i])
    mail.To = df['Email_id'][i]

    mail.HTMLBody = '''\
        <html>
        <head></head>
        <body>
        <p>Hello ''' + df['Name'][i] + ''',<br>
        <p> Please refer your score.<br>
        <body> ''' + str(df['Content'][i]) + ''' </br></body>
        <font color ='blue'>
       <br>Thanks</>
        </font>
       <br></br>
       <font color ='red'>
        Kindly do not reply to this email.<t1>
       </font>
         </p>
        </body>
        </style>
        </html>
        '''

    mail.Send()

【问题讨论】:

  • 为什么不直接使用df.iterrows?当您打印 df 时,它是否显示您期望的列名称,拼写是否完全一样?

标签: python email outlook win32com office-automation


【解决方案1】:

发送电子邮件的代码看起来不错。我在那里没有看到任何奇怪的东西。

To 类的 MailItem 属性接受 Outlook 项目的 To 收件人的显示名称的分号分隔字符串列表。确保字符串格式正确且编码正确。

【讨论】:

    【解决方案2】:

    这必须在 Python 中完成吗?如果您的数据在 CSV 中,您可能可以访问 Excel,如果可以,您可以在 Excel 中运行下面的代码来实现您的目标。

    In column A : Names of the people
    In column B : E-mail addresses
    In column C:Z : Filenames like this C:DataBook2.xls (don't have to be Excel files)
    

    宏将遍历“Sheet1”中的每一行,如果 B 列中有电子邮件地址,C:Z 列中有文件名,它将创建包含此信息的邮件并发送。

    Sub Send_Files()
    'Working in Excel 2000-2016
        Dim OutApp As Object
        Dim OutMail As Object
        Dim sh As Worksheet
        Dim cell As Range
        Dim FileCell As Range
        Dim rng As Range
    
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set sh = Sheets("Sheet1")
    
        Set OutApp = CreateObject("Outlook.Application")
    
        For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    
            'Enter the path/file names in the C:Z column in each row
            Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    
            If cell.Value Like "?*@?*.?*" And _
               Application.WorksheetFunction.CountA(rng) > 0 Then
                Set OutMail = OutApp.CreateItem(0)
    
                With OutMail
                    .to = cell.Value
                    .Subject = "Testfile"
                    .Body = "Hi " & cell.Offset(0, -1).Value
    
                    For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                        If Trim(FileCell) <> "" Then
                            If Dir(FileCell.Value) <> "" Then
                                .Attachments.Add FileCell.Value
                            End If
                        End If
                    Next FileCell
    
                    .Send  'Or use .Display
                End With
    
                Set OutMail = Nothing
            End If
        Next cell
    
        Set OutApp = Nothing
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    

    https://www.rondebruin.nl/win/s1/outlook/amail6.htm

    【讨论】: