Php code for sending attachments

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to send emails in php. You can send emails with attachments. 1, emailclass. php? ClassCMailFile {var $ subject; var $ addr_to; var $ text_body; var $ text_encoded; var $ mime_headers; var $ mime_boundary --

Welcome to the Linux community forum, interact with 2 million technical staff> go to php to send an email, and you can send an email with an attachment. 1, emailclass. php? Class CMailFile {var $ subject; var $ addr_to; var $ text_body; var $ text_encoded; var $ mime_headers; var $ mime_boundary = "-- ======

Welcome to the Linux community forum and interact with 2 million technicians>

Php can send emails with attachments.

1. emailclass. php

  

Class CMailFile {

Var $ subject;

Var $ addr_to;

Var $ text_body;

Var $ text_encoded;

Var $ mime_headers;

Var $ mime_boundary = "-- =========================_ 846811060 == _";

Var $ smtp_headers;

Function CMailFile ($ subject, $ to, $ from, $ msg, $ filename, $ downfilename, $ mimetype = "application/octet-stream", $ mime_filename = false ){

$ This-> subject = $ subject;

$ This-> addr_to = $;

$ This-> smtp_headers = $ this-> write_smtpheaders ($ from );

$ This-> text_body = $ this-> write_body ($ msg );

$ This-> text_encoded = $ this-> attach_file ($ filename, $ downfilename, $ mimetype, $ mime_filename );

$ This-> mime_headers = $ this-> write_mimeheaders ($ filename, $ mime_filename );

}

Function attach_file ($ filename, $ downfilename, $ mimetype, $ mime_filename ){

$ Encoded = $ this-> encode_file ($ filename );

If ($ mime_filename) $ filename = $ mime_filename;

$ Out = "--". $ this-> mime_boundary. "\ n ";

$ Out = $ out. "Content-type:". $ mimetype. "; name = \" $ filename \ "; \ n ";

$ Out = $ out. "Content-Transfer-Encoding: base64 \ n ";

$ Out = $ out. "Content-disposition: attachment; filename = \" $ downfilename \ "\ n ";

$ Out = $ out. $ encoded. "\ n ";

$ Out = $ out. "--". $ this-> mime_boundary. "--". "\ n ";

Return $ out;

}

Function encode_file ($ sourcefile ){

If (is_readable ($ sourcefile )){

$ Fd = fopen ($ sourcefile, "r ");

$ Contents = fread ($ fd, filesize ($ sourcefile ));

$ Encoded = chunk_split (base64_encode ($ contents ));

Fclose ($ fd );

}

Return $ encoded;

}

Function sendfile (){

$ Headers = $ this-> smtp_headers. $ this-> mime_headers;

$ Message = $ this-> text_body. $ this-> text_encoded;

Mail ($ this-> addr_to, $ this-> subject, $ message, $ headers );

}

Function write_body ($ msgtext ){

$ Out = "--". $ this-> mime_boundary. "\ n ";

$ Out = $ out. "Content-Type: text/plain; charset = \" us-ascii \ "\ n ";

$ Out = $ out. $ msgtext. "\ n ";

Return $ out;

}

Function write_mimeheaders ($ filename, $ mime_filename ){

If ($ mime_filename) $ filename = $ mime_filename;

$ Out = "MIME-version: 1.0 \ n ";

$ Out = $ out. "Content-type: multipart/mixed ;";

$ Out = $ out. "boundary = \" $ this-> mime_boundary \ "\ n ";

$ Out = $ out. "Content-transfer-encoding: 7BIT \ n ";

$ Out = $ out. "X-attachments: $ filename; \ n ";

Return $ out;

}

[1] [2]

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.