Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 690141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:24:19+00:00 2026-05-14T02:24:19+00:00

I need suggestions on how can I download attachments from my IMAP mails which

  • 0

I need suggestions on how can I download attachments from my IMAP mails which have attachments and current date in subject line i.e. YYYYMMDD format and save the attachments to a local path.

I went through the Perl module Mail::IMAPClient and am able to connect to the IMAP mail server, but need help on other tasks. One more thing to note is that my IMAP sever requires SSL auth.

Also the attachments could be gz, tar or tar.gz files.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:24:19+00:00Added an answer on May 14, 2026 at 2:24 am

    A simple program that does what you want is below.

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    

    The minimum version for Email::MIME is for when walk_parts was introduced.

    use Email::MIME 1.901;
    use IO::Socket::SSL;
    use Mail::IMAPClient;
    use POSIX qw/ strftime /;
    use Term::ReadKey;
    

    You don’t want to hardcode your password in your program, do you?

    sub read_password {
      local $| = 1;
      print "Enter password: ";
    
      ReadMode "noecho";
      my $password = <STDIN>;
      ReadMode "restore";
    
      die "$0: unexpected end of input"
        unless defined $password;
    
      print "\n";
      chomp $password; 
      $password;
    }
    

    Connect using SSL. We ought to be able to be able to do this with a simple Ssl parameter to the constructor, but some vendors have chosen to break it in their packages.

    my $pw = read_password;
    my $imap = Mail::IMAPClient->new(
     #Debug    => 1,
      User     => "you\@domain.com",
      Password => $pw,
      Uid      => 1,
      Peek     => 1,  # don't set \Seen flag
      Socket   => IO::Socket::SSL->new(
                    Proto    => 'tcp',
                    PeerAddr => 'imap.domain.com',
                    PeerPort => 993,
                  ),
    );
    
    die "$0: connect: $@" if defined $@;
    

    If you want a folder other than the inbox, change it.

    $imap->select("INBOX")
      or die "$0: select INBOX: ", $imap->LastError, "\n";
    

    Using IMAP search, we look for all messages whose subjects contain today’s date in YYYYMMDD format. The date can be anywhere in the subject, so, for example, a subject of “foo bar baz 20100316” would match today.

    my $today = strftime "%Y%m%d", localtime $^T;
    my @messages = $imap->search(SUBJECT => $today);
    die "$0: search: $@" if defined $@;
    

    For each such message, write its attachments to files in the current directory. We write the outermost layer of attachments and do not dig for nested attachments. A part with a name parameter in its content type (as in image/jpeg; name="foo.jpg") is assumed to be an attachment, and we ignore all other parts. A saved attachment’s name is the following components separated by -: today’s date, its IMAP message ID, a one-based index of its position in the message, and its name.

    foreach my $id (@messages) {
      die "$0: funky ID ($id)" unless $id =~ /\A\d+\z/;
    
      my $str = $imap->message_string($id)
        or die "$0: message_string: $@";
    
      my $n = 1;
      Email::MIME->new($str)->walk_parts(sub {
        my($part) = @_;
        return unless ($part->content_type =~ /\bname=([^"]+)/ 
                    or $part->content_type =~ /\bname="([^"]+)"/); # " grr...
    
        my $name = "./$today-$id-" . $n++ . "-$1";
        print "$0: writing $name...\n";
        open my $fh, ">", $name
          or die "$0: open $name: $!";
        print $fh $part->content_type =~ m!^text/!
                    ? $part->body_str
                    : $part->body
          or die "$0: print $name: $!";
        close $fh
          or warn "$0: close $name: $!";
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a java application which can merge docx files. Any suggestions?
I need suggestion about YAMI library . I have a system which receives Json
I need a little information/help/suggestions how can I build my application so I can
Need suggestions on implementing associating single or many objects to an entity. All soccer
I need some asp.net pagination code samples. I would like suggestions on open source
I'm just looking for suggestions on the best way to do this... I need
I need a excel-like grid control in MFC, do anyone have good suggestion to
I am trying to copy file(s) using webrequest inside WCF. I need some suggestions.
I have been investigating how My ASP.NET MVC application can log unsuccessful / incomplete
I have a unique and interesting difficulty: I need to find a way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.