Home php sending attachments in PHP letter

sending attachments in PHP letter

Author

Date

Category

I write a mail script with an attachment.

$ file = "act.pdf"; // File
$ mailto = "[email protected]"; // to whom
$ from = "[email protected]"; // from whom
$ subject = "Public Reception"; // letter subject
$ Message = "Contact phone turned"; // text of the letter
$ R = Sendmailattachment ($ Mailto, $ From, $ Subject, $ Message, $ File); // sending a letter with attachment
echo ($ r)? '& lt; center & gt; & lt; h2 class = "action_title" & gt; Your application is sent! We will soon consider it and call you! & Lt; h2 & gt; & lt; / center & gt; ':' Error. The letter is not sent! ';
// $ R = SendmailAttachment ($ Mailto, $ From, $ Subject, $ Message); // Sending a letter without investment
// Echo ($ R)? 'letter sent': 'Error. The letter is not sent! ';
Function Sendmailattachment ($ Mailto, $ From, $ Subject, $ Message, $ File = False) {
  $ separator = "---"; // Separator in the letter
  // writing headers
  $ Headers = "Mime-Version: 1.0 \ R \ n";
  $ Headers. = "From: $ from \ Nreply-to: $ torm \ n"; // ask from whom the letter
  $ Headers. = "Content-Type: MultiPart / Mixed; Boundary = \" $ separator \ ""; // In the title Indicate the separator
  // if the letter with the attachment
  If ($ File) {
    $ bodymail = "- $ separator \ n"; // Beginning of the body of the letter, take the separator
    $ Bodymail. = "Content-Type: text / html; charset = \" UTF-8 \ "\ n"; // letter encoding
    $ bodymail. = "CONTENT-TRANSFER-ENCODING: 7BIT \ N \ N"; // Set the conversion of the letter
    $ bodymail. = $ message. "\ n"; // add text text
    $ bodymail. = "- $ separator \ n";
    $ FileRead = Fopen ($ File, "R"); // Open the file
    $ ContentFile = Fread ($ FileRead, FileSize ($ File)); // read it to the end
    FClose ($ FileRad); // Close File
    $ bodymail. = "CONTENT-TYPE: Application / OCTET-STREAM; Name ==? UTF-8? B?". Base64_ENCode (Basename ($ File)). "? = \ n";
    $ bodymail. = "CONTENT-TRANSFER-ENCODING: BASE64 \ N"; // File encoding
    $ Bodymail. = "CONTENT-DISPOSITION: attachment; filename ==? UTF-8? B?" Base64_ENCode (Basename ($ File)). "? = \ n \ n";
    $ Bodymail. = Chunk_Split (Base64_ENCode ($ ContentFile)). "\ n"; // Code and attach the file
    $ Bodymail. = "-". $ separator. "- \ n";
  // Letter without investment
  } else {
    $ bodymail = $ message;
  }
  $ result = mail ($ MailTo, $ Subject, $ Bodymail, $ Headers); // sending letter
  Return $ Result;
}

How can I add a second attachment from Input Type = “File” on the form?


Answer 1, Authority 100%

Three simple steps to solve this problem:

  1. look at the calendar and clarify which age is now in the yard.
  2. Gently highlight this code, and press the Del
  3. key

  4. download phpmailer
  5. forget all these handicrafts as a terrible dream.

As a result, the code should turn around:

request 'phpmailerautolaoad.php';
$ Mail = New phpMailer;
$ Mail- & gt; setFrom ('[email protected] ',' first last ');
$ Mail- & gt; addaddress ('[email protected] ',' john doe ');
$ Mail- & gt; subject = 'phpmailer file sender';
$ Mail- & gt; msghtml ("My Message Body");
  // Attach Uploaded Files
$ Mail- & gt; AddatTachment ($ FileName1);
$ Mail- & gt; AddatTachment ($ FileName2);
$ R = $ Mail- & gt; send ();

It should be understood that sending mail is not just copying into your script some specific combinations of symbols, they accidentally worked in the last century by the author of any dopup. This is a much more complex process that includes many nuances. And therefore sending mail should not be powered by manually from the remedies on the go, but entrust the proven and debugged decision.


Answer 2, Authority 50%

In the function, throw the $ _files array and then disassemble it:

Function SendmailAttachment ($ Mailto, $ From, $ Subject_Text, $ Message, $ _files) {
    $ to = $ MailTo;
    $ Eol = "\ r \ n"; // Limiter Limiter, Some mail servers require \ n - pick up an experienced way 
$ boundary = "-". MD5 (UNIQID (TIME ())); // Any string that will not be lower in the data stream.
    $ SUBJECT = '=? UTF-8? B?' . BASE64_ENCODE ($ subject_text). '? =';
    $ Headers = "Mime-Version: 1.0; $ Eol";
    $ Headers. = "Content-Type: MultiPart / Mixed; Boundary = \" $ Boundary \ "$ EOL";
    $ Headers. = "From: $ from \ Nreply-to: $ torm \ n";
    $ multipart = "- $ Boundary $ Eol";
    $ multipart. = "CONTENT-TYPE: text / html; charset = utf-8 $ EOL";
    $ multipart. = "CONTENT-TRANSFER-ENCODING: BASE64 $ EOL";
    $ multipart. = $ EOL; // Section between headlines and body HTML
    $ multipart. = chunk_split (Base64_ENCode ($ Message));
    # start inserting files
    Foreach ($ _ files ["File"] ["Name"] AS $ Key = & gt; $ value) {
      $ filename = $ _files ["File"] ["TMP_NAME"] [$ Key];
      $ File = Fopen ($ FileName, "Rb");
      $ Data = Fread ($ File, FileSize ($ FileName));
      FClose ($ File);
      $ Namefile = $ _files ["File"] ["Name"] [$ Key]; // in this variable you need to form a file name (without any way);
      $ File = $ DATA;
      $ multipart. = "$ EOL - $ Boundary $ Eol";
      $ multipart. = "Content-Type: Application / OCTET-STREAM; NAME = \" $ Namefile \ "$ EOL";
      $ multipart. = "CONTENT-TRANSFER-ENCODING: BASE64 $ EOL";
      $ multipart. = "Content-Disposition: attachment; filename = \" $ Namefile \ "$ EOL";
      $ multipart. = $ EOL; // Section between headlines and body of attached file
      $ multipart. = chunk_split (Base64_ENCode ($ File));
    }
    # & gt; & gt; End of file insertion
    $ multipart. = "$ EOL - $ Boundary - $ Eol";
    If (! Mail ($ To, $ Subject, $ Multipart, $ Headers)) {
      Echo 'not sent' letter ';
    } // send a letter
    ELSE {
      Echo 'letter sent';
    }
  }

p.s.: The code is checked for performance.


Answer 3

$ _ files – a global array is available everywhere, it is not necessary to transmit to the function. The way phpmailer is the best solution.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions