• 31-07-2023, 13:09:23
    #1
    Başlıkta da belirtiğim gibi excel projemde e-posta ekinde göndermem gereken bazı alanlar olduğu için aşağıdaki makroyu hazırladım.
    Elle göndermem gereken Sütunları seçip makroyu çalıştırıp gönderiyorum.

    Gönderdiği e-postanın ekini kontrol ettiğimde "tarih alanlarının biçiminin bozulduğunu" fark ettim.
    Bu sorunu nasıl düzeltebilirim?

    Sub MailGonderXLS()
    
        Dim wPath As String, wFile As String
        Dim x As Date
        Set WshShell = CreateObject("WScript.Shell")
        strDesktop = WshShell.SpecialFolders("Desktop")
    
          Selection.Copy
         Set yeni = Workbooks.Add
         yeni.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
         yeni.SaveAs strDesktop & "\Seçilen.xls"
    
        Set dam = CreateObject("Outlook.Application").CreateItem(0)
        '
        dam.To = "onurturan@hotmail.com"
        dam.Subject = "D.Talepler"
        dam.Body = "Bu bir test e-postasıdır."
        dam.Attachments.Add strDesktop & "\Seçilen.xls"
        dam.Send
        MsgBox "Mail gönderildi."
    
    End Sub
  • 31-07-2023, 13:10:57
    #2
    usta python ile yapsana daha kolay değilmi
  • 31-07-2023, 13:13:39
    #3
    shms adlı üyeden alıntı: mesajı görüntüle
    usta python ile yapsana daha kolay değilmi
    hocam,
    genel olarak birimlerimizde excel kullandığımız için excelde yapmayı tercih ettim.
    python'a aktardığımda masaüstü uygulama olarak mı kullanabilirim?
  • 31-07-2023, 13:17:23
    #4
    import win32com.client as win32
    import win32com.client.makepy as makepy
    import poplib
    from email import parser
    
    makepy.GenerateFromTypeLibSpec("Outlook.Application")
    
    def get_email_body(pop_server, username, password):
        pop_conn = poplib.POP3_SSL(pop_server)
        pop_conn.user(username)
        pop_conn.pass_(password)
    
        num_messages = len(pop_conn.list()[1])
        if num_messages == 0:
            pop_conn.quit()
            raise Exception("No messages in the mailbox")
    
        # Assuming we want to read the latest email (index starts from 1)
        latest_email_index = num_messages
    
        # Retrieve the latest email
        response, msg_lines, octets = pop_conn.retr(latest_email_index)
        message_content = b"\r\n".join(msg_lines).decode('utf-8')
    
        # Parse the email content
        msg = parser.Parser().parsestr(message_content)
        body = ""
        if msg.is_multipart():
            for part in msg.walk():
                if part.get_content_type() == 'text/plain':
                    body += part.get_payload()
        else:
            body = msg.get_payload()
    
        pop_conn.quit()
        return body
    
    def mail_gonder_xls():
        outlook = win32.Dispatch("Outlook.Application")
        wsh_shell = win32.Dispatch("WScript.Shell")
        str_desktop = wsh_shell.SpecialFolders("Desktop")
    
        selection = win32.Dispatch("Excel.Application").Selection
        selection.Copy()
    
        yeni = win32.Dispatch("Excel.Application").Workbooks.Add()
        yeni.Worksheets(1).Range("A1").PasteSpecial(-4163)  # xlPasteValues constant in Excel
    
        yeni.SaveAs(str_desktop + "\\Seçilen.xls")
    
        dam = outlook.CreateItem(0)
        dam.To = "onurturan@hotmail.com"
        dam.Subject = "D.Talepler"
        dam.Body = get_email_body("pop.example.com", "your_username", "your_password")
        dam.Attachments.Add(str_desktop + "\\Seçilen.xls")
        dam.Send()
        
        print("Mail gönderildi.")
    
    if __name__ == "__main__":
        mail_gonder_xls()