Send MIME mail in Perl using Sendmail

zhaozj2021-02-08  242

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

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

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

= d0 = bb = d0 = bb = c4 = fa = b5 = c4 = b2 = CE = D3 = EB = a3 = a1 </ title> <meta http-equiv = 3dContent-Type content = 3D "text / html; charset = 3Dgb2312"> <META content = 3D "MSHTML 6.00.2800.1126" name = 3DGENERATOR> </ HEAD> <BODY text 3D # 000000 bgColor = 3D # ffffff leftMargin = 3D0 topMargin = 3D0 = > <Img = height = 3d400 = 20SRC = 3D "file: /// c: /dev/perl/images/popup_thanks.gif" = width = 3D400 = 20border = 3d0> </ body> </ html></p> <p>------ = _ 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</p> <p>R0lGODlhkAGQAfcAAPCyTdvr0cvbCJ8tTt3nAEeRGarPkRSmULrVqrRtLtbeB7CRNdTZ0LGtNaXJEaLR7ejz23e1GFir2NNMayaR0VWaLfjMRtnINsfWuYi6aGWsNevKN5PHdLZKMyZLC6smNs3nu8mINWetGBIqCOqXNdutkWyRRzZvEqvIK9fJSZGMNrSxSlalGO / 36rS5revt6MySScswSperNZWtT9C3</p> <p>------ = _ nextpart_000_0008_01c2bcb0.9cf9ae70 -</p> <p>END_TAG</p> <p>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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-2638.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="2638" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.076</b>, SQL: <b>10</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'recHu0ar7bVzsI5dZZnbADDYhPfKv1AWEwcL9rINN1H7U4_2FBvSHH393pGLKBnXUN51S8G8CafADOb8Ak'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>