2016-05-24 8 views
0

基本的には、XAMPPを使用する前に他のPHPコードをテストしましたが、XAMPPを使用して自分のGmailアカウントに電子メールを送信するこのコードをテストしたいと思います。電子メールが送信されるかどうかを確認するためのテストにはどのような手順が必要ですか。 XAMPPを使ってPHPメーラーコードをテストするには?

私のPHPコード:

<?php 
header('Content-type: application/json'); 
$status = array(
    'type'=>'success', 
    'message'=>'Thank you for contact us. As early as possible we will contact you ' 
); 

//Added to deal with Files 
require_once('./PHPMailer/class.phpmailer.php') 
    //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 = 10240; // size in KB 
    $allowed_extensions = array("jpg", "jpeg", "gif", "bmp","png"); 

    //Validations 
    if($size_of_uploaded_file > $max_allowed_file_size) 
    { 
     $errors .= "\n Size of file should be less than $max_allowed_file_size (~10MB). The file you attempted to upload is too large. To reduce the size, open the file in an image editor and change the Image Size and resave the file."; 
    } 

    //------ 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) 
    { 
     $errors .= "\n 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 - make sure the folder exists on the server and has 777 as its permission 
    $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)) 
     { 
     $errors .= '\n error while copying the uploaded file'; 
     } 
    } 

//------------------------ 
$name = @trim(stripslashes($_POST['name'])); 
$clientemail = @trim(stripslashes($_POST['email'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $clientemail . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message; 

$email = new PHPMailer(); 
$email->From  = $clientemail; 
$email->FromName = $name; 
$email->Subject = $subject; 
$email->Body  = $body; 
$email->AddAddress('[email protected]'); //Send to this email 

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE'; 

$email->AddAttachment($path_of_uploaded_file , $name_of_uploaded_file); 

return $email->Send(); 

echo json_encode($status); 
die; 

アップデート:私はXAMPPローカルサーバーと話をするfakeSMTPサーバーを取得する方法がわからないFakeSMTPが、私をダウンロードし

...

リンクFakeSMTPへ:http://nilhcem.github.io/FakeSMTP/project-reports.html

答えて

0

PHPメーラーでメールを送信するにはローカルサーバーの次の機能を有効にするコード

  1. PHP_OPENSSL
  2. PHP_SMTP
  3. PHP_SOCKETS

してから、このコード

$filename="Important-Notice.docx"; // file name if you want to send attachment 
$message=" <h2>Your Testing Mesage</h2>";// You Can Use HTML code inside  
include "Classes/class.phpmailer.php"; // include the class name 
$mail = new PHPMailer(); // create a new object 
$mail->IsSMTP(); // enable SMTP 
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only 
$mail->SMTPAuth = true; // authentication enabled 
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 465; // or 587 
$mail->IsHTML(true); 
$mail->Username = "[email protected]"; 
$mail->Password = "Your_password";//Your password Key 
$mail->SetFrom("[email protected]"); 
$mail->Subject = "Your Gmail SMTP Mail"; 
$mail->Body = $message; 
$mail->AddAddress($email); 
$mail->AddBCC("[email protected]"); 
$fileatt= "notes/".$filename;//Your Attachment Location 
$mail->AddAttachment($fileatt); 


if(!$mail->Send()){ 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else{ 
    //echo "Message has been sent"; 
} 
を使用
関連する問題