0

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

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

我想使用php的普通邮件方法在它之后添加一个图像。

4

4 回答 4

1

您需要使用 pear 库来撰写或发送邮件。

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

这是一种方法

于 2013-04-20T10:10:46.683 回答
0

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

它已在前面的问题中介绍

于 2013-04-20T09:57:21.777 回答
0

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

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

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'));
于 2013-04-20T11:50:51.223 回答
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";
        }



        ?>
于 2020-03-03T10:03:28.510 回答