Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Jquery发送带有附件的电子邮件_Php_Jquery_Email_Email Attachments - Fatal编程技术网

Php Jquery发送带有附件的电子邮件

Php Jquery发送带有附件的电子邮件,php,jquery,email,email-attachments,Php,Jquery,Email,Email Attachments,我正在使用jquery上传附件,以便通过ajax发布文件名。我在数组中存储文件名并在循环中显示每个文件名时遇到问题,因为它将使用以下代码在一个索引中存储数组中的两个文件名: $(".file-list_yhs").each(function() { attachments[i++] = $(this).find('.vI').text(); }); 输出: Array { [0] => uploads/add email.txtemail.zip } Array 应该是

我正在使用jquery上传附件,以便通过ajax发布文件名。我在数组中存储文件名并在循环中显示每个文件名时遇到问题,因为它将使用以下代码在一个索引中存储数组中的两个文件名:

$(".file-list_yhs").each(function() {
    attachments[i++] = $(this).find('.vI').text();
});
输出:

Array
{
    [0] => uploads/add email.txtemail.zip
}
Array
应该是:

Array
{
    [0] => uploads/add email.txt
}
Array
{
    [1] => uploads/email.zip
}
以下是完整的代码:

$(document).on('click','#domodal_send', function(e) {

    if ($(".notify-outer").css('display') == 'none') {

        var email = $("#domodal_email_receipt").val();
        var subject = $("#domodal_subject").val();
        var from_name = $("#avater_name").text();
        var from_email = $('#avater_email').text();
        var emailbody = $("#domodal_message").html();
        var attachments = [];
        var i = 0;

        $(".file-list_yhs").each(function() {
            attachments[i++] = $(this).find('.vI').text();
        });


         $.ajax({
                url: 'sendMail.php',
                type: 'POST',

                data : {
                    send_to: email,
                    from: from_name,
                    from_email: from_email,
                    emailsubject: subject,
                    emailbody: emailbody,
                    emailattachment: attachments
                },

                success: function(result)
                {
                    alert(result);
                }
         });
    }
});
SendMail.php:

<?php
require_once "Mail.php";
require_once "Mail/mime.php";
require_once('Mail/IMAPv2.php');

//Connect to the database
include('config.php');

$inbox = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX';
$mailserver = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX.Sent';

