2

私の貧弱なPHPスキルで、私のWebサーバー上のフォルダーに画像をアップロードするスクリプトを作成することができました。アップロード後に画像が添付されたメールをスクリプトで送信するようになりました。

これは私のスクリプトです。おそらく私の問題は、ファイルが完全にアップロードされる前に電子メールが送信されることです。小さな画像で一度は機能しました..しかし、今は何も起こりません...(ファイルがアップロードされることを除いて...)

<?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"; 
};
?>
4

2 に答える 2

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]";
    }
于 2012-08-21T12:23:50.480 に答える
0

代わりに phpMailer のような任意のメーリング ライブラリを使用してください

http://phpmailer.worxware.com/

これにより、メール送信をより細かく制御できます。

于 2012-08-16T11:01:38.207 に答える