【发布时间】:2019-11-12 10:09:15
【问题描述】:
我正在使用 jquery 上传附件,以便我可以通过 ajax 发布文件名。我在将文件名存储在数组中并在循环中显示每个文件名时遇到问题,因为它将使用以下代码将数组中的 2 个文件名存储在一个索引中:
$(".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>");
}
}
?>
我要做的是将文件名存储在数组中,并在将文件上传到服务器后通过 ajax 发布。当文件上传到服务器时,我可以输出每个文件名来添加服务器上的文件位置并发送带有附件的电子邮件。
你能告诉我一个例子,我可以如何将每个文件名存储在循环中,我可以使用它用 ajax 发布它以输出每个文件名吗?
谢谢。
编辑:这是html:
<div style="width: 70%; margin-left: 10px; position:relative;" class="file-list_yhs">
<div class="dL" tabindex="-1" id=":vw" aria-label="Attachment: edit_link1.png. Press enter to view the attachment and delete to remove it" style="position:relative;">
<input id=":vp" name="attach" type="hidden" value="#msg-a:r-6685135251679589431_#msg-a:r-6685135251679589431_null_-1" checked="">
<div style="display:flex">
<a class="dO" id=":vq" style="display:flex;">
<div id="domodal_attachment" class="vI">addd her email.txt</div>
<div class="vJ">(0K)</div>
</a>
<div id=":vn" role="button" aria-label="Remove attachment" class="vq" tabindex="-1" data-tooltip="Remove attachment" style="position:absolute; right:5px; top:5px;"></div>
</div>
</div>
<div class="dL" tabindex="-1" id=":vw" aria-label="Attachment: edit_link1.png. Press enter to view the attachment and delete to remove it" style="position:relative;">
<input id=":vp" name="attach" type="hidden" value="#msg-a:r-6685135251679589431_#msg-a:r-6685135251679589431_null_-1" checked="">
<div style="display:flex">
<a class="dO" id=":vq" style="display:flex;">
<div id="domodal_attachment" class="vI">email.zip</div>
<div class="vJ">(183K)</div>
</a>
<div id=":vn" role="button" aria-label="Remove attachment" class="vq" tabindex="-1" data-tooltip="Remove attachment" style="position:absolute; right:5px; top:5px;"></div>
</div>
</div>
</div>
【问题讨论】:
-
我没有看到您在“完整代码”中填充
attachments的部分。它位于该代码中的什么位置? -
@showdev 完整代码位于 index.php 中,发送电子邮件的代码位于 sendmail.php 中。你想让我添加附件的代码,我将文件上传到我的服务器吗?
-
只是在您说“这里是完整代码”的部分中我没有看到
attachments[i++] = $(this).find('.vI').text();部分。 -
哦,
attachments只是一个数组,因为我用它来存储文件名。你想看看vI作为一个类附带的html吗? -
为什么您期望看到两个不同的
Array{...}值?我想你应该期待Array{[0] => uploads/add email.txt [1] => uploads/email.zip}
标签: php jquery email email-attachments