title: Perl - How to get the email address from the FROM part of header?
body: <p>I am trying to set up this script for my local bands newsletter.</p>
  <p>Currently, someone sends an email with a request to be added, we manually add it to newsletter mailer I set up.
  (Which works great thanks to help I found here!)</p>
  <p>The intent now is to have my script below log into the email account I set up for the list on our server, grab the info to add the email automatically.</p>
  <p>I know there are a bunch of apps that do this but, I want to learn myself.</p>
  <p>I already have the &quot;add to list&quot; working when there is an email address returned from the header(from) below BUT, sometimes the header(from) is a name and not the email address (<code>eg &quot;persons name&quot; is returned from persons name&lt;email@address&gt; but, not the &lt;email@address&gt;</code>.)</p>
  <p>Now, I am not set in stone on the below method but, it works famously... to a point.</p>
  <p>I read all the docs on these modules and there was nothing I could find to get the darn email in there all the time.</p>
  <p>Can someone help me here? Verbose examples are greatly appreciated since I am struggling learning Perl.</p>
  <pre><code> #!/usr/bin/perl -w
      ##########
      use CGI;
      use Net::IMAP::Simple;
      use Email::Simple;
      use IO::Socket::SSL; #optional i think if no ssl is needed
      use strict;
      use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
      ######################################################
      # fill in your details here
      my $username = '#########';
      my $password = '#############';
      my $mailhost = '##############';
      #######################################################
      print CGI::header();
      # Connect
      my $imap = Net::IMAP::Simple-&gt;new($mailhost, port=&gt; 143, use_ssl =&gt; 0, ) || die &quot;Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n&quot;;
      # Log in
       if ( !$imap-&gt;login( $username, $password ) ) {
        print STDERR &quot;Login failed: &quot; . $imap-&gt;errstr . &quot;\n&quot;;
        exit(64);
       }
      # Look in the INBOX
      my $nm = $imap-&gt;select('INBOX');
      # How many messages are there?
      my ($unseen, $recent, $num_messages) = $imap-&gt;status();
      print &quot;unseen: $unseen, &lt;br /&gt;recent: $recent, &lt;br /&gt;total: $num_messages&lt;br /&gt;\n\n&quot;;
      ## Iterate through unseen messages
       for ( my $i = 1 ; $i &lt;= $nm ; $i++ ) {
           if ( $imap-&gt;seen($i) ) {
           my $es = Email::Simple-&gt;new( join '', @{ $imap-&gt;top($i) } );
           printf( &quot;[%03d] %s\n\t%s\n&quot;, $i, $es-&gt;header('From'), $es-&gt;header('Subject'));
           print &quot;&lt;br /&gt;&quot;;
           next;
           }## in the long version these are pushed into different arrays for experimenting purposes
         else {
         my $es = Email::Simple-&gt;new( join '', @{ $imap-&gt;top($i) } );
         printf( &quot;[%03d] %s\n\t%s\n&quot;, $i, $es-&gt;header('From'), $es-&gt;header('Subject'));
         print &quot;&lt;br /&gt;&quot;;
         }
       }
      # Disconnect
      $imap-&gt;quit;
      exit;
  </code></pre>