-1

我正在尝试创建一个脚本,该脚本从用户那里获取文件,将其编码为 base64,然后将其作为带有编码文件的附件转发到电子邮件。

这是到目前为止的代码:

客户端 :

<form action="send.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" value="Submit" name='submit'>
</form>

PHP 脚本 (send.php)

<?php
$uploaded_file = basename($_FILES['uploaded_file']['name']);
$file_size = filesize($uploaded_file);
$handle = fopen($uploaded_file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content1 = base64_encode($content);
$my_file = "'$encoded_file' . '_encoded' . '.html'";
$handle1 = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
fwrite($handle1, $content1);
$from="tes@gmail.com";
$to="myemail@gmail.com"
$subject="New file";
$message="you got a new file";
mail($from,$to,$subject,$message);
unlink($my_file);
?>

我不知道如何在电子邮件中附加 $myfile。

4

2 回答 2

1

你可以只使用PHPMailer

$mail = new PHPMailer(); // defaults to using php "mail()"

$body = file_get_contents('contents.html');
$body = preg_replace('/[\]/', '', $body);

$mail->SetFrom('tes@gmail.com', 'First Last');
$mail->AddReplyTo("tes@gmail.com", "First Last");
$mail->AddAddress("myemail@gmail.com", "John Doe");
$mail->Subject = "New File";
$mail->AltBody = "you got a new file";
$mail->MsgHTML($body);


$my_file = file_put_contents("tmp.html", base64_encode($content));
$mail->AddAttachment($my_file); // attachment


if (! $mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
于 2012-12-17T11:47:32.120 回答
0

如果您没有使用任何外部邮件库,例如 PEAR::SMTP 或 Swift,那么创建附件并不是一件简单的事情。

您必须检查他们指示的邮件标准,了解如何将标头与邮件内容以及邮件内容与附件分开。

分隔符是\r\n,并且每个部分之前必须有一个分隔符。

检查此链接:http ://webcheatsheet.com/php/send_email_text_html_attachment.php

我的代码示例:

$uid = md5(uniqid(time()));
$header = implode("\r\n",
    array(
        'MIME-Version: 1.0',
        'From: Address <address@example.com>',
        'Reply-To: Address <address@example.com>',
        'Cc: Address <address@example.com>',
        'Content-Type: multipart/mixed;',
        '    boundary="'.$uid.'"',
        '',
        '--'.$uid,
        'Content-Type: text/plain;',
        '    charset="us-ascii"',
        'Content-Transfer-Encoding: 7bit',
        '',
        mb_convert_encoding(
            implode("\r\n",
                array_map('wordwrap',
                    array(
                        'Hello,',
                        '',
                        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla laoreet varius augue non pretium. Nullam dignissim iaculis velit, ut cursus neque mollis et. Fusce faucibus dictum nunc, sit amet cursus tellus aliquam ut. Praesent id libero orci, ut vehicula orci. Proin nulla sem, dapibus in lacinia vel, pharetra eget eros. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras rhoncus, enim sed mollis suscipit, sapien mauris suscipit mauris, nec placerat nisi sapien ac lectus. Sed vehicula faucibus iaculis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer placerat commodo nibh. Morbi sit amet augue vitae urna mattis pulvinar ac eget tellus. Donec mattis cursus posuere. Donec dolor velit, aliquam eu pretium vel, accumsan eu est. Proin at ante nisi, ut malesuada diam.',
                        '',
                        'Nulla ac urna lacus, in luctus ipsum. Nunc luctus risus non dui tincidunt in ultricies tellus rhoncus. Aliquam pellentesque vehicula adipiscing. Phasellus porttitor sodales quam et cursus. Pellentesque iaculis consectetur lorem, et consectetur sem tristique sed. Duis vehicula eleifend nisl, a semper augue lobortis cursus. Fusce ut ligula diam. Quisque fringilla sodales massa vitae rhoncus. Pellentesque a libero sed enim lacinia commodo quis et dolor. Praesent facilisis, orci nec lacinia ultrices, enim nunc imperdiet augue, at aliquet dolor augue nec nulla.',
                        '',
                        'Ut venenatis commodo pulvinar. Vestibulum quam odio, malesuada posuere porttitor facilisis, molestie vel risus. Mauris auctor fringilla ornare. Morbi eu libero orci, nec semper lorem. Quisque blandit, ante nec pharetra bibendum, felis felis tincidunt felis, sed mattis sapien lorem et risus. In sed interdum quam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas vitae metus id purus ultrices fermentum. In dui eros, feugiat sit amet feugiat vel, accumsan vitae ligula. Nam at consectetur elit. Maecenas eget metus id nibh luctus mattis. Quisque mollis, sapien semper vulputate venenatis, purus arcu ornare orci, sit amet pellentesque enim ipsum non urna. Quisque at augue nec quam vehicula suscipit eu a dui. Morbi at molestie nunc. Mauris ultricies rutrum euismod. Sed non sem nisi. '
                    )
                )
            ),
            'iso-8859-1'
        ),
        '',
        '--'.$uid,
        'Content-Type: application/pdf;',
        '    name="file.pdf"',
        'Content-Transfer-Encoding: base64',
        'Content-Disposition: attachment;',
        '    filename="file.pdf"',
        '',
        chunk_split(base64_encode(file_get_contents('file.pdf'))),
        '--'.$uid.'--'
    )
);
mail($contacts, 'Subject', '', $header);
于 2012-12-17T11:38:31.283 回答