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

  • Home
  • SEARCH
  • 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 6755891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:27:25+00:00 2026-05-26T13:27:25+00:00

I am trying to create a PHP function to send an e-mail using SMTP

  • 0

I am trying to create a PHP function to send an e-mail using SMTP with an attachment. I am having a hard time trying to create the MIME headers as I want the body to include text in HTM format and the file to be attached.I am using a very old version of PHP (4.3.8) and this is the only method that is working. Tried PEAR, but won’t authenticate the SMTP correctly.

This is what I have so far:
I have edited this code as now I get the message correctly, but the file is corrupt. I changed the file into a text file and it starts out fine but then there appears a lot of garbage text.

$newLine = "\r\n";
$attachment="myFile.zip";
$message = "<br><h1><center>TEST MESSAGE</center></h1>" ;   


    //Body
    $headers .= "Content-Type: multipart/mixed;" . $newLine;
    $headers .= "     boundary=\"_e9e06aa5-1550-464d-ace4-e85b575d1899_\"" . $newLine . $newLine;   
    $newLine . $newLine;

    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . $newLine;
    $headers .= "     boundary=\"_7fdd1316-6f68-41bb-93f7-134933fc9aad_\"" . $newLine . $newLine;

    $headers .=  $message . $newLine;

    //$headers .= "--_7fdd1316-6f68-41bb-93f7-134933fc9aad_--" . $newLine; // If I leave this line it appears in the end of the message area.

    //Attachment
    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;

    $headers .= "Content-Transfer-Encoding: base64" . $newLine;
    $headers .= "Content-Type: text/plain;" . $newLine;
    $headers .= "Content-Disposition: attachment;" . $newLine;
    $headers .= "    filename=" . $attachment . $newLine . $newLine;

    $handle = fopen($attachment, "rb");
    $contents = '';
    while (!feof($handle)) {
        $contents .= fread($handle, filesize($attachment));
    }
    fclose($handle);

    $contents = base64_encode($contents);

    $headers .= $contents . $newLine;
    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_--" . $newLine;
  • 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-05-26T13:27:26+00:00Added an answer on May 26, 2026 at 1:27 pm

    What about using ready class like PHPMailer? It’s much much eaysier. And there is version for PHP4.

    Example:

    require_once '../class.phpmailer.php';
    
    $mail = new PHPMailer();
    $mail->AddReplyTo('name@yourdomain.com', 'First Last');
    $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    $mail->AddReplyTo('name@yourdomain.com', 'First Last');
    $mail->Subject = 'PHPMailer Test Subject via mail()';
    $mail->MsgHTML('Hello world');
    $mail->AddAttachment('file.zip');
    $mail->Send();
    

    Making custom headers can make headache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a iPhone Objective C login page using PHP/MySQL to authenticate.
I'm trying to create a simple database table using the PHP MySQL query Create
I am trying to create a table in MySql using php. My code looks
I'm trying to create a AJAX push implementation in PHP using a Comet long-polling
I am trying to create a PHP page for users to send emails to
I am trying to create a go-moku game using jquery,php, and mysql database. I
I am trying to create a php file that adds a user and create
I'm trying to create system users with a php script securely, In that, I'd
I'm trying to create a generic class in PHP that will provide a way
I'm trying to create an SQL query in PHP to update a table. Is

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.