2016-03-19 7 views
0

php mail()コードをデバッグしようとしています。私は以下のPHPブロック全体を含んでいますので、関連しない書式設定やセクションを許してください(rCaptcha、フィールドバリデーションなど)php mail()関数の添付と本体が両方とも空です

私が抱えている問題は、メール本文はありません... 2)添付ファイルは空です。

この行をコメントアウトすると、 "$ body。= $ my_attachment;"私は体内に期待されるテキストを受け取り、驚くことには添付ファイルはありません。

誰でも私の間違いなくルーキーミスを見つけることができますか?私はいくつかの回答を期待しています。私は今調べているPHPのメールライブラリを使用していますが、私の理解と教育のために私の間違いを指摘してくれる特定のフィードバックを感謝します。

ありがとうございました。

<?php 
/******************************************** 
/Start processing the email 
/*******************************************/ 
# We'll make a list of error messages in an array 
$messages = array(); 
$upload_folder = "uploads/"; 
// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = PHP_EOL; 

// Change this to YOUR address 
$recipient = [email protected]>COM; 
$email = $_POST['myemail']; 
$phone = $_POST['phone']; 
$realName = $_POST['name']; 
$subject = "WEB CONTACT: Careers" ; 

$body = "--" . $separator . $eol; 
$body .= "FROM: " . $realName . 
     "\r\nPHONE: " . $phone . 
     "\r\nEMAIL: " . $email . 
     "\r\nCONTACT ME VIA: " . $_POST['contact_me'] . 
     "\r\nMESSAGE:" . $_POST['mymessage'] ; 

/******************************************** 
/ATTACHMENT 
/*******************************************/ 
//Get the uploaded file information 
$name_of_uploaded_file = 
    basename($_FILES['uploaded_file']['name']); 

//get the file extension of the file 
$type_of_uploaded_file = 
    substr($name_of_uploaded_file, 
    strrpos($name_of_uploaded_file, '.') + 1); 

$size_of_uploaded_file = 
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs 

//Settings 
$max_allowed_file_size = 1024; // size in KB 
$allowed_extensions = array("pdf", "doc", "txt"); 

//Validations 
if($size_of_uploaded_file > $max_allowed_file_size) 
{ 
    $messages[] = "Size of file should be less than " . $max_allowed_file_size; 
} 

//------ Validate the file extension ----- 
$allowed_ext = false; 
for($i=0; $i<sizeof($allowed_extensions); $i++) 
{ 
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) 
    { 
    $allowed_ext = true; 
    } 
} 

if(!$allowed_ext) 
{ 
    $messages[] = "The uploaded file is not supported file type. ". 
    " Only the following file types are supported: ".implode(',',$allowed_extensions); 
} 

//copy the temp. uploaded file to uploads folder 
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; 
$tmp_path = $_FILES["uploaded_file"]["tmp_name"]; 

if(is_uploaded_file($tmp_path)) 
{ 
    if(!copy($tmp_path,$path_of_uploaded_file)) 
    { 
    $messages[] = 'Error while copying the uploaded file'; 
    } 
} 

// attachment 

    $file = $upload_folder . "/" . $name_of_uploaded_file; 
    $file_size = filesize($file); 
    $handle = fopen($file, "r"); 
    $content = fread($handle, $file_size); 
    fclose($handle); 
    $content = chunk_split(base64_encode($content)); 



/*********************************************************** 
// reCAPTCHA your recaptcha secret key 
***********************************************************/ 
$secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 

if(isset($_POST['email'])) 
{ 
    $email=$_POST['email']; 
} 
if(isset($_POST['comment'])) 
{ 
    $email=$_POST['comment']; 
} 
if(isset($_POST['g-recaptcha-response'])) 
{ 
    $captcha=$_POST['g-recaptcha-response']; 
} 
     $ip = $_SERVER['REMOTE_ADDR']; 
     $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); 
     $responseKeys = json_decode($response,true); 
     if(intval($responseKeys["success"]) !== 1) { 
      $messages[] = "The reCaptcha Question was not answered correctly. I am begining to suspect that you are a robot."; 
     } 
/*********************************************************** 
// END reCAPTCHA 
***********************************************************/   

