以下は、sendgrid に送信されるヘッダーと生データです。送信される生データを追加しました。最初の生データが正常に送信されていることに注意してください。このデータを sendgrid に送信すると、メールが正常に送信されます。それがしないことは、電子メールを非表示にすることです。そのため、受信者はセクションで他の人のメール アドレスを見ることができますto
。これはセキュリティ上のリスクです。
POST /api/mail.send.json HTTP/1.1
Host: sendgrid.com
Connection: close
User-Agent: CakePHP
X-SMTPAPI: {"to":["add@hw.com","Yaz4302@coldmail.com","number25@gmail.com","rieks@amail.com"]}
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
2 回目の試行
この試みではto
、メッセージ内からパラメーターを削除し、それをヘッダーに追加しました。結果は次のようになります。これはまったく機能せず、例外がスローされることに注意してください。例外をキャッチして投稿しようとします。
POST /api/mail.send.json HTTP/1.1
Host: sendgrid.com
Connection: close
User-Agent: CakePHP
To: number@amail.com
X-SMTPAPI: {"to":["add@hw.com","Yaz4302@coldmail.com","number25@gmail.com","rieks@amail.com"]}
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
更新されたコード
以下は私が提供したコードです...注、コード提供は提供された順序でコードを処理します。
実装されたコード (これは cron ジョブにあります)
$controller =& new Controller();
$email =& new SendgridComponent(null);
$email->initialize($controller);
$email->from = "support@workworktest.com";
$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 = 'number@amail.com';
$email->smtpapi = $smtpapi;
$email->subject = 'Todays Newest Open Projects';
$return = $email->send();
sendgrid コンポーネント
次のコードでは、受信者の電子メールが設定されているメソッドTo
内のパラメーターを変更しました。_sendgrid()
このパラメーターは任意の電子メールである可能性があり、sendgrid はそれを無視し、指定されているX-SMTPAPI
場合は代わりに検索すると言われました。代わりに、X-SMTPAPI
パラメーターは無視されています。http://www.opensource.org/licenses/mit-license.php MIT ライセンス */ App::import('Component', 'Email');
/**
* 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;
}
}
?>