Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Need a Mail::SendEasy example using attachments

by keithneargarder (Initiate)
on Aug 16, 2006 at 17:01 UTC ( [id://567733]=perlquestion: print w/replies, xml ) Need Help??

keithneargarder has asked for the wisdom of the Perl Monks concerning the following question:

Anyone have a code example of sending an email using SendEasy with multiple attachments specifying the MIME header tags like "Content-Type" etc.?
  • Comment on Need a Mail::SendEasy example using attachments

Replies are listed 'Best First'.
Re: Need a Mail::SendEasy example using attachments
by gellyfish (Monsignor) on Aug 16, 2006 at 17:46 UTC

    Mail::SendEasy doesn't appear to allow you specify any Content-Type other than application/octet-stream with a Base64 encoding. If you need more control over the construction of the MIME message I would recommend using either MIME::Lite or Email::MIME::Creator in association with Email::Send.

    /J\

Re: Need a Mail::SendEasy example using attachments
by roboticus (Chancellor) on Aug 16, 2006 at 19:27 UTC
    keithneargarder--

    I don't have one for Mail::SendEasy, but I have one I did a while ago using Mime::Lite and Net::SMTP. Here it is, if it'll help...

    #!/usr/bin/perl #------------------------------------------------------------------- # faxer.pl <recipient> <file> # # Send file <file> to <recipient>. Note that the recipient field is # a specially formatted field that contains recipient name, fax #, # and email address of the fax server, like so: # # "/name=Lonnie Phillips/fax=5551234/<rfax@npc.net>" # # 20050111 Roboticus - Added comments, some error checking # ???????? LPhillips - originally written #------------------------------------------------------------------- use strict; use MIME::Lite; MIME::Lite->send('smtp','mail.your.domain',Timeout=>60); use Net::SMTP; my $smtp = Net::SMTP->new('mail.your.domain', Debug=>1); print $smtp->domain,"\n"; my $usage= 'usage: faxer.pl <recipient> where: <recipient> = Fax address, formatted like: "/name=Roboticus/fax=5551234/<rfax@your.domain>" '; #my $fax_dest = @ARGV[0] or die $usage . "\nmissing <recipient>"; my $msg = MIME::Lite->new( To => '/name=%fromName%/fax=%toFax%/<rfax@your.domain>', From => 'MailBot@your.domain', Subject => 'Roboticus\'s test fax', Type => 'multipart/related', ) or die "Error creating MIME container!\n"; $msg->attach( Type => 'text/html', Data => qq {<html> <body> <img src="cid:ltrhead.gif"> </p> <div align="center"> <table border="1"> <tr align="center"><td colspan="2">TO</td><td colspan="2">FROM</td></ +tr> <tr><td align="right">Name:</td><td>%toName%</td><td align="right">Na +me:</td><t d>%fromName%</tr> <tr><td align="right">Fax:</td><td>%toFax%</td><td align="right">Fax: +</td><td>% fromFax%</td></tr> <tr><td></td><td></td><td align="right">Phone:</td><td>%fromPhone%</t +d></tr> <tr><td></td><td></td><td align="right">EMail:</td><td>%fromEMail%</t +d></tr> </table> </div> <hr> </p> To Whom It May Concern,</p> Regional Security is currently conducting a review on merchant %MerchID% "%MerchName%" for a batch of \$%MerchBatch%.</p> All funds are on hold until the review is complete. If you have any questions or information, please feel free to contact me at %fromPhone +% or email me at %fromEMail%.</p> Thank You,</p> %fromName% </body> </html> }, ) or die "Error creating HTML!\n"; $msg->attach( Type => 'image/gif', Id => 'ltrhead.gif', Path => 'fake_letterhead.gif', ) or die "Error adding image!\n"; open(OUTF,">faxer.mht") or die "Can't open output file"; print OUTF $msg->as_string; close(OUTF); $smtp->mail('mmason@npc.net'); ##$smtp->to('/name=Roboticus/fax=5551234/<rfax@your.domain>'); $smtp->to('roboticus@your.domain'); $smtp->data(); $smtp->datasend($msg->as_string); $smtp->dataend(); $smtp->quit();

    Note: I've hacked out a little proprietary stuff, and this is just an experimental (proof of concept) program I wrote some time ago. It functions, but will need some tweaking!

    --roboticus

      I'm not quite sure why you need to use Net::SMTP in your example, I've had luck with code like the following:
      use strict; use warnings; use MIME::Lite; my $msg = MIME::Lite->build( From => 'from@mail.address', To => 'recipient@mail.address', Subject => 'some subject', Type => 'multipart/mixed', ); $msg->attach( Type => 'TEXT', Data => "see attached file\n\n" ); $msg->attach( Type => 'AUTO', # change AUTO to your prefered type if +you wish Path => '/path/to/some/file', ); # MIME::Lite->send('smtp', 'some.smtp.host' ); # don't use sendmail, +see POD $msg->send();
        duckyd--

        I don't know that I needed it, but after hacking around with it last year, once I got it working, I stopped.

        Next time I have to do some EMail stuff, I'll use your suggestion and tune up the code a bit. Thanks!

        --roboticus

      RFC2111 specifies:
      Note: in Internet mail messages, the addr-spec in a Content-ID [MIME] or Message-ID [822] header are enclosed in angle brackets (<>).
      Whether this is neccesary depends on the mail client. Gmail requires the angle brackets, Outlook does not. To make this work in the above example this should be used instead:
      $msg->attach( Type => 'image/gif', Id => '<ltrhead.gif>', Path => 'fake_letterhead.gif', );
      The current version of MIME::Lite (3.0.1) does not add the angle brackets, but that could change in the future so be sure to check the documentation.
Re: Need a Mail::SendEasy example using attachments
by Jenda (Abbot) on Aug 16, 2006 at 22:13 UTC

    Just 'cause ... you know ... I'm the author ... and ... well ... I have to advertise my work since noone else seems to want to do that for me ... you may also try Mail::Sender. Examples included in the docs.

      I can vouch for it as I have used the mod for a couple report generators. The doc examples do the job!
      I forgot one bit of important information in my initial post - I need to use a client agent versus a server. I am not really sure how I tell the difference, but the admin person at my site told me this. I am trying to use Perl to send emails outside my company and he told me I had to use a client agent that would pass a username/password to the Exchange server to be authenticated, otherwise I get "Unable to Relay" error. I am using SendEasy because he suggested it when I told him I was using SendMail. Subsequently I was able to get SendEasy to work with a PDF attachment, but like another poster pointed out it uses "application/octet-stream" for "Content-Type" and I would prefer using a package where I could specify the MIME tags. Could you tell me if Mail::Sender fits what I want? Thanks!!!

        It depends on the authentication protocols supported by your exchange server. Mail::Sender does support authentication, but not all protocols. The ones currently supported by Mail::Sender are LOGIN, PLAIN, CRAM-MD5 and NTLM. If you already have Mail::Sender installed you can find what protocols does the server support by this:

        c:\> perl -MMail::Sender -e "print join ', ', Mail::Sender->QueryAuthP +rotocols('the.server.name')"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567733]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-20 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found