/*********************************************************** 
// Message format validations 
***********************************************************/ 
# Allow only reasonable email addresses 
if (!preg_match("/^[\w\+\-.~]+\@[\-\w\.\!]+$/", $email)) { 
$messages[] = "That is not a valid email address."; 
} 
# Allow only reasonable real phone numbers 
if (!preg_match("/^[\+0-9\-\(\)\s]*$/", $phone)) { 
$messages[] = "The phone number must only include numbers, spaces, brackets(), and '+'."; 
} 
# Allow only reasonable real names 
if (!preg_match("/^[\w\ \+\-\'\"]+$/", $realName)) { 
$messages[] = "The real name field must contain only " . 
"alphabetical characters, numbers, spaces, and " . 
"reasonable punctuation. We apologize for any inconvenience."; 
} 
# CAREFUL: don't allow hackers to sneak line breaks and additional 
# headers into the message and trick us into spamming for them! 
$subject = preg_replace('/\s+/', ' ', $subject); 
# Make sure the subject isn't blank afterwards! 
if (preg_match('/^\s*$/', $subject)) { 
$messages[] = "Please choose area and office for your message."; 
} 

# Make sure the message has a body 
if (preg_match('/^\s*$/', $body)) { 
$messages[] = "Your message was blank. Did you mean to say " . 
"something?"; 
} 
    if (count($messages)) { 
    # There were problems, so tell the user and 
    # don't send the message yet 
    foreach ($messages as $message) { 
     echo("<p>$message</p>\n"); 
    } 
    echo("<p>Click the back button and correct the problems. " . 
     "Then click Send Your Message again.</p>"); 
    } 
    //else 
    { 
    # Send the email - we're done 


    // main header (multipart mandatory) 
    $headers = "From: " . $realName . $eol; 
    $headers .= "MIME-Version: 1.0" . $eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol; 
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol; 

    // message 
    $headers .= "--" . $separator . $eol; 
    $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol; 
    $headers .= "Content-Transfer-Encoding: 8bit" . $eol; 
    $headers .= $body . $eol; 

    // attachment 
    $my_attachment =""; 
    $my_attachment .= "--" . $separator . $eol; 
    $my_attachment .= "Content-Type: application/octet-stream; name=\"" . $name_of_uploaded_file . "\"" . $eol; 
    $my_attachment .= "Content-Disposition: attachment; filename=\"" . $name_of_uploaded_file . "\"" . $eol; 
    $my_attachment .= "Content-Transfer-Encoding: base64" . $eol; 
    $my_attachment .= "Content-Disposition: attachment" . $eol; 
    $my_attachment .= $content . $eol; 
    $my_attachment .= "--" . $separator . "--"; 


    $body .= $my_attachment ; 



mail($recipient, 
     $subject, 
     $body, 
     $headers 
    ); 
    echo("<p>Your message has been sent. Thank you!</p>\n"); 
    } 
?> 
+0

PhpMailerを使用して添付ファイルを送信することをお勧めします。在庫のmail()機能よりも簡単です。 https://github.com/PHPMailer/PHPMailer –

答えて

0

メールを送信するためにPHPmailerクラスを使用できます。メールを送るのはとても簡単です。ここからphpmailerのスクリプトをダウンロード

  • http://github.com/PHPMailer/PHPMailer
  • は、アーカイブを解凍し、プロジェクト内の便利な場所にスクリプトのフォルダをコピーphpmailerのを使用するには

  • メインスクリプトファイルをインクルード - require_once('path/to/file/class.phpmailer.php');

を、添付ファイル付きのメールを送ることは信じられないほど簡単にめちゃくちゃ困難であるから行く:

$email = new PHPMailer(); 
$email->From  = '[email protected]'; 

$email->FromName = 'Your Name'; 

$email->Subject = 'Message Subject'; 
$email->Body  = $bodytext; 
$email->AddAddress('[email protected]'); 

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE'; 

$email->AddAttachment($file_to_attach , 'NameOfFile.pdf'); 

return $email->Send(); 

それは(ちょうどその一行$メール:> AddAttachmentです);添付ファイルを追加します。

+0

"私は現在調査中のPHPメールライブラリを使用することを提案していると回答していますが、私の理解と教育のために私の間違いを指摘してくれた特定のフィードバックに感謝します。 – lmorse

関連する問題