📄 perlfaq9.1
字号:
.PPWithout sending mail to the address and seeing whether there's a humanon the other end to answer you, you cannot fully answer part \fIb\fR, buteither the \f(CW\*(C`Email::Valid\*(C'\fR or the \f(CW\*(C`RFC::RFC822::Address\*(C'\fR module will doboth part \fIa\fR and part \fIb\fR as far as you can in real-time..PPIf you want to just check part \fIa\fR to see that the address is validaccording to the mail header standard with a simple regular expression,you can have problems, because there are deliverable addresses thataren't \s-1RFC\-2822\s0 (the latest mail header standard) compliant, andaddresses that aren't deliverable which, are compliant. However, thefollowing will match valid \s-1RFC\-2822\s0 addresses that do not have comments,folding whitespace, or any other obsolete or non-essential elements.This \fIjust\fR matches the address itself:.PP.Vb 7\& my $atom = qr{[a\-zA\-Z0\-9_!#\e$\e%&\*(Aq*+/=?\e^\`{}~|\e\-]+};\& my $dot_atom = qr{$atom(?:\e.$atom)*};\& my $quoted = qr{"(?:\e\e[^\er\en]|[^\e\e"])*"};\& my $local = qr{(?:$dot_atom|$quoted)};\& my $domain_lit = qr{\e[(?:\e\e\eS|[\ex21\-\ex5a\ex5e\-\ex7e])*\e]};\& my $domain = qr{(?:$dot_atom|$domain_lit)};\& my $addr_spec = qr{$local\e@$domain};.Ve.PPJust match an address against \f(CW\*(C`/^${addr_spec}$/\*(C'\fR to see if it followsthe \s-1RFC2822\s0 specification. However, because it is impossible to besure that such a correctly formed address is actually the correct wayto reach a particular person or even has a mailbox associated with it,you must be very careful about how you use this..PPOur best advice for verifying a person's mail address is to have thementer their address twice, just as you normally do to change apassword. This usually weeds out typos. If both versions match, sendmail to that address with a personal message. If you get the messageback and they've followed your directions, you can be reasonablyassured that it's real..PPA related strategy that's less open to forgery is to give them a \s-1PIN\s0(personal \s-1ID\s0 number). Record the address and \s-1PIN\s0 (best that it be arandom one) for later processing. In the mail you send, ask them toinclude the \s-1PIN\s0 in their reply. But if it bounces, or the message isincluded via a \*(L"vacation\*(R" script, it'll be there anyway. So it'sbest to ask them to mail back a slight alteration of the \s-1PIN\s0, such aswith the characters reversed, one added or subtracted to each digit, etc..Sh "How do I decode a \s-1MIME/BASE64\s0 string?".IX Subsection "How do I decode a MIME/BASE64 string?"The MIME\-Base64 package (available from \s-1CPAN\s0) handles this as well asthe \s-1MIME/QP\s0 encoding. Decoding \s-1BASE64\s0 becomes as simple as:.PP.Vb 2\& use MIME::Base64;\& $decoded = decode_base64($encoded);.Ve.PPThe MIME-Tools package (available from \s-1CPAN\s0) supports extraction withdecoding of \s-1BASE64\s0 encoded attachments and content directly from emailmessages..PPIf the string to decode is short (less than 84 bytes long)a more direct approach is to use the \fIunpack()\fR function's \*(L"u\*(R"format after minor transliterations:.PP.Vb 4\& tr#A\-Za\-z0\-9+/##cd; # remove non\-base64 chars\& tr#A\-Za\-z0\-9+/# \-_#; # convert to uuencoded format\& $len = pack("c", 32 + 0.75*length); # compute length byte\& print unpack("u", $len . $_); # uudecode and print.Ve.Sh "How do I return the user's mail address?".IX Subsection "How do I return the user's mail address?"On systems that support getpwuid, the $< variable, and theSys::Hostname module (which is part of the standard perl distribution),you can probably try using something like this:.PP.Vb 2\& use Sys::Hostname;\& $address = sprintf(\*(Aq%s@%s\*(Aq, scalar getpwuid($<), hostname);.Ve.PPCompany policies on mail address can mean that this generates addressesthat the company's mail system will not accept, so you should ask forusers' mail addresses when this matters. Furthermore, not all systemson which Perl runs are so forthcoming with this information as is Unix..PPThe Mail::Util module from \s-1CPAN\s0 (part of the MailTools package) provides a\&\fImailaddress()\fR function that tries to guess the mail address of the user.It makes a more intelligent guess than the code above, using informationgiven when the module was installed, but it could still be incorrect.Again, the best way is often just to ask the user..Sh "How do I send mail?".IX Subsection "How do I send mail?"Use the \f(CW\*(C`sendmail\*(C'\fR program directly:.PP.Vb 6\& open(SENDMAIL, "|/usr/lib/sendmail \-oi \-t \-odq")\& or die "Can\*(Aqt fork for sendmail: $!\en";\& print SENDMAIL <<"EOF";\& From: User Originating Mail <me\e@host>\& To: Final Destination <you\e@otherhost>\& Subject: A relevant subject line\&\& Body of the message goes here after the blank line\& in as many lines as you like.\& EOF\& close(SENDMAIL) or warn "sendmail didn\*(Aqt close nicely";.Ve.PPThe \fB\-oi\fR option prevents sendmail from interpreting a line consistingof a single dot as \*(L"end of message\*(R". The \fB\-t\fR option says to use theheaders to decide who to send the message to, and \fB\-odq\fR says to putthe message into the queue. This last option means your message won'tbe immediately delivered, so leave it out if you want immediatedelivery..PPAlternate, less convenient approaches include calling mail (sometimescalled mailx) directly or simply opening up port 25 have having anintimate conversation between just you and the remote \s-1SMTP\s0 daemon,probably sendmail..PPOr you might be able use the \s-1CPAN\s0 module Mail::Mailer:.PP.Vb 1\& use Mail::Mailer;\&\& $mailer = Mail::Mailer\->new();\& $mailer\->open({ From => $from_address,\& To => $to_address,\& Subject => $subject,\& })\& or die "Can\*(Aqt open: $!\en";\& print $mailer $body;\& $mailer\->close();.Ve.PPThe Mail::Internet module uses Net::SMTP which is less Unix-centric thanMail::Mailer, but less reliable. Avoid raw \s-1SMTP\s0 commands. Thereare many reasons to use a mail transport agent like sendmail. Theseinclude queuing, \s-1MX\s0 records, and security..Sh "How do I use \s-1MIME\s0 to make an attachment to a mail message?".IX Subsection "How do I use MIME to make an attachment to a mail message?"This answer is extracted directly from the MIME::Lite documentation.Create a multipart message (i.e., one with attachments)..PP.Vb 1\& use MIME::Lite;\&\& ### Create a new multipart message:\& $msg = MIME::Lite\->new(\& From =>\*(Aqme@myhost.com\*(Aq,\& To =>\*(Aqyou@yourhost.com\*(Aq,\& Cc =>\*(Aqsome@other.com, some@more.com\*(Aq,\& Subject =>\*(AqA message with 2 parts...\*(Aq,\& Type =>\*(Aqmultipart/mixed\*(Aq\& );\&\& ### Add parts (each "attach" has same arguments as "new"):\& $msg\->attach(Type =>\*(AqTEXT\*(Aq,\& Data =>"Here\*(Aqs the GIF file you wanted"\& );\& $msg\->attach(Type =>\*(Aqimage/gif\*(Aq,\& Path =>\*(Aqaaa000123.gif\*(Aq,\& Filename =>\*(Aqlogo.gif\*(Aq\& );\&\& $text = $msg\->as_string;.Ve.PPMIME::Lite also includes a method for sending these things..PP.Vb 1\& $msg\->send;.Ve.PPThis defaults to using sendmail but can be customized to use\&\s-1SMTP\s0 via Net::SMTP..Sh "How do I read mail?".IX Subsection "How do I read mail?"While you could use the Mail::Folder module from \s-1CPAN\s0 (part of theMailFolder package) or the Mail::Internet module from \s-1CPAN\s0 (partof the MailTools package), often a module is overkill. Here's amail sorter..PP.Vb 1\& #!/usr/bin/perl\&\& my(@msgs, @sub);\& my $msgno = \-1;\& $/ = \*(Aq\*(Aq; # paragraph reads\& while (<>) {\& if (/^From /m) {\& /^Subject:\es*(?:Re:\es*)*(.*)/mi;\& $sub[++$msgno] = lc($1) || \*(Aq\*(Aq;\& }\& $msgs[$msgno] .= $_;\& }\& for my $i (sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msgs)) {\& print $msgs[$i];\& }.Ve.PPOr more succinctly,.PP.Vb 6\& #!/usr/bin/perl \-n00\& # bysub2 \- awkish sort\-by\-subject\& BEGIN { $msgno = \-1 }\& $sub[++$msgno] = (/^Subject:\es*(?:Re:\es*)*(.*)/mi)[0] if /^From/m;\& $msg[$msgno] .= $_;\& END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] }.Ve.Sh "How do I find out my hostname, domainname, or \s-1IP\s0 address?".IX Xref "hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa,gethostbyname, Socket, Net::Domain, Sys::Hostname".IX Subsection "How do I find out my hostname, domainname, or IP address?"(contributed by brian d foy).PPThe Net::Domain module, which is part of the standard distribution startingin perl5.7.3, can get you the fully qualified domain name (\s-1FQDN\s0), the hostname, or the domain name..PP.Vb 1\& use Net::Domain qw(hostname hostfqdn hostdomain);\&\& my $host = hostfqdn();.Ve.PPThe \f(CW\*(C`Sys::Hostname\*(C'\fR module, included in the standard distribution sinceperl5.6, can also get the hostname..PP.Vb 1\& use Sys::Hostname;\&\& $host = hostname();.Ve.PPTo get the \s-1IP\s0 address, you can use the \f(CW\*(C`gethostbyname\*(C'\fR built-in functionto turn the name into a number. To turn that number into the dotted octetform (a.b.c.d) that most people expect, use the \f(CW\*(C`inet_ntoa\*(C'\fR functionfrom the <Socket> module, which also comes with perl..PP.Vb 1\& use Socket;\&\& my $address = inet_ntoa(\& scalar gethostbyname( $host || \*(Aqlocalhost\*(Aq )\& );.Ve.Sh "How do I fetch a news article or the active newsgroups?".IX Subsection "How do I fetch a news article or the active newsgroups?"Use the Net::NNTP or News::NNTPClient modules, both available from \s-1CPAN\s0.This can make tasks like fetching the newsgroup list as simple as.PP.Vb 2\& perl \-MNews::NNTPClient\& \-e \*(Aqprint News::NNTPClient\->new\->list("newsgroups")\*(Aq.Ve.Sh "How do I fetch/put an \s-1FTP\s0 file?".IX Subsection "How do I fetch/put an FTP file?"LWP::Simple (available from \s-1CPAN\s0) can fetch but not put. Net::FTP (alsoavailable from \s-1CPAN\s0) is more complex but can put as well as fetch..Sh "How can I do \s-1RPC\s0 in Perl?".IX Subsection "How can I do RPC in Perl?"(Contributed by brian d foy).PPUse one of the \s-1RPC\s0 modules you can find on \s-1CPAN\s0 (http://search.cpan.org/search?query=RPC&mode=all )..SH "REVISION".IX Header "REVISION"Revision: \f(CW$Revision:\fR 8539 $.PPDate: \f(CW$Date:\fR 2007\-01\-11 00:07:14 +0100 (Thu, 11 Jan 2007) $.PPSee perlfaq for source control details and availability..SH "AUTHOR AND COPYRIGHT".IX Header "AUTHOR AND COPYRIGHT"Copyright (c) 1997\-2007 Tom Christiansen, Nathan Torkington, andother authors as noted. All rights reserved..PPThis documentation is free; you can redistribute it and/or modify itunder the same terms as Perl itself..PPIrrespective of its distribution, all code examples in this fileare hereby placed into the public domain. You are permitted andencouraged to use this code in your own programs for funor for profit as you see fit. A simple comment in the code givingcredit would be courteous but is not required.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -