0

I want to attach a image as an attachment using mail() function.
I am using xampp and want the image to be sent from my computer to an email id. This code is sending text email easily:

<?php 
    if(mail('abc@gmail.com','Hello','Testing  Testing','From:xyz@gmail.com'))
    {
       echo "Success";
    } else {
       echo "Fail";
    }
?>

I want to add an image after it using normal mail method of php.

kumar
  • 1,796
  • 2
  • 15
  • 37
user2280276
  • 125
  • 1
  • 2
  • 7
  • 1
    simple advise: don't. use alternatives like swiftmailer or phpmailer. – eis Apr 20 '13 at 09:49
  • i want to keep it simple! – user2280276 Apr 20 '13 at 09:51
  • yes, I can read. I was just advising not to do that. with mail(), you'll have a lot of issues to deal with that you don't have to resolve using alternatives. – eis Apr 20 '13 at 09:51
  • that is why you want to use the alternatives - to keep it simple. implementing these by yourself using mail() is *hard* to get right and working reliably in different scenarios. – eis Apr 20 '13 at 09:54
  • hmmm .... how can it be done using phpmailer.I haven't used it till now. – user2280276 Apr 20 '13 at 09:55

4 Answers4

1

you need to use the pear library for composing or sending the mail.

 include_once('Mail.php');
 include_once('Mail_Mime/mime.php');
 $message = new Mail_mime();

 $message->setTXTBody($text);

$message->addAttachment($path_of_uploaded_file);

$body = $message->get();

$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);

$headers = $message->headers($extraheaders);

$mail = Mail::factory("mail");

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

here is a way

Rudra
  • 354
  • 1
  • 2
  • 10
0

You could use the Mail class from the Zend library, very simple and no reliance on PEAR.

Its been covered in a previous question here.

Community
  • 1
  • 1
Paul J
  • 60
  • 4
0

I suggest to use Swiftmailer. It is up to date, easy to install and use. You can install it via PEAR, but there are lots of other options you might find more convenient as well.

Example code to send a mail with an attachement taken from the manual:

require_once 'lib/swift_required.php';

 // Create the message
 $message = Swift_Message::newInstance()

 // Give the message a subject
 ->setSubject('Your subject')

 // Set the From address with an associative array
 ->setFrom(array('john@doe.com' => 'John Doe'))

 // Set the To addresses with an associative array
 ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))

 // Give it a body
 ->setBody('Here is the message itself')

 // And optionally an alternative body
->addPart('<q>Here is the message itself</q>', 'text/html')

// Optionally add any attachments
->attach(Swift_Attachment::fromPath('my-document.pdf'));
herrjeh42
  • 2,782
  • 4
  • 35
  • 47
0

this is using php and ajax it will work 100%

 <?php
        include "db.php";
        if(isset($_POST['tourid']))
       {

        $to=$_POST['email'];
        $file_name = "test/sample.pdf";                               
        require 'class/class.phpmailer.php';
        $mail = new PHPMailer;
        $mail->IsSMTP();                                           //Sets Mailer to send message using SMTP
        $mail->Host = '';                                           //Sets the SMTP hosts of your Email hosting, this for Godaddy
        $mail->Port = '';                                       //Sets the default SMTP server port
        $mail->SMTPAuth = true;                                     //Sets SMTP authentication. Utilizes the Username and Password variables
        $mail->Username = '';                                        //Sets SMTP username
        $mail->Password = '';                                       //Sets SMTP password
        $mail->SMTPSecure = '';                                      //Sets connection prefix. Options are "", "ssl" or "tls"
        $mail->From = '';                                              //Sets the From email address for the message
        $mail->FromName = '';                                          //Sets the From name of the message
        $mail->AddAddress($to, 'Name');                                 //Adds a "To" address
        $mail->WordWrap = 50;                         ``                  //Sets word wrapping on the body of the message to a given number of characters
        $mail->IsHTML(true);                                            //Sets message type to HTML 
        $mail->AddAttachment($file_name);                                //Adds an attachment from a path on the filesystem
        $mail->Subject = 'Customer Details';                             //Sets the Subject of the message
        $mail->Body = 'Please Find Tour details in attached PDF File.';  //An HTML or plain text message body
        if($mail->Send())                                                //Send an Email. Return true on success or false on error
        {
           $message = '<label class="text-success">Tour Details has been send successfully...</label>';
           echo $message;
           unlink($file_name);
        }

        }
         else
        {

             echo "sending error";
        }



        ?>
Prakash Bhosale
  • 227
  • 1
  • 7