Send MIME mail in Perl using Sendmail
Author: oulix-mail: oulix@263.net
For the first time, please refer to the wrong place.
Comrades engage in web development may encounter the functionality that needs to be embedded in the page. If it is a normal plain text, it is ok, no problem, there are many components with ASP, and there is also a good tool with CGI, such as Perl. This purpose can be implemented using Sendmail under the Unix platform in Perl. The typical example of sending a plain text message in Perl is as follows:
#! / usr / lib / perluse stric;
My ($ r_mail) = 'Recipients@aaa.net'; My ($ s_mail) = 'sender@bb.com'; my ($ subject) = 'Subject';
Open (Mail, '| / USR / lib / sendmail -t'); select (mail);
Print << "end_tag"; to: $ r_mailfrom: $ s_mailsubject: $ SUBJECT
content of email
END_TAG
A few things to pay attention to, in the send mail to, from and recipient mail address variable $ r_mail and sender mail $ s_mail to leave a space, avoid unnecessary error issues (I have encountered, I don't know you have No encounter). There is also the end mark end_tag if it is the last line of the file, it is best to add one or two spaces behind, I have encountered the situation of the end_tag who didn't find the end of the empty PERL. Also, don't forget that the space behind the subject is necessary, it separates the message header and email content.
Ok, enter the topic, if we need to send an email in the HTML format? If you write this
#! / usr / lib / perluse stric;
My ($ r_mail) = 'Recipients@aaa.net'; My ($ s_mail) = 'sender@bb.com'; my ($ subject) = 'Subject';
Open (Mail, '| / USR / lib / sendmail -t'); select (mail);
Print << "end_tag"; to: $ r_mailfrom: $ s_mailsubject: $ SUBJECT
Mail content body> html>
END_TAG
Try it, in the 263m source code, in Hotmail, if the message is a complete HTML message, it is basically able to complete the HTML page. In fact, there is a MIME type of MIME. Detailed MIME information, everyone is looking for it, otherwise it will be too far, I am not falling enough. If this HTML message does not connect to any image and the external content of this class, that is a good office, adding a content-type: text / html in the mail header: Text / HTML. If you need a Chinese, you need to specify the code page, directly add charSet = "GB2312", and the middle semicolon is opened. The full code is as follows:
#! / usr / lib / perluse stric;
MY ($ r_mail) = 'Recipients@aaa.net'; My ($ s_mail) = 'sender@bbb.com'; My ($ SUBJECT) = 'Subject'; Open (Mail, '| / USR / lib / sendmail -t '); SELECT (MAIL);
Print << "end_tag"; to: $ r_mailfrom: $ s_mailsubject: $ SUBJECTCONTENT-TYPE: TEXT / HTML; Charset = "GB2312"
Mail content body> html>
END_TAG
This generally used the user can see an email in the HTML format. If the problem is more complicated, there is a picture in this HTML page, and Flash, what should I do? There is a way: put these pictures online, the pictures of the page write all path links, so you don't need to bring these cumbersome on the email, and reduce the size of the email, two! I agree seriously. But there is always a way to do this, so continue. The HTML page does not have these pictures and flash files in the messages received by the user. The HTML page seen is a page that opens the skylight. Take a look at the MIME type, there is a multipart / mixed type to complete our ultimate goal, let the user receive the full page of the graphic. First, you need to coding files such as image or flash in a certain encoding method. The most commonly used Base64 encoding is available, and there is quoted-printable encoding. Find a tool, use the base64 encoding for files such as HMTL messages, etc., using quoted-printable encoding. Then, in the mail header content-type: multipart / mix; bitchary = "---- = _ nextpart_000_0008_01c2bcb0.9cf9ae70" Name = "Thanks.gif" This Multipart / Mixed indicates that this email is a mixed type. The next Boundary is what the marks of each part of each part is specified in the content. Here is ---- = _ nextpart_000_0008_01c2bcb0.9cf9ae70. How do I do this value is not very clear, my understanding is that it is possible not to be the same as any code after any encoding. The Name behind will not be. To make a comparative rocko, or first look at the code. Below is a complete example of sending HMTL mail.
#! / usr / lib / perluse stric;
My ($ r_mail) = 'Recipients@aaa.net'; My ($ s_mail) = 'sender@bb.com'; my ($ subject) = 'Subject';
Open (Mail, '| / USR / lib / sendmail -t'); select (mail);
Print << "END_TAG"; To: $ r_mailfrom: $ s_mailsubject: $ SUBJECTCONTENT-TYPE: MULTIPART / MIXED; Boundary = "---- = _ nextpart_000_0008_01c2bcb0.9cf9ae70"
This is a multi-part message in mime format.
------ = _ nextpart_000_0008_01c2bcb0.9cf9ae70content-type: text / html; charset = "GB2312" Content-Transfer-Encoding: quoted-printable
------ = _ NextPart_000_0008_01C2BCB0.9CF9AE70Content-Type: image / gifContent-Transfer-Encoding: base64Content-Disposition: attachment; filename = "thanks.gif" Content-Location: file: /// C: / DEV / perl / Images / popup_thanks.gif
R0lGODlhkAGQAfcAAPCyTdvr0cvbCJ8tTt3nAEeRGarPkRSmULrVqrRtLtbeB7CRNdTZ0LGtNaXJEaLR7ejz23e1GFir2NNMayaR0VWaLfjMRtnINsfWuYi6aGWsNevKN5PHdLZKMyZLC6smNs3nu8mINWetGBIqCOqXNdutkWyRRzZvEqvIK9fJSZGMNrSxSlalGO / 36rS5revt6MySScswSperNZWtT9C3
------ = _ nextpart_000_0008_01c2bcb0.9cf9ae70 -
END_TAG
It's a bit long, slowly explaining it. There is only one picture popup_thanks.gif in this sea-saved. "This is a multi-part message in mime format.", This is a description information before the first Boundary appears, does not use it. Then the first Boundary: ------ = _ nextpart_000_0008_01c2bcb0.9cf9ae70, which tells the user that there is some content here. Note that it is --Boundary, that is, in front of Boundary -, please pay attention to see the last Boundary, it has doubled before and after, indicating the end of the email. Content-type: text / html; charset = "GB2312" Description This section is the document type of this section is HTML format, Content-Transfer-Encoding: quoted-printable Description This section is encoded using the quoted-printable method, of course, below The content must be QUOTED-Printable encoding, otherwise the user can not see the correct content. There is no good email content, then the next Boundary, the thing here is the popup_thanks.gif we want. Look at the MIME type is: Content-Type: Image / GIF picture Generally use Base64 encoding, so this is Content-Transfer-Encoding: base64 and then look at it is a line content-disposition: attachment; filename = "thanks.gif" here Attahment Refiguring this picture as an attachment, it can also be inline, which is like this image will display directly in the mail display area of the recipient's mail program. FileName specifies what file name displayed in the attachment area, here will change popup_thanks.gif to Thanks.gif. There is also a content-location: file: file: /// c: / dev / perl / images / popup_thanks. GIF specifies the original path of the file. It seems useless? Actually, what is the SRC in the label of this image in the HTML file? Between these two, there is a picture in the attachment, but the email is still open. Ok, basically this is the case. No, there is a problem, when you do a program, how can I get the required code after the program? How do I do in Perl? I don't know if there may be such packages in the cpan. The name is familiar with the encoding, but I can write it myself, but I am trying to take it. Do you have used the saving of IE5 as a .mht file? Yes, it is it! Turn the HTML message you need to send with IE5 locally, then save it as a MHT file, all encodings are obtained, and the link relationship of the picture is also ready, other as needed to adjust, post into your program Think of all things. Further, if you need to do it like 263, receive information such as a body, recipient, and a variety of possible accessories, etc. It is a bit complicated, nor is it to be discussed here. The master has done these things, you can contribute the experience, let us learn together, this is a throwing brick.