PHP sends a class with an attachment message

Source: Internet
Author: User
Tags sendfile
  1. /**
  2. * Send a message with an attachment
  3. * by bbs.it-home.org
  4. */
  5. Class Cmailfile {
  6. var $subject;
  7. var $addr _to;
  8. var $text _body;
  9. var $text _encoded;
  10. var $mime _headers;
  11. var $mime _boundary = "--==================_846811060==_";
  12. var $smtp _headers;
  13. function Cmailfile ($subject, $to, $from, $msg, $filename, $downfilename, $mimetype = "Application/octet-stream", $mime _ filename = False) {
  14. $this->subject = $subject;
  15. $this->addr_to = $to;
  16. $this->smtp_headers = $this->write_smtpheaders ($from);
  17. $this->text_body = $this->write_body ($msg);
  18. $this->text_encoded = $this->attach_file ($filename, $downfilename, $mimetype, $mime _filename);
  19. $this->mime_headers = $this->write_mimeheaders ($filename, $mime _filename);
  20. }
  21. function Attach_file ($filename, $downfilename, $mimetype, $mime _filename) {
  22. $encoded = $this->encode_file ($filename);
  23. if ($mime _filename) $filename = $mime _filename;
  24. $out = "--". $this->mime_boundary. "\ n";
  25. $out = $out. "Content-type:". $mimetype. "; Name=\ "$filename \"; \ n ";
  26. $out = $out. "Content-transfer-encoding:base64\n";
  27. $out = $out. "Content-disposition:attachment; Filename=\ "$downfilename \" \ n ";
  28. $out = $out. $encoded. "\ n";
  29. $out = $out. "--" . $this->mime_boundary. "--" . "\ n";
  30. return $out;
  31. }
  32. function Encode_file ($sourcefile) {
  33. if (is_readable ($sourcefile)) {
  34. $FD = fopen ($sourcefile, "R");
  35. $contents = Fread ($FD, FileSize ($sourcefile));
  36. $encoded = Chunk_split (Base64_encode ($contents));
  37. Fclose ($FD);
  38. }
  39. return $encoded;
  40. }
  41. function Sendfile () {
  42. $headers = $this->smtp_headers. $this->mime_headers;
  43. $message = $this->text_body. $this->text_encoded;
  44. Mail ($this->addr_to, $this->subject, $message, $headers);
  45. }
  46. function Write_body ($msgtext) {
  47. $out = "--". $this->mime_boundary. "\ n";
  48. $out = $out. "Content-type:text/plain; Charset=\ "us-ascii\" \ n ";
  49. $out = $out. $msgtext. "\ n";
  50. return $out;
  51. }
  52. function Write_mimeheaders ($filename, $mime _filename) {
  53. if ($mime _filename) $filename = $mime _filename;
  54. $out = "mime-version:1.0\n";
  55. $out = $out. "Content-type:multipart/mixed;";
  56. $out = $out. "Boundary=\" $this->mime_boundary\ "\ n";
  57. $out = $out. "Content-transfer-encoding:7bit\n";
  58. $out = $out. "x-attachments: $filename; \ n";
  59. return $out;
  60. }
  61. function write_smtpheaders ($addr _from) {
  62. $out = "From: $addr _from\n";
  63. $out = $out. "Reply-to: $addr _from\n";
  64. $out = $out. "X-mailer:php3\n";
  65. $out = $out. "X-sender: $addr _from\n";
  66. return $out;
  67. }
  68. }
  69. /* Usage-for example: MimeType to "Image/gif"
  70. $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $mimetype);
  71. $mailfile->sendfile ();
  72. $subject--Themes
  73. $sendto--The address of the person receiving the letter
  74. $replyto--Reply to address
  75. $message--contents of the letter
  76. $filename--Attachment file name
  77. $downfilename--The file name of the download
  78. $mimetype--MIME type
  79. */
  80. ?>
Copy Code

2, demo example demo.php

    1. Require_once (' emailclass.php ');
    2. Send mail
    3. Main topic
    4. $subject = "Test send email";
    5. Recipient
    6. $sendto = ' abc@163.com ';
    7. Sender
    8. $replyto = ' cdf@163.com ';
    9. Content
    10. $message = "Test send email content";
    11. Attachment
    12. $filename = ' test.jpg ';
    13. Annex Category
    14. $mimetype = "Image/jpeg";
    15. $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $excelname, $mimetype);
    16. $mailfile->sendfile ();
    17. ?>
Copy Code

>>> you may be interested in the article: PHP socket using SMTP to send messages with attachments IMAP application example in PHP (send and receive mail, delete mail, attachment download) Phpmailer Send an example with an attachment message phpmailer send a message Chinese attachment name garbled solution

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.