Guest User

Untitled

a guest
Feb 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use Mail::IMAPClient;
  4. use IO::Socket;
  5. use IO::Socket::SSL;
  6. use Time::ParseDate;
  7. use Data::Dumper;
  8.  
  9. # Config stuff
  10. my $mail_hostname = '';
  11. my $mail_username = '';
  12. my $mail_password = '';
  13. my $mail_ssl = 1;
  14.  
  15. # Make sure this is accessable for this namespace
  16. my $socket = undef;
  17.  
  18. if( $mail_ssl ) {
  19. # Open up a SSL socket to use with IMAPClient later
  20. $socket = IO::Socket::SSL->new(
  21. PeerAddr => $mail_hostname,
  22. PeerPort => 993,
  23. Timeout => 5,
  24. );
  25. } else {
  26. # Open up a none SSL socket to use with IMAPClient later
  27. $socket = IO::Socket::INET->new(
  28. PeerAddr => $mail_hostname,
  29. PeerPort => 143,
  30. Timeout => 5,
  31. );
  32. }
  33.  
  34. # Check we connected
  35. if( ! defined( $socket ) ) {
  36. print STDERR "Could not open socket to mailserver: $@\n";
  37. exit 1;
  38. }
  39.  
  40. my $client = Mail::IMAPClient->new(
  41. Socket => $socket,
  42. User => $mail_username,
  43. Password => $mail_password,
  44. Timeout => 5,
  45. );
  46.  
  47. # Check we have an imap client
  48. if( ! defined( $client ) ) {
  49. print STDERR "Could not initialize the imap client: $@\n";
  50. exit 1;
  51. }
  52.  
  53. # Check we are authenticated
  54. if( $client->IsAuthenticated() ) {
  55. # Select the INBOX folder
  56. if( ! $client->select("INBOX") ) {
  57. print STDERR "Could not select the INBOX: $@\n";
  58. } else {
  59. if( $client->message_count("INBOX") > 0) {
  60. print "Processing " . $client->message_count("INBOX") . " messages....\n";
  61.  
  62. # We delete messages after processing so get all in the inbox
  63. for( $client->search('ALL') ) {
  64. print " ..." . $_ . "\n";
  65.  
  66. # Pull the RFC822 date out the message
  67. my $date = $client->date( $_ );
  68.  
  69. # Pull the subject out the message
  70. my $subject = $client->subject( $_ );
  71.  
  72. # Pull the body out the message
  73. my $body = $client->body_string( $_ );
  74.  
  75. # Try and get the unix time of the message being sent
  76. my $unix_date = undef;
  77. if( $date ) {
  78. $unix_date = parsedate( $date );
  79. }
  80.  
  81. # Log the recieved time
  82. my $recv_unix = time;
  83.  
  84. # Check we have valid stuff
  85. if( ! $unix_date || ! $subject || ! $body ) {
  86. print "Missing header stuff....\n";
  87. } else {
  88. print Dumper( $unix_date );
  89. print Dumper( $subject );
  90. print Dumper( $body );
  91. }
  92.  
  93. # Remove the message
  94. $client->delete_message($_);
  95. }
  96.  
  97. # Delete the messages we have deleted
  98. # Yes, you read that right, IMAP is strangly awesome
  99. $client->expunge("INBOX");
  100. } else {
  101. # No messages
  102. print "No messages to process\n";
  103. }
  104.  
  105. # Close the inbox
  106. $client->close("INBOX");
  107. }
  108. } else {
  109. print STDERR "Could not authenticate against IMAP: $@\n";
  110. exit 1;
  111. }
  112.  
  113. # Tidy up after we are done
  114. $client->done();
  115. exit 0;
Add Comment
Please, Sign In to add comment