sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 623 forks source link

Bulk mail with smtpapi #304

Closed janith closed 7 years ago

janith commented 8 years ago

Issue Summary

I was trying to implement to bulk email with your smtpapi without getting a proper result. I have tested my code with 2*2(2 batches with have 2 emails each) and surprisingly I received twice the same email for first batch and non for the second.

Steps to Reproduce

Please refer the x-smtpapi headers that I used for testing.

batch 1 :

{ "to": [ "franc@first-class-and-more.de", "jan.thch@gmail.com" ], "sub": { "-salutation-": [ "Liebe Frau Franc YYY,", "Lieber Herr xxx Steffens," ], "-site_url-": [ "http://first-class-and-more.local/?mid=1082_c318d46b2be462566e7c2db8e245522d&data=%7B%22title_id%22%3A%222%22%2C%22name%22%3A%22Franc%22%2C%22surname%22%3A%22YYY%22%2C%22email%22%3A%22franc%40first-class-and-more.de%22%7D", "http://first-class-and-more.local/?mid=1082_9ee0f700d6fbcee8220102f04133bb55&data=%7B%22title_id%22%3A%221%22%2C%22name%22%3A%22Daniel%22%2C%22surname%22%3A%22Steffens%22%2C%22email%22%3A%22jan.thch%40gmail.com%22%7D" ], "-unsubscribe_url-": [ "http://first-class-and-more.local/?mailingAction[action]=unsubscribe&mailingAction[email]=franc%40first-class-and-more.de", "http://first-class-and-more.local/?mailingAction[action]=unsubscribe&mailingAction[email]=jan.thch%40gmail.com" ] }, "category": [ "MailID-1082" ] }

batch 2 :

{ "to": [ "jani.win@hotmail.com", "jani2003@gmail.com" ], "sub": { "-salutation-": [ "Lieber Herr xxx Wuethrich,", "Lieber Herr janith chinthana," ], "-site_url-": [ "http://first-class-and-more.local/?mid=1082_75ecefe10e55ddf7dc1757cafb6784e0&data=%7B%22title_id%22%3A%221%22%2C%22name%22%3A%22Urs%22%2C%22surname%22%3A%22Wuethrich%22%2C%22email%22%3A%22jani.win%40hotmail.com%22%7D", "http://first-class-and-more.local/?mid=1082_606eaa53b4f5a172f270d5af16d3a526&data=%7B%22title_id%22%3A%221%22%2C%22name%22%3A%22janith%22%2C%22surname%22%3A%22chinthana%22%2C%22email%22%3A%22jani2003%40gmail.com%22%7D" ], "-unsubscribe_url-": [ "http://first-class-and-more.local/?mailingAction[action]=unsubscribe&mailingAction[email]=jani.win%40hotmail.com", "http://first-class-and-more.local/?mailingAction[action]=unsubscribe&mailingAction[email]=jani2003%40gmail.com" ] }, "category": [ "MailID-1082" ] }

unfortunately for the second batch is not receiving the emails while the first one receive the same email twice.

I have done this test in locally, not in the live environment, and I hope I miss something in the smptapi header which cause this issue. Please help me to figure out the issue. when I communicate with support team they told me to post the issue over here, so that I get fast replies.

thinkingserious commented 8 years ago

Have you taken a look at our PHP client? https://github.com/sendgrid/sendgrid-php. That library might make this process easier for you.

janith commented 8 years ago

I went through the example folder, but I couldn't find example code for bulk mail sending. Could you please help me to find proper example.

janith commented 8 years ago

I think we can't use this library as we are still using PHP 5.3/5.4.

thinkingserious commented 8 years ago

Hi @janith,

The library should still work, but it is not supported, because those versions of PHP are end of life. You will need to download the library like this: https://github.com/sendgrid/sendgrid-php#alternative-install-package-from-zip

For bulk sending, you will want to get familiar with personalizations: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html

I hope that helps!

janith commented 8 years ago

I still couldn't resolve the issue. Actually the real issue is, not the sending. when I send the emails it is sending, but it is not the way I need.

However, when I try to use above library, I receive following error, even after setting the proper API key.

401{"errors":[{"field":null,"message":"authorization required"}]}Array

janith commented 8 years ago

my task would be very simple. eg: let's say I have 11 email address. I will divide them into batches, each batch will have 3 email address. That means I have 3 batches with 3 emails and one batch with 2 emails, altogether 4 batches.

I need to send a email which is same for all, but salutation would be different according to the email address.

Can you please point me out to a sample code.

Please note that I followed "SMTPAPI" (https://sendgrid.com/docs/API_Reference/SMTP_API/using_the_smtp_api.html) which leads me to the issue, which I mentioned in the question.

thinkingserious commented 8 years ago

Hello @janith,

For the 401 error, that is an API key related issue and our support team can help you with that: https://support.sendgrid.com.

With regards to the specific use case you have, we do not have any code that does what you want specifically. It sounds like what you need is a template, that takes in the salutation as a parameter. You can find a template example here: https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#transactional_templates

janith commented 8 years ago

Hello @thinkingserious,

After 10 days of time, I still couldn't find a proper solution for batch mail, I will try to explain again what I need to implement in my best. I have tried various ways, however all of them were end up with a dead end.

Here is the task in brief : I need to send newsletter to more than 50000 customers, currently we are sending one by one using php mime which will take ages to send out all the emails. So we plan to use bulk email sending option. but still we create the email content(html) on our side(currently not planing to use the template option) with substitutions. Let say I need just need to modify few things such as salutation, unsubscribe link, web viewing link per email address. rest will be same for all. In other word we have general newsletter for all 50000 members, only different per member would be salutation and few links in the mail.

My question is which API or service I should use for this particular task. Please note that I'm using PHP as programming language.

I figured out the 401 error with your support team, which means I can use the API key with your library. I hope this time I will get a clear answer from your side. I'm so frustrated with the situation as my management need a quick solution for this issue and they were hoping to use any other service provider in the business if you unable to response well. This is really critical situation.

If you think personalizations will help to overcome this issue, please let me know how to handle personalizations string with substitutions for each email address.

https://github.com/sendgrid/sendgrid-nodejs/blob/master/examples/helpers/mail/example.js#L146

this is nearest example that I found in your example folder. However, it is not giving clear idea how to do substitution which is related to each mail address. (for each mail address will receive a general mail but with specific salutation and few specific links)

I hope you get my point. Please point me out to a proper documentation/sample code/link or any resource which could help me to implement that.

thinkingserious commented 8 years ago

Hello @janith,

I think we can help you figure out what you need. Please contact our support team at https://support.sendgrid.com for further assistance.

BTW, have you looked at this example? https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#transactional_templates

With Best Regards,

Elmer

janith commented 8 years ago

Hello @thinkingserious ,

When ever I am getting touch with your support team regarding a code issue, they will point me out here. I feel this is just a passing the ball here & there for last two weeks without finding a solution. I am really disappointing the way of handling the customer issues.

temples is not helping me as I describe it in my earlier message.

awwa commented 7 years ago

Hi @janith , Your situation is difficult to support. The reason is here:

  1. You have not provided sufficient information to reproduce your problem.
  2. You have not isolated the problem whether there is a problem on your program, on API side or on library side.
  3. The current version of sendgrid-php does not support smtpapi.
  4. The current version of sendgrid-php does not work on PHP 5.3/5.4.

So my recommendation is forgot about this library and use curl directly from PHP. Here is the simple code to access Web API v3 Mail Send endpoint which does not use smtpapi. You can add any option parameters include substitutions and category. Also, you can add recipients under the personalizations array. Do not forget set your API key to SENDGRID_API_KEY environment variable. You should see the documentation for more information. I hope this will be your help.

<?php
$data = '{
  "personalizations": [
    {
      "to": [
        {
          "email": "recipient1@hoge.com"
        }
      ]
    },
    {
      "to": [
        {
          "email": "recipient2@hoge.com"
        }
      ]
    }
  ],
  "subject": "Hello, World!",
  "from": {
    "email": "sender@hoge.com"
  },
  "content": [
    {
      "type": "text/plain",
      "value": "Hello, World!"
    }
  ]
}';

$header = array(
  'Authorization: Bearer '.getenv('SENDGRID_API_KEY'),
  'Content-Type: application/json'
);

$opts = [
  CURLOPT_URL => 'https://api.sendgrid.com/v3/mail/send',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HEADER => true,
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => $header,
  CURLOPT_POSTFIELDS => $data
];
$ch = curl_init();
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
print_r(array_filter(explode("\r\n", $response)));
curl_close($ch);
janith commented 7 years ago

Hi @awwa ,

Thanks. Anyway I was able to solve the with the help of sendgrid support team and array as follows,

{
    "personalizations": [{
        "to": [{
            "email": "jani2003@gmail.com"
        }],
        "substitutions": {
            "-salutation-": "Lieber Herr janith chinthana,",
            "-email-": "jani2003%40gmail.com",
            "-site_url-": "-site-",
            "-unsubscribe_url-": "-unsub-"
        }
    }, {
        "to": [{
            "email": "jani.win@hotmail.com"
        }],
        "substitutions": {
            "-salutation-": "Lieber Herr xxx Wuethrich,",
            "-email-": "jani.win%40hotmail.com",
            "-site_url-": "-site-",
            "-unsubscribe_url-": "-unsub-"
        }
    }, {
        "to": [{
            "email": "jan.thch@gmail.com"
        }],
        "substitutions": {
            "-salutation-": "Lieber Herr xxx Steffens,",
            "-email-": "jan.thch%40gmail.com",
            "-site_url-": "-site-",
            "-unsubscribe_url-": "-unsub-"
        }
    }, {
        "to": [{
            "email": "franc@first-class-and-more.de"
        }],
        "substitutions": {
            "-salutation-": "Liebe Frau Franc YYY,",
            "-email-": "franc%40first-class-and-more.de",
            "-site_url-": "-site-",
            "-unsubscribe_url-": "-unsub-"
        }
    }],
    "from": {
        "email": "string (required)",
        "name": "string (optional)"
    },
    "reply_to": {
        "email": "string (required)",
        "name": "string (optional)"
    },
    "subject": "string (required)",
    "content": [{
        "type": "string (optional)",
        "value": "string (optional)"
    }],
    "sections": {
        "-unsub-": "http://first-class-and-more.local/?mailingAction[action]=unsubscribe&mailingAction[email]=-email-",
        "-site-": "http://first-class-and-more.local/?mid=1082_606eaa53b4f5a172f270d5af16d3a526&data=%7B%22title_id%22%3A%221%22%2C%22name%22%3A%22janith%22%2C%22surname%22%3A%22chinthana%22%2C%22email%22%3A%22-email-%22%7D"
    },
    "categories": [
        "MailID-1082"
    ]
}