2012-12-06 2 views
2

아래는 sendgrid로 보내지는 헤더 및 원시 데이터입니다. 나에게 보내지는 원시 데이터를 추가했습니다. 첫 번째 원시 데이터가 성공적으로 전송된다는 점에 유의하십시오. 이 데이터를 sendgrid로 보내면 성공적으로 이메일을 보냅니다. 그것이하지 않는 것은 전자 메일을 숨기는 것입니다. 수신자는 to 섹션에서 다른 사용자의 이메일 주소를 볼 수 있습니다. 이것은 보안 상 위험합니다.SendGrid X-SMTPAPI가 내 헤더 요청 파일에서 sendgrid로 보내지는 중 무시됩니다.

POST /api/mail.send.json HTTP/1.1 
    Host: sendgrid.com 
    Connection: close 
    User-Agent: CakePHP 
    X-SMTPAPI: {"to":["[email protected]","[email protected]","[email protected]","[email protected]"]} 
    Content-Type: application/x-www-form-urlencoded 
    Content-Length: 1746 

    from=support%40workworktest.com&to%5B0%5D=admin%40hw.com&to%5B1%5D=Yaz02%40coldmail.com 
&to%5B2%5D=number25%40amail.com&to%5B3%5D=rieks%40gmail.com&subject=Todays+Newest+Open+Projects 
&html=%3C%21DOCTYPE+HTML+PUBLIC+%22-%2F%2FW3C%2F%2FDTD+HTML+4.0+Transitional%2F%2FEN%22%3E%0A%3Chtml%3E%0A%3Cbody%3E%0A%3Ca+href%3D%22http%3A%2F%2Fwww.hireremoteworkers.com%2F%22%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.hireremoteworkers.com%2Fimg%2Fheaderbg.jpg%22+width%3D%22750px%22+height%3D%22200px%22+alt%3D%22%22+%2F%3E%3C%2Fa%3E%3Cbr%2F%3E%0A%3Cstyle+type%3D%22text%2Fcss%22%3E%0A%09.newest-table+%7B%0A%09%09width%3A500px%3B%0A%09%7D%0A%3C%2Fstyle%3E%0A%3Ch1%3ENewest+Open+Work%3C%2Fh1%3E%0AHere+are+todays+newest+open+work.%3Cbr%2F%3E%0A%3Ctable+style%3D%22width%3A100%25%3B%22%3E%3Ctr+style%3D%22background%3A%233F8D35%3Bcolor%3Awhite%3B%22%3E%3Ctd%3EProject+Name%3C%2Ftd%3E%3Ctd%3EMaximum+Budget%3C%2Ftd%3E%3Ctd%3EProject+Created%3C%2Ftd%3E%3C%2Ftr%3E%3Ctr%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E%3Ca+href%3D%22http%3A%2F%2Fhireremoteworkers.com%2Fprojects%2Fview%2F50ba7257-bd20-4f19-a888-02d5323f2e01%22%3EGreat+New+Project%3C%2Fa%3E%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E%24500.00%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E2012-12-01+16%3A10%3A47%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3C%2Ftr%3E%3Ctr%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E%3Ca+href%3D%22http%3A%2F%2Fhireremoteworkers.com%2Fprojects%2Fview%2F50bf6573-d2ec-4107-a74c-29e8323f2e01%22%3EProject+64%3C%2Fa%3E%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E%24500.00%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E2012-12-05+10%3A17%3A07%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3C%2Ftr%3E%3C%2Ftable%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A&api_user=Y&api_key=Moment 

두 번째 시도

이 시도에서

, 나는 메시지 내에서 to 매개 변수를 제거하고 난 헤더에 추가하고 결과는 다음과 같은 것을 보인다. 이것은 전혀 작동하지 않으며 예외를 throw합니다. 예외를 잡아 게시하려고합니다.

POST /api/mail.send.json HTTP/1.1 
Host: sendgrid.com 
Connection: close 
User-Agent: CakePHP 
To: [email protected] 
X-SMTPAPI: {"to":["[email protected]","[email protected]","[email protected]","[email protected]"]} 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 1620 

from=support%40workworktest.com&subject=Todays+Newest+Open+Projects&html=%3C%21DOCTYPE+HTML            +PUBLIC+%22-%2F%2FW3C%2F%2FDTD+HTML+4.0+Transitional%2F%2FEN%22%3E%0A%3Chtml%3E%0A%3Cbody%3E%0A%            3Ca+href%3D%22http%3A%2F%2Fwww.hireremoteworkers.com%2F%22%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.hi            reremoteworkers.com%2Fimg%2Fheaderbg.jpg%22+width%3D%22750px%22+height%3D%22200px%22+alt%3D%22%2            2+%2F%3E%3C%2Fa%3E%3Cbr%2F%3E%0A%3Cstyle+type%3D%22text%2Fcss%22%3E%0A%09.newest-table+%7B%0A%09            %09width%3A500px%3B%0A%09%7D%0A%3C%2Fstyle%3E%0A%3Ch1%3ENewest+Open+Work%3C%2Fh1%3E%0AHere+are+t            odays+newest+open+work.%3Cbr%2F%3E%0A%3Ctable+style%3D%22width%3A100%25%3B%22%3E%3Ctr+style%3D%2            2background%3A%233F8D35%3Bcolor%3Awhite%3B%22%3E%3Ctd%3EProject+Name%3C%2Ftd%3E%3Ctd%3EMaximum+B            udget%3C%2Ftd%3E%3Ctd%3EProject+Created%3C%2Ftd%3E%3C%2Ftr%3E%3Ctr%3E%0A%09%09%09%09%09%09%09%09            %3Ctd%3E%3Ca+href%3D%22http%3A%2F%2Fhireremoteworkers.com%2Fprojects%2Fview%2F50ba7257-bd20-4f19            -a888-02d5323f2e01%22%3EGreat+New+Project%3C%2Fa%3E%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3            E%24500.00%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E2012-12-01+16%3A10%3A47%3C%2Ftd%3E%0A%09            %09%09%09%09%09%09%09%3C%2Ftr%3E%3Ctr%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E%3Ca+href%3D%22http%3            A%2F%2Fhireremoteworkers.com%2Fprojects%2Fview%2F50bf6573-d2ec-4107-a74c-29e8323f2e01%22%3EProje            ct+64%3C%2Fa%3E%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3Ctd%3E%24500.00%3C%2Ftd%3E%0A%09%09%09%09            %09%09%09%09%3Ctd%3E2012-12-05+10%3A17%3A07%3C%2Ftd%3E%0A%09%09%09%09%09%09%09%09%3C%2Ftr%3E%3C%            2Ftable%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A&api_user=Y&api_key=Moment 

업데이트 된 코드 아래

내 제공된 코드 ... 노트, 코드는 순서대로 코드가 제공 프로세스를 제공한다.

나는받는 사람의 이메일이 설정되고있는 _sendgrid() 방법 내에서 To 매개 변수를 변경, (이 크론 작업에) 다음 코드에서

$controller =& new Controller(); 
    $email =& new SendgridComponent(null); 
    $email->initialize($controller); 
    $email->from = "[email protected]"; 
    $email->template = "newest_open"; 
    $email->sendAs = 'html'; 
    $email -> delivery = 'sendgrid'; 
    $controller->set('tableData',$tableData); 
    $userEmails = array(); 
    $smtpapi = array(); 
    foreach($users as $user) { 
     $smtpapi[] = $user['User']['email']; 
    }   
    $email->to  = '[email protected]'; 
    $email->smtpapi = $smtpapi; 
    $email->subject = 'Todays Newest Open Projects'; 
    $return = $email->send(); 

sendgrid 구성 요소

코드를 구현했습니다. 이 매개 변수는 이메일 일 수 있으며 sendgrid는이를 무시하고 제공된 경우 X-SMTPAPI을 찾습니다. 대신 X-SMTPAPI 매개 변수가 무시됩니다. http://www.opensource.org/licenses/mit-license.php MIT 라이선스 */ App :: import ('Component', 'Email'); 당신은 X-SMTPAPI 헤더뿐만 아니라 SMTP 메시지의 수신자를 지정하는 것 같습니다 원시 데이터에서

