Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8101379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:58:05+00:00 2026-06-05T22:58:05+00:00

After trying to read various articles on sending emails with attachments in PHP (I

  • 0

After trying to read various articles on sending emails with attachments in PHP (I am use to ASP with VBScript), I wrote the code below. Unfortunately, it does not work at all. Not only does it not send the email with the attachment, the email doesn’t seem to send at all, even though my script says that it did send. Where have I gone wrong? I’m not using a form to upload a file. This is a static script.

<?php

$EmailTo = "Me@here.com";
$EmailFrom = "You@There.com";
$EmailSubject = "The Email Subject";

$MailBoundary = md5(uniqid(time()));

$Headers = "To: ". $EmailTo . "\r\n";
$Headers .= "From: ". $EmailFrom . "\r\n";
$Headers = "MIME-Version: 1.0\r\n";
$Headers .= "Content-type: multipart/mixed;boundary=\"$MailBoundary \"";
$Headers .= "\r\n\r\n";
$Headers .= "This is a multi-part message in MIME format.";
$Headers .= "\r\n\r\n";

$FileAttachment = "AttachedFile.pdf";
$File = fopen($FileAttachment, "r");
$FileData = fread($File, filesize($FileAttachment));
$FileData = chunk_split(base64_encode($FileData));
$FileName = basename($FileAttachment);

$EmailBody = "--$MailBoundary\r\n";
$EmailBody .= "Content-type: text/html; charset=iso-8859-1\r\n";
$EmailBody .= "Content-transfer-encoding: 8bit\r\n\r\n";

$EmailBody .= "<html>" . chr(13) .
              "<head>" . chr(13) .
              "<style>" . chr(13) .
              ".breg {font-family:arial;font-size:10pt;color:#000000;padding:5px;}" . chr(13) .
              "</style>" . chr(13) .
              "</head>" . chr(13) .
              "<body>" . chr(13) .
              "<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) .
              "The message text body goes here" . chr(13) .
              "</div>" . chr(13) .
              "</body>" . chr(13) .
              "</html>";

$EmailBody .= "--$MailBoundary\r\n";


$EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName\r\n";
$EmailBody .= "Content-transfer-encoding:base64\r\n\r\n";
$EmailBody .= $FileData. "\r\n\r\n";

$EmailBody .= " --$MailBoundary--";

if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers))
{
 echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13);
}
else
{
 echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13);
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:58:07+00:00Added an answer on June 5, 2026 at 10:58 pm
    For sending mail with attachment using php mail().Try this code:
    
    <?php
    
     //If there is no error, send the email
     if(isset($_POST['ur_submit_button_name'])) {
      $EmailTo = "Me@here.com";
      $EmailFrom = "You@There.com";
      $EmailSubject = "The Email Subject";
    
    
      $separator = md5(time());
    
      // carriage return type (we use a PHP end of line constant)
      $eol = PHP_EOL;
    
      // attachment name
      $filename = "ip.zip";//store that zip file in ur root directory
      $attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
    
      // main header
      $headers  = "From: ".$from.$eol;
      $headers .= "MIME-Version: 1.0".$eol; 
      $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
    
      // no more headers after this, we start the body! //
    
      $body = "--".$separator.$eol;
      $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
      $body .= "This is a MIME encoded message.".$eol;
    
      // message
      $body .= "--".$separator.$eol;
      $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
      $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
      $body .= $message.$eol;
    
      // attachment
      $body .= "--".$separator.$eol;
      $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
      $body .= "Content-Transfer-Encoding: base64".$eol;
      $body .= "Content-Disposition: attachment".$eol.$eol;
      $body .= $attachment.$eol;
      $body .= "--".$separator."--";
    
      // send message
      if (mail($to, $subject, $body, $headers)) {
      $mail_sent=true;
      echo "mail sent";
      } else {
      $mail_sent=false;
      echo "Error,Mail not sent";
    
     }
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read several articles/questions/forums discussing the best auto-complete plugin for jQuery. After trying
Ok, after failing to read a polynomial, I'm trying first a basic approach to
I've read in various books/articles that some Bindings, for example netTcpBinding and netMsmqBinding can
I'm trying to use jQuery Mobile for a mobile website (after trying and ditching
I'm trying to read specific values from the text file (below): Current Online Users:
I am trying to read characters from a file and after removing punctuations. I
Hello I am trying to read a textbox(runatserver) after is gets populated form the
After trying this simple console input with 5, the result is shown as 53
After trying to figure how to have an effective word counter of a string,
After trying to make a while(bool) loop and it failing because I couldn't see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.