2017-01-31 9 views
1

Amazon AWSでPHPを使用してメールを送信する必要がありますが、単純なメールを送信できますがエラーが発生しました。毎回エラーが発生します。Amazon AWSとPHPを使用して添付ファイル付きメールを送信する際にエラーが発生する

Fatal error: Cannot redeclare Aws\constantly() (previously declared in /path/vendor/aws/aws-sdk-php/src/functions.php:20) in phar:///opt/lampp/htdocs/path/amazon/aws.phar/Aws/functions.php on line 22

私が使用したコード: -

require_once '/mypath/vendor/autoload.php'; 
include_once("SESUtils.php"); 

$subject_str = "Some Subject"; 
$body_str = "<strong>Some email body</strong>"; 
$attachment_str = file_get_contents("mypdf_file.pdf"); 

//send the email 
$params = 
    array("to" => "[email protected]", 
    "subject" => "Some subject", 
    "message" => "<strong>Some email body</strong>", 
    "from" => "[email protected]", 
    "replyTo" => "[email protected]", 
    "files" => 
    array(
    "1" => array(
    "name" => "filename1", 
    "filepath" => "/path/to/mypdf_file.pdf", 
    "mime" => "application/pdf" 
), 
) 
); 

$res = SESUtils::sendMail($params); 

AWSのバージョン: - 3.21.6 とSESUtils.php

require_once('aws.phar'); 
use Aws\Ses\SesClient; 
class SESUtils { 
    const version = "1.0"; 
    const AWS_KEY = "AWS_KEY"; 
    const AWS_SEC = "AWS_SEC"; 
    const AWS_REGION = "us-east-1"; 
    const MAX_ATTACHMENT_NAME_LEN = 60; 

    public static function sendMail($params) { 
     $to = self::getParam($params, 'to', true); 
     $subject = self::getParam($params, 'subject', true); 
     $body = self::getParam($params, 'message', true); 
     $from = self::getParam($params, 'from', true); 
     $replyTo = self::getParam($params, 'replyTo'); 
     $files = self::getParam($params, 'files'); 

     $res = new ResultHelper(); 

     // get the client ready 
     $client = SesClient::factory(array(
        'key' => self::AWS_KEY, 
        'secret' => self::AWS_SEC, 
        'region' => self::AWS_REGION 
     )); 

     // build the message 
     if (is_array($to)) { 
      $to_str = rtrim(implode(',', $to), ','); 
     } else { 
      $to_str = $to; 
     } 

     $msg = "To: $to_str\n"; 
     $msg .= "From: $from\n"; 

     if ($replyTo) { 
      $msg .= "Reply-To: $replyTo\n"; 
     } 

     // in case you have funny characters in the subject 
     $subject = mb_encode_mimeheader($subject, 'UTF-8'); 
     $msg .= "Subject: $subject\n"; 
     $msg .= "MIME-Version: 1.0\n"; 
     $msg .= "Content-Type: multipart/mixed;\n"; 
     $boundary = uniqid("_Part_".time(), true); //random unique string 
     $boundary2 = uniqid("_Part2_".time(), true); //random unique string 
     $msg .= " boundary=\"$boundary\"\n"; 
     $msg .= "\n"; 

     $msg .= "--$boundary\n"; 
     $msg .= "Content-Type: multipart/alternative;\n"; 
     $msg .= " boundary=\"$boundary2\"\n"; 
     $msg .= "\n"; 
     $msg .= "--$boundary2\n"; 

     $msg .= "Content-Type: text/plain; charset=utf-8\n"; 
     $msg .= "Content-Transfer-Encoding: 7bit\n"; 
     $msg .= "\n"; 
     $msg .= strip_tags($body); //remove any HTML tags 
     $msg .= "\n"; 

     // now, the html text 
     $msg .= "--$boundary2\n"; 
     $msg .= "Content-Type: text/html; charset=utf-8\n"; 
     $msg .= "Content-Transfer-Encoding: 7bit\n"; 
     $msg .= "\n"; 
     $msg .= $body; 
     $msg .= "\n"; 
     $msg .= "--$boundary2--\n"; 

     // add attachments 
     if (is_array($files)) { 
      $count = count($files); 
      foreach ($files as $file) { 
       $msg .= "\n"; 
       $msg .= "--$boundary\n"; 
       $msg .= "Content-Transfer-Encoding: base64\n"; 
       $clean_filename = self::clean_filename($file["name"], self::MAX_ATTACHMENT_NAME_LEN); 
       $msg .= "Content-Type: {$file['mime']}; name=$clean_filename;\n"; 
       $msg .= "Content-Disposition: attachment; filename=$clean_filename;\n"; 
       $msg .= "\n"; 
       $msg .= base64_encode(file_get_contents($file['filepath'])); 
       $msg .= "\n--$boundary"; 
      } 
      // close email 
      $msg .= "--\n"; 
     } 

     // now send the email out 
     try { 
      $ses_result = $client->sendRawEmail(
        array(
       'RawMessage' => array(
        'Data' => base64_encode($msg) 
       ) 
        ), array(
       'Source' => $from, 
       'Destinations' => $to_str 
        ) 
      ); 
      if ($ses_result) { 
       $res->message_id = $ses_result->get('MessageId'); 
      } else { 
       $res->success = false; 
       $res->result_text = "Amazon SES did not return a MessageId"; 
      } 
     } catch (Exception $e) { 
      $res->success = false; 
      $res->result_text = $e->getMessage(). 
        " - To: $to_str, Sender: $from, Subject: $subject"; 
     } 
     return $res; 
    } 