/** 
* SendgridComponent 
* 
* This component is used for sending email messages 
* using the Sendgrid API http://sendgridapp.com/ 
* 
*/ 
class SendgridComponent extends EmailComponent { 

/** 
* Sendgrid API URI 
* 
* @var string 
* @access public 
*/ 
    var $uri = 'https://sendgrid.com/'; 

/** 
* Sendgrid API Request URI 
* 
* @var string 
* @access public 
*/ 
    var $request = 'api/mail.send.json'; 

/** 
* Sendgrid API Username 
* 
* @var string 
* @access public 
*/ 
    var $username = null; 

/** 
* Sendgrid API Password 
* 
* @var string 
* @access public 
*/ 
    var $password = null; 

/** 
* Sendgrid SMTPAPI 
* for sending information to users without displaying their emails 
* @var string 
* @access public 
*/ 
    var $smtpapi = null; 

/** 
* Sendgrid Tag property 
* 
* @var string 
* @access public 
*/ 
    var $tag = null; 

/** 
* Sets Http Header Information after submission 
* 
* @var string 
* @access public 
*/ 
    var $raw = null; 

/** 
* Variable that holds Sendgrid connection 
* 
* @var resource 
* @access private 
*/ 
    var $__sendgridConnection = null; 

/** 
* Initialize component 
* 
* @param object $controller Instantiating controller 
* @access public 
*/ 
    function initialize(&$controller, $settings = array()) { 
     parent::initialize($controller, $settings); 
     if (Configure::read('Sendgrid.username') !== null) { 
      $this->username = Configure::read('Sendgrid.username'); 
     } 
     if (Configure::read('Sendgrid.password') !== null) { 
      $this->password = Configure::read('Sendgrid.password'); 
     } 
    } 

/** 
* Sends out email via Sendgrid 
* 
* @return bool Success 
* @access private 
*/ 
    function _sendgrid() { 
     App::import('Core', 'HttpSocket'); 

     // Setup connection 
     $this->__sendgridConnection =& new HttpSocket(); 

     // Construct message 
     $message = array(); 

     // From 
     $message['from'] = $this->_formatAddress($this->from); 
     if (!empty($this->fromname)) { 
      $message['fromname'] = $this->fromname; 
     } 

     // To 
     if (is_array($this->to)) { 
      $message['to'] = array_map(array($this, '_formatAddress'), $this->to); 
     } else { 
        //I altered this code to assign the 'to' value to the request header 
        //instead of adding it to the message etc $message['to'] 
        // OLD LINE: $message['to'] = $this->_formatAddress($this->to); 
      $this->__sendgridConnection->request['header']['To'] = $this->_formatAddress($this->to);//NEW LINE 
      if (!empty($this->toname)) { 
       $message['toname'] = $this->toname; 
      } 
     } 
     // SMTPAPI : This was added by me to include X-SMTPAPI in the message header 
     if(is_array($this->smtpapi) && $this->smtpapi) { 
      $this->__sendgridConnection->request['header']['X-SMTPAPI'] = '{"to":['; 
      foreach($this->smtpapi as $smtpapi) { 
       $this->__sendgridConnection->request['header']['X-SMTPAPI'] .= '"'.$smtpapi.'",'; 
      } 
      $this->__sendgridConnection->request['header']['X-SMTPAPI'] = substr_replace($this->__sendgridConnection->request['header']['X-SMTPAPI'] ,"",-1); 
      $this->__sendgridConnection->request['header']['X-SMTPAPI'] .= ']}'; 
     } 
     // Cc 
     if (!empty($this->cc)) { 
      if (is_array($this->cc)) { 
       $message['cc'] = array_map(array($this, '_formatAddress'), $this->cc); 
      } else { 
       $message['cc'] = $this->_formatAddress($this->cc); 
      } 
     } 

     // Bcc 
     if (!empty($this->bcc)) { 
      if (is_array($this->bcc)) { 
       $message['bcc'] = array_map(array($this, '_formatAddress'), $this->bcc); 
      } else { 
       $message['bcc'] = $this->_formatAddress($this->bcc); 
      } 
     } 

     // Subject 
     $message['subject'] = $this->subject; 

     // Tag 
     if (!empty($this->tag)) { 
      $message['Tag'] = $this->tag; 
     } 

     // HtmlBody 
     if ($this->sendAs === 'html' || $this->sendAs === 'both') { 
      $message['html'] = $this->htmlMessage; 
     } 

     // TextBody 
     if ($this->sendAs === 'text' || $this->sendAs === 'both') { 
      $message['text'] = strip_tags($this->textMessage); 
     } 

     // ReplyTo 
     if (!empty($this->replyTo)) { 
      $message['replyto'] = $this->_formatAddress($this->replyTo); 
     } 

     // Setup header 
     $message['api_user'] = $this->username; 
     $message['api_key'] = $this->password; 


     // Send message 
     $jsonInfo = json_decode($this->__sendgridConnection->post('https://sendgrid.com/api/mail.send.json', $message), true); 
     $this->raw = $this->__sendgridConnection->request['raw']; 
     debug($this->__sendgridConnection->request['header']); 
     return $jsonInfo; 
    } 
} 
?> 

답변

1

. X-SMTPAPI 헤더에만 지정하려고합니다. SMTP 메시지를 생성하는 코드를 게시하면 문제가있는 위치를 식별하는 데 도움이됩니다.

+0

안녕하세요. 브랜드, 고맙습니다. 나는 그것을 시도한 후에 코드를 제공 할 것이다. sendgrid에 따르면 수신자에게 보내는 두 가지 방법이 있습니다. 하나는 메시지에 있고, 다른 하나는 헤더에 'To'를 사용하고 있습니다. 이 일을하기 위해서 당신의 말. To '는 헤더 내에 지정해야합니다. – numerical25

+0

두 번째 시도는 더 가깝습니다. 당신이 지금 얻고있는 예외를 게시하면 많은 도움이 될 것입니다. X-SMTPAPI 헤더 행이 너무 길어서 랩핑해야 할 수도 있습니다. 일반적인 PHP SMTP API 헤더 코드를 살펴보고 RFC가 준수하도록 줄 바꿈하는 방법에 대한 예제를 확인하십시오. http://sendgrid.com/docs/Code%20Examples/SMTP%20API%20Header%20Examples/php. HTML – bwest