Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Send mail help

by paramjit (Novice)
on Jun 29, 2011 at 11:36 UTC ( #911937=perlquestion: print w/ replies, xml ) Need Help??
paramjit has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to send a mail using sendmail how can i specify charset in header?

open(MAIL, "|/usr/sbin/sendmail -t"); ## Mail Header print MAIL "To: $from\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n\n"; ## Mail Body print MAIL "This is a test mail"; close(MAIL);
Thanks

Comment on Send mail help
Download Code
Re: Send mail help
by Neighbour (Friar) on Jun 29, 2011 at 11:56 UTC
    Which bit of your question involves Perl?
    If you want help with your sendmail application (which, according to the manpage I'm getting is just a frontend for another mailprogram), this is not the best place to ask :).

    However, for a more Perl-oriented solution, try toying with MIME::Lite.
Re: Send mail help
by Anonymous Monk on Jun 29, 2011 at 12:04 UTC
Re: Send mail help
by flexvault (Parson) on Jun 29, 2011 at 17:30 UTC

    ...try toying with MIME::Lite.

    While the above may be true for the casual email admin, there are times that you need the functionally of working with sendmail directly. The standard config file may be set up to discourage spammers. Many email providers have multiple configurations and multiple queues for email activity. (Note: How about a different config file for timing considerations for sending a 'text' message to a cell phone). So while the above code is nice, it doesn't account for the interaction required to talk to sendmail directly. I prefer the following for directly calling sendmail from Unix/Linux:

    my $CR = "\r\n"; ## This should be what your sendmail accepts. ## sendmail will use the correct $CR for the rec +ipient(s) open ( MAIL, ">", $out_email )|| die " Not open $out_email $!\n"; ## Mail Header print MAIL "To: $to$CR"; print MAIL "From: $from$CR"; print MAIL "Subject: $subject$CR"; print MAIL "MIME-Version: 1.0$CR"; print MAIL "Content-Type: text/html; charset=utf-8;$CR"; print MAIL "$CR"; ## "end of headers" ## Mail Body print MAIL "<html><body><br />$CR"; print MAIL "This is a test mail$CR"; print MAIL "<br /></body></html>$CR"; print MAIL "$CR"; ## "end of body" close(MAIL); my $conf = "-C /etc/mail/alt_sendmail.cf"; ## Optional if the stand +ard is okay! my $smail = "/usr/sbin/sendmail $conf -bm -v -t < $out_mail 1>>/dev/nu +ll 2>>/dev/null /&"; system($smail);

    I have had to add the "-v" and "1>>/dev/null 2>>/dev/null" for some Linux distributions, so you may not need them, but they don't hurt either. Sendmail is very capable of having hundreds of email sessions running at the same time, so I add the "/&" so that I get control back and can continue processing in perl. Another good reason to do it this way is you can print the $smail and run the command from the command line. If it doesn't work, remove the "1>>/dev/null 2>>/dev/null" and you can see the conversation with sendmail (and any errors).

    For future readers of this post, that may need help working with email and perl, an excellent O'Reilly book is "Programming Internet Email" by David Wood. His examples are in perl and the information is a great basic explanation of internet email. I do not have any affiliation with the author or the publisher but when I was looking for a book on programming email, I was discouraged by the horrible reviews for this book. One review stated the examples were written in Visual Basic, so I bought a used book. When I received the book, I was pleasantly surprised to see the extensive use of perl throughout the book.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

Re: Send mail help
by dwm042 (Priest) on Jun 29, 2011 at 22:14 UTC
    Just an FYI about Mime::Lite and other means of sending HTML emails.

    Well configured HTML emails also have a pure text version as well. In part, this is because certain mail clients can't handle HTML emails. To note, one signature of HTML mail from viruses is that they only include the HTML version since their mails are intended to infect others (via urls bringing in nastiness). Therefore, HTML emails without a pure text equivalent have much higher spam scores.

    I'm in the process of replacing hard coded text emails (coded much in the manner of the OP) with a Mime:Lite and Template based solution, that allows my internal customers to write their own emails to our external clients. And I'm asking my customers to write both a HTML version and a non-HTML version to place in an email.

    A code snippet would be:

    if ( $header->[2] =~ /html/i ) { $tt->process( $file_html, $tt_vars, \$html_out ) || warn $tt->error(), "\n"; $vars{Type} = 'multipart/alternative'; $msg = MIME::Lite->new( %vars ); # Add parts (each "attach" has same arguments as "new"): $msg->attach( Type => 'text/plain', Data => $text_out, ); $msg->attach( Type => 'text/html', Data => $html_out, ); } $msg->send;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://911937]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-07-13 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (250 votes), past polls