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 957449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:42:08+00:00 2026-05-16T00:42:08+00:00

I am sending emails successfully using following code. But now I want to attach

  • 0

I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?

require_once "Mail.php";

$from = "Usman <from@example.com>";
$to = "Naveed <to@example.com>";
$subject = "subject";
$body = "";

$host = "smtp.gmail.com";
$username = "username";
$password = "password";

$headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);

$smtp = Mail::factory( 'smtp', array('host' => $host,
          'auth' => true,
          'username' => $username,
          'password' => $password ) );

$mail = $smtp->send( $to, $headers, $body );

if ( PEAR::isError($mail) ) {
echo( "<p>" . $mail->getMessage() . "</p>" );
} else {
echo( "<p>Message successfully sent!</p>" );
}
  • 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-16T00:42:08+00:00Added an answer on May 16, 2026 at 12:42 am

    Found this code as one of the first hits of the google://pear mail attachment search.

    include('Mail.php');
    include('Mail/mime.php');
    
    $text = 'Text version of email';
    $html = '<html><body>HTML version of email</body></html>';
    $file = './files/example.zip';
    $hdrs = array(
                  'From'    => 'someone@domain.pl',
                  'To'      => 'someone@domain.pl',
                  'Subject' => 'Test mime message'
                  );
    
    $mime = new Mail_mime();
    
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html);
    
    $mime->addAttachment($file,'application/octet-stream');
    
    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
    
    $mail =& Mail::factory('mail', $params);
    $mail->send('mail@domain.pl', $hdrs, $body); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using the Code Snippet for sending email in VB.Net I have successfully sent an
I'm using SMTPClient for sending emails to a SMTP server. But with Send method
Hey all. We're sending quite a few emails (around 23k) using IIS6 SMTP service
Using the obsolete System.Web.Mail sending email works fine, here's the code snippet: Public Shared
I am sending emails via an html page using javascript and the outlook activex
I am practicing sending emails with Google App Engine with Python. This code checks
I have several different c# worker applications that run various continuous tasks: sending emails
Our company is sending out a lot of emails per day and planning to
How can I test sending email from my application without flooding my inbox? Is
I want to send notification on Yammer through Ant, but no notification are sent.

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.