    private static function getParam($params, $param, $required = false) { 
     $value = isset($params[$param]) ? $params[$param] : null; 
     if ($required && empty($value)) { 
      throw new Exception('"'.$param.'" parameter is required.'); 
     } else { 
      return $value; 
     } 
    } 

    /** Clean filename function - to be mail friendly **/ 
    public static function clean_filename($str, $limit = 0, $replace=array(), $delimiter='-') { 
     if(!empty($replace)) { 
      $str = str_replace((array)$replace, ' ', $str); 
     } 

     $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); 
     $clean = preg_replace("/[^a-zA-Z0-9\.\/_| -]/", '', $clean); 
     $clean = preg_replace("/[\/| -]+/", '-', $clean); 

     if ($limit > 0) { 
      //don't truncate file extension 
      $arr = explode(".", $clean); 
      $size = count($arr); 
      $base = ""; 
      $ext = ""; 
      if ($size > 0) { 
       for ($i = 0; $i < $size; $i++) { 
        if ($i < $size - 1) { //if it's not the last item, add to $bn 
         $base .= $arr[$i]; 
         //if next one isn't last, add a dot 
         if ($i < $size - 2) 
          $base .= "."; 
        } else { 
         if ($i > 0) 
          $ext = "."; 
         $ext .= $arr[$i]; 
        } 
       } 
      } 
      $bn_size = mb_strlen($base); 
      $ex_size = mb_strlen($ext); 
      $bn_new = mb_substr($base, 0, $limit - $ex_size); 
      // doing again in case extension is long 
      $clean = mb_substr($bn_new.$ext, 0, $limit); 
     } 
     return $clean; 
    } 

} 
class ResultHelper { 
    public $success = true; 
    public $result_text = ""; 
    public $message_id = ""; 
} 
+0

「aws.phar」のどのバージョンを使用していますか?また、 'SESUtils'で' sendMail'のコードを共有すると便利です。 –

+0

awsバージョン: - 3.21.6、上記のSESUtils.phpをご覧ください – Gourav

答えて

1

私はconstantly()を再宣言については、このエラーを持っていた問題が解決されました私たちのコードでは、単に変更することによって:

include('/PATH/TO/aws-sdk-3/aws-autoloader.php'); 
このエラーメッセージをGoogleにあなたや次の人を助けるかもしれない

include_once('/PATH/TO/aws-sdk-3/aws-autoloader.php'); 

から

0

これは名前空間にすぎません。例を参照してください。名前空間クラスを使用するか、絶対に参照する必要があります。

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

//Load composer's autoloader 
require 'vendor/autoload.php'; 
関連する問題