繁体   English   中英

将文件上传到服务器,然后附加到电子邮件并发送一个php文件

[英]Upload file to server, then attach to email and send in one php file

我已经用我较差的php技巧成功制作了一个脚本,该脚本将图像上传到我的网络服务器上的文件夹中。 现在,我希望脚本在上载后发送带有附件图像的电子邮件。

这是我的剧本。 Maby我的问题是在文件完全上传之前已发送电子邮件。 它曾经用一张小图像工作过一次。但是现在什么也没发生...(例如文件已上传...)

<?php
print_r($_FILES);
$new_image_name = "image".rand(10, 20).".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/home/virtual/domain.com/public_html/upload/".$new_image_name);

//define the receiver of the email 
$to = 'sender@domain.com'; 
//define the subject of the email 
$subject = 'Upload: '.$_GET["album"]; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: reciver@domain.com\r\nReply-To: reciver@domain.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('upload/'.$new_image_name))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/jpg; name="attachment.jpg"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>
ge is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
};
?>

使用任何邮件库代替phpMailer

http://phpmailer.worxware.com/

这将使您更好地控制电子邮件发送。

在Waygood的一点逻辑帮助下。 我发现附加和发送文件后可以移动文件。 我发现此脚本对我有用http : //netsperience.org/content/blog/attach-a-file-email-with-php-code-actually-works

经过我的修改,它现在就像一个魅力!

print_r($_FILES);

$new_image_name = "image".rand(10, 20).".jpg";
$email_to = "reciver@domain.com"; // The email you are sending to (example)
$email_from = "sender@domain.com"; // The email you are sending from
$email_subject = "Upload: ".$_GET["album"]; // The Subject of the email
$email_txt = "the body text"; // Message that the email has in it
$fileatt = $_FILES["file"]["tmp_name"]; // Path to the tmp file
$fileatt_type = "image/jpeg"; // File Type
$fileatt_name = "upload.jpg"; // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: $email_from"; // Who the email is from (example)
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to,$email_subject,$email_message,$headers);

if (mail($email_to,$email_subject,$email_message,$headers)) {

       move_uploaded_file($_FILES["file"]["tmp_name"], "/home/virtual/domain.com/public_html/upload/".$new_image_name);

    } else {
       echo "[B]email could not be sent[/B]";
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM