title: Generating an email with an Excel XLSX attachment
body: <p>The following Perl program gives an access denied error at line 44 when I try to print either the entire string or the encoded portion. If I print just the header using <code>$msg-&gt;print_header(\*STDOUT)</code>.</p>
  
  <p>What I am trying to do is generate a text file that contains all the information that could be used in a telnet command to test a <em>Message Transfer Agent</em> (MTA) by sending an email with an attachment.</p>
  
  <pre><code>use MIME::Lite;
  use Net::SMTP;
  
  ### Add the sender, recipient and your SMTP mailhost
  my $from_address = 'temp999 at gmail.com';
  my $to_address   = 'test05@gmail.com';
  my $mail_host    = 'gmail.com';
  
  ### Adjust subject and body message
  my $subject      = 'Testing script';
  my $message_body = "I am sending an email with an attachment";
  
  ### Adjust the filenames
  my $my_file_xlsx   = 'c:/temp';
  my $your_file_xlsx = 'count.xlsx';
  
  ### Create the multipart container
  $msg = MIME::Lite-&gt;new(
      From    =&gt; $from_address,
      To      =&gt; $to_address,
      Subject =&gt; $subject,
      Type    =&gt; 'multipart/mixed'
  ) or die "Error creating multipart container: $!\n";
  
  ### Add the text for the message body
  $msg-&gt;attach(
      Type =&gt; 'TEXT',
      Data =&gt; $message_body
  ) or die "Error adding the text message part: $!\n";
  
  ### Adding an Excel file
  $msg-&gt;attach(
      Type        =&gt; 'application/octet-stream',
      Path        =&gt; $my_file_xlsx,
      Filename    =&gt; $your_file_xlsx,
      Disposition =&gt; 'attachment'
  ) or die "Error adding $file_xls: $!\n";
  
  ### Send the Message
  MIME::Lite-&gt;send('smtp', $mail_host, Timeout =&gt; 60);
  #$msg-&gt;send;
  $msg-&gt;print(\*STDOUT); # Write to a file handle ### LINE 44 ###
  #$msg-&gt;print_header(\*STDOUT); # Write the header
  #$msg-&gt;print_body(\*STDOUT); # Write the encoded body
  </code></pre>
  
  <p>I haven't found anything matching exactly what I am trying to do, but I might not be using the correct terminology when searching.</p>