if (isset($_POST['send_to']))
{
    $from = "Robert <rob@example.com>";
    $to_email = $_POST['send_to'];
    $to = $firstname . " <$to_email>";
    $subject = $_POST['emailsubject'];
    $message = $_POST['emailbody'];
    $message_without_image = $_POST['emailbody'];
    $filenames =  $_POST['emailattachment'];
    $files = [];

    foreach ($filenames as $filename) {
        $files[] = 'uploads/' . $filename;
    }
    print_r($files);

    $username = 'username';
    $password = 'password';
    $smtp_hostname = "smtp.example.com";
    $port = "587";
    echo $files;

    /*$messageID = sprintf("<%s.%s@%s>",
            base_convert(microtime(), 10, 36),
            base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
            'example.com');
    $date = date('Y-m-d H:i:s');
    $emailID = '';
    $sent_db = "SELECT email_id FROM sent WHERE email_id = '$messageID'";
    $sent_result = mysqli_query($link, $sent_db) or die($link->error);

    if(mysqli_num_rows($sent_result) == 0)
    {
        $sent_db1 = "INSERT INTO sent (id, email_id, email, sendDateTime, isRead, readDateTIme) VALUES ('', '$messageID', '$to', '$date', '0', '0000-00-00 00:00:00')";

        if (mysqli_query($link, $sent_db1)) {
            $emailID = mysqli_insert_id($link);
        }
    }
    $message .= "<img src=\"http://test.example.com/project/track/Images/signature.gif?id=".$emailID."&etc=".time()." \" style=\"width: 0; max-height:0; overflow:hidden; \">";

    $headers = array ('From' => $from, 
        'To' => $to, 'Subject' => $subject,
        'Reply-To' => $from,
        'Content-Type'  => 'Content-Type: text/plain; charset=UTF-8',
        'MIME-Version' => '1.0',
        'Received' => 'from smtp.example.com',
        'Date'  => date("r"),
        'Message-ID' => sprintf("<%s.%s@%s>",
            base_convert(microtime(), 10, 36),
            base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
            'example.com'));

    $params = array ('host' => $smtp_hostname,
        'port' => $port,
        'auth' => 'PLAIN', // Note 1
        'socket_options' => array('ssl' => array('verify_peer_name' => false, 'verify_peer' => false)), // Note 2
        'username' => $username,
        'password' => $password);

    $mime_params = array(
        'text_encoding' => '7bit',
        'text_charset'  => 'UTF-8',
        'html_charset'  => 'UTF-8',
        'head_charset'  => 'UTF-8'
    );

    $crlf = "\r\n";
    $mime = new Mail_mime(array('eol' => $crlf));
    $body = $message;

    $mime->setTXTBody($body);
    $mime->setHTMLBody($body);

    if(!empty($files)) {
        $mime->addAttachment($files);
    }
    $body = $mime->get($mime_params);
    $headers = $mime->headers($headers);
    $smtp = Mail::factory ('smtp', $params);
    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) 
    {
        echo("<p>" . $mail->getMessage() . "</p>");
    }
    else 
    {
        echo("<p>Email has been sent!</p>");
    }
}
?>

您正在为每个
.file-list\u yhs
DIV创建一个数组元素,而不是为每个
.vI
DIV创建一个数组元素。
$(此)。find('.vI').text()
将连接它在每个
.file-list\u yhs
中找到的所有
.vI
元素的文本

将循环更改为

$(".file-list_yhs .vI").each(function() {
    attachments.push($(this).text());
}
您还可以使用
.map

attachments = $(".file-list_yhs .vI").map(function() {
    return $(this).text();
}).get();

最后需要使用
.get()
.map()
返回的jQuery对象转换为数组。

您正在为每个
.file-list\u yhs
DIV创建数组元素,而不是为每个
.vI
DIV.
$(this).查找('.vI').text()
将连接它在每个
.file-list\u yhs
中找到的所有
.vI
元素的文本

将循环更改为

$(".file-list_yhs .vI").each(function() {
    attachments.push($(this).text());
}
您还可以使用
.map

attachments = $(".file-list_yhs .vI").map(function() {
    return $(this).text();
}).get();

最后需要使用
.get()
.map()
返回到数组的jQuery对象转换为数组。

我在“完整代码”中没有看到填充
附件的部分。该代码位于何处?@showdev完整代码位于index.php,发送电子邮件的代码位于sendmail.php。你想让我为我上传文件到服务器的附件添加代码吗?只是我没有看到
attachments[I++]=$(this.find('.vI').text()部分,你说“这是完整的代码”。哦,
附件只是一个数组,因为我用它来存储文件名。您想将
vI
附带的html作为一个类来查看吗?为什么希望看到两个不同的
Array{…}
值?我想你应该期待
Array{[0]=>uploads/add email.txt[1]=>uploads/email.zip}
我没有看到你在“完整代码”中填充
附件的部分。该代码位于何处?@showdev完整代码位于index.php,发送电子邮件的代码位于sendmail.php。你想让我为我上传文件到服务器的附件添加代码吗?只是我没有看到
attachments[I++]=$(this.find('.vI').text()部分,你说“这是完整的代码”。哦,
附件只是一个数组,因为我用它来存储文件名。您想将
vI
附带的html作为一个类来查看吗?为什么希望看到两个不同的
Array{…}
值?我想你应该期待
Array{[0]=>uploads/add email.txt[1]=>uploads/email.zip}
非常感谢你,我可以看到循环的第一个代码工作得很好,因为我现在得到了这个
Array{[0]=>add email.txt}Array{[1]=>email.zip}
。问题现在已解决。您应该得到
Array{[0]=>add email.txt[1]=>email.zip}
是的,我现在收到了,谢谢您的帮助。问题现在已经解决。非常感谢您的帮助,我可以看到循环的第一个代码工作得很好,因为我现在得到了这个
Array{[0]=>add email.txt}Array{[1]=>email.zip}
。问题现在已解决。您应该得到
Array{[0]=>add email.txt[1]=>email.zip}
是的,我现在收到了,谢谢您的帮助。问题现在已经解决了。