2

下面是发送到 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

第二次尝试

在这次尝试中,我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();

发送网格组件

在以下代码中,我更改了设置收件人电子邮件的方法中的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;
    }
}
?>
4

1 回答 1

1

从原始数据看来,您正在指定 SMTP 消息以及 X-SMTPAPI 标头中的收件人。您只想在 X-SMTPAPI 标头中指定它们。如果您发布生成 SMTP 消息的代码,我可以帮助确定问题可能出在哪里。

于 2012-12-06T22:06:06.400 回答