Posts tagged ‘SMTP’

Quick-and-dirty SMTP Server For Debugging

2013-12-27 11:51

Every computer program expands until it can read e-mail – or so they say. But many applications need not to read, but to send e-mails; web apps or web services are probably the most prominent examples. If you happen to develop them, you may sometimes want a local, dummy SMTP server just for testing this functionality. It doesn’t even have to send anything (it must not, actually), but it should allow you to see what would be sent if the app worked in a production environment.

By far the easiest way to setup such a server involves, quite surprisingly, Python. There is a standard library module called smtpd, which is built exactly for this purpose. Amusingly, you don’t even have to write any code that uses it; you can invoke it straight from the command line:

  1. $ python -m smtpd -n -c DebuggingServer

This will start a server that listens on port 8025 and dumps every message “sent” through it to the standard output. A custom port is chosen because on *nix systems, only the ports above 1024 are accessible to an ordinary user. For the standard SMTP port 25, you need to start the server as root:

  1. $ sudo python -m stmpd -c DebuggingServer localhost:25

While it’s more typing, it frees you from having to change the SMTP port number inside your application’s code.

If you plan to use smtpd more extensively, though, you may want to look at the small runner script I’ve prepared. By default, it tries to listen on port 25, but you can supply a port number as its sole argument.

Tags: , , ,
Author: Xion, posted under Applications, Internet, Programming » Comments Off on Quick-and-dirty SMTP Server For Debugging

Triki z PowerShellem #6 – Mail z załącznikami

2008-07-03 20:05

Kiedy mamy komuś przesłać plik, możemy niekiedy użyć do tego zwykłej poczty e-mail. Nie jest to oczywiście możliwe zawsze: plik nie powinien być zbyt duży i zwykle nie może też należeć do żadnego z “niebezpiecznych” typów (np. aplikacji EXE), by serwery pocztowe mogły go przepchnąć bez narażania lub zatykania sieci.
Jeśli w naszym przypadku tak jest, to możemy wykonać całą operację na przykład przy pomocy poniższego skryptu PowerShella:

  1. # MailFile.ps1 - Wysyła podany plik e-mailem na podany adres
  2. # Parametr: nazwa pliku lokalnego
  3. param([string]$file = $(throw "File not specified"))
  4.  
  5. # Stałe
  6. $SERVER = "moj.server.pl"
  7. $LOGIN = "loginSmtp"
  8. $PASSWORD = "hasłoSmtp"
  9. $FROM_ADDRESS = "ja@server.pl"
  10.  
  11. # Odczytanie adresu docelowego
  12. $recipentAddress = Read-Host -Prompt "Recipent e-mail address"
  13. $recipent = New-Object Net.Mail.MailAddress @($recipentAddress)
  14.  
  15. # Złożenie maila
  16. $mail = New-Object Net.Mail.MailMessage
  17. $mail.From = New-Object Net.Mail.MailAddress @($FROM_ADDRESS)
  18. $mail.To.Add($recipent)
  19. $mail.Subject = (New-Object IO.FileInfo @($file)).Name
  20.  
  21. # Dodanie załącznika
  22. $attachment = New-Object Net.Mail.Attachment @($file,
  23.     [Net.Mime.MediaTypeNames+Application]::Octet)
  24. $cd = $attachment.ContentDisposition
  25. $cd.CreationDate = [IO.File]::GetCreationTime($file)
  26. $cd.ModificationDate = [IO.File]::GetLastWriteTime($file)
  27. $cd.ReadDate = [IO.File]::GetLastAccessTime($file)
  28. $mail.Attachments.Add($attachment)
  29.  
  30. # Łączenie z serwerem SMTP i wysłanie maila
  31. $smtp = New-Object Net.Mail.SmtpClient @($SERVER)
  32. $smtp.Credentials = New-Object Net.NetworkCredential @($LOGIN, $PASSWORD)
  33. # $smtp.EnableSsl = $true # Odkomentowujemy, jeśli serwer wymaga SSL
  34. $smtp.Send($mail)

Jak widać, nie jest to nic skomplikowanego. A jak wykorzystać go w praktyce?… Podobnie jak prezentowany wcześniej skrypt do uploadu na serwer FTP, można go wywołać komendą w rodzaju:

  1. powershell -Command . 'C:\Sciezka\Do\Skryptu\MailFile.ps1' '%1'

którą podpinamy do menu kontekstowego plików lub podmenu Wyślij do.
W ten prosty sposób możemy oszczędzić sobie uruchamiania całego klienta poczty, względnie przeglądarki z webmailem. Naturalnie sporo rzeczy – jak chociażby temat wysyłanej wiadomości czy jej treść – możemy w zaprezentowanym kawałku kodu zmienić lub uzupełnić.

Tags: , ,
Author: Xion, posted under Applications, Internet » 1 comment
 


© 2023 Karol Kuczmarski "Xion". Layout by Urszulka. Powered by WordPress with QuickLaTeX.com.