我设法制作了一个脚本,用我糟糕的PHP技能,将图像上传到我的网络服务器上的文件夹。我现在希望脚本在上传后发送附有图像的电子邮件。
这是我的剧本。 Maby我的问题是在完全上传文件之前发送电子邮件。它用一个小图像工作了一次..但现在没有任何反应......(exept文件上传...)
<?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";
};
?>
答案 0 :(得分:0)
答案 1 :(得分:0)
来自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]";
}