Python ile Excel'deki kullanıcıların listesine ekli Outlook e-postası gönderin
Aug 16 2020
Outlook'u aşağıdaki komut dosyasıyla kullanarak e-posta gönderebiliyorum, ancak ek göndermeye çalışırsam hata veriyor.
Dosya Adı_Email.xlsx
NAME EMAIL
Roy [email protected]
Jack [email protected]
Python Komut Dosyası
import win32com.client as win32
import pandas as pd
email_list = pd.read_excel(r'C:\Users\roy\Name_Email.xlsx')
names = email_list['NAME']
emails = email_list['EMAIL']
for i in range(len(emails)):
name = names[i]
email = emails[i]
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = email
mail.Subject = 'Message subject'
mail.Body = 'Hello ' + name
attachment = "hosts.txt"
mail.Attachments.Add(attachment)
mail.Send()
alıyorum hata:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)
Dosya adı doğru ve komut dosyasıyla aynı dizinde.
Ayrıca eki = 'hosts.txt' değiştirmeye çalıştım, ancak aynı hata. Neyin eksik olduğundan emin değilim.
Yanıtlar
1 SrinathNeela Aug 16 2020 at 21:00
bunu dene
from win32com.client import Dispatch
import win32com
import pandas as pd
def mailprepare():
num = range(0, 5)
for kk in num:
outlook = win32com.client.Dispatch("Outlook.Application")
for accoun in outlook.Session.Accounts:
if accoun.SmtpAddress == '[email protected]':
newaccount = accoun
break
mail = outlook.CreateItem(0)
mail._oleobj_.Invoke(*(64209, 0, 8, 0, newaccount))
data = pd.ExcelFile('D:\\path.xlsx')
sheet = data.parse('Sheet1')
name = sheet['Names'][kk]
mailto = sheet['Mails'][kk]
att = 'D:\\yourattchment.txt'
with open(att, 'r') as my_attch:
myfile=my_attch.read()
mail.To = mailto
mail.Subject = 'Subject'
mail.Body = 'mail body'+ name
mail.Attachments.Add(att)
mail.Display(True)
mail.send
mailprepare()
0m3r Aug 17 2020 at 08:25
Yalnızca hosts.txt
bir değişkene atıyorsunuz , ancak Ekler.Add , bir dosya adı ile tam dosya sistemi yolunu arıyor
Mevcut dizini almak için şunu kullanın:
Misal
import os
print(os.getcwd() + "\hosts.txt")
Veya
import os
attachment = "hosts.txt"
print(os.path.realpath(attachment))
Bilginize - 3.8 python kullanıyorum
Donovan, Şarkılarından 1'ini The Beatles'ın "Lucy in the Sky with Diamonds" şarkısıyla karşılaştırdı
Kevin Jonas'ın Kızı Alena, Doğum Günü Fotoğrafında Büyümüş Görünüyor: '9 Yaşında Gerçek Hissetmiyor'
Charly Reynolds Yakın Zamandaki Vokal Kord Ameliyatını Açıkladı: 'Şarkı Söylemekte Sorun Yaşıyordum'