简体   繁体   中英

SendGrid X-SMTPAPI is being ignored in my header request file being sent out to sendgrid

below is the header and raw data being sent out to sendgrid. I've added the raw data being sent out. Please note that the first raw data is being sent successfully. When I send this data to sendgrid, it successfully sends the emails out. what it DOES NOT do is hide the emails. so the recipient can see other people's email address in the to section. which is a security risk.

    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

Second Attempt

In this attempt, I removed the to parameters from within the message and I added it to the header and the result looks something like below. Please note, this does not work at all and throws a exception. I am going to try to catch the exception and post it.

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

The updated code

Below is my provided code... note, the code provide processes the code in the order provided.

implemented code (This is in cron job)

    $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 component

In the following code, I altered the To parameter within the _sendgrid() method where the recipients email is being set. I was told that this parameter could be any email and sendgrid would ignore it and look for the X-SMTPAPI instead if it was provided. but instead the X-SMTPAPI param is being ignored. http://www.opensource.org/licenses/mit-license.php The MIT License */ 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;
    }
}
?>

From the raw data it looks like you are specifying the recipients in the SMTP message as well as in the X-SMTPAPI header. You want to specify them only in the X-SMTPAPI header. If you post the code that is generating the SMTP message I can help identify where the problem might be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM