使用php mail发送附件()

时间:2013-04-20 09:48:19

标签: php

我想使用mail()函数附加图像作为附件 我正在使用xampp并希望将图像从我的计算机发送到电子邮件ID。 此代码可以轻松发送文本电子邮件:

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

我想在使用php的普通邮件方法后添加图片。

4 个答案:

答案 0 :(得分:1)

你需要使用梨库来编写或发送邮件。

 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);

这是一个way

答案 1 :(得分:0)

您可以使用Zend库中的Mail类,非常简单,不依赖于PEAR。

上一个问题here已经涵盖了这一点。

答案 2 :(得分:0)

我建议使用Swiftmailer。它是最新的,易于安装和使用。您可以通过PEAR install,但是您可能会发现其他许多选项也更方便。

发送带有附件的邮件的示例代码:

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'));

答案 3 :(得分:0)

这是使用php和ajax它将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";
        }



        ?>
相关问题