Sendgrid-nodejs: خطأ غير متوقع عند محاولة إرسال بريد إلكتروني للمعاملات

تم إنشاؤها على ٣١ أغسطس ٢٠١٧  ·  23تعليقات  ·  مصدر: sendgrid/sendgrid-nodejs

ملخص القضية

الحصول على خطأ "طلب غير صالح" مع الحمولة التالية.

طلب

cosnt msg = {
        "to": "[email protected]",
        "from": "[email protected]",
        "subject": "Support Pay Transparency at PayCheck",
        "templateId": "24ae3147-4faa-4380-8613-c5be144f4542",
        "customArgs": {
            "ally_id": "cj6zlh7yd000001qir4r5suuk"
        }
};
sgMail.send(msg)

استجابة

{
        "message": "Bad Request",
        "code": 400,
        "response": {
            "headers": {
                "server": "nginx",
                "date": "Wed, 30 Aug 2017 22:30:41 GMT",
                "content-type": "application/json",
                "content-length": "365",
                "connection": "close",
                "access-control-allow-origin": "https://sendgrid.api-docs.io",
                "access-control-allow-methods": "POST",
                "access-control-allow-headers": "Authorization, Content-Type, On-behalf-of, x-sg-elas-acl",
                "access-control-max-age": "600",
                "x-no-cors-reason": "https://sendgrid.com/docs/Classroom/Basics/API/cors.html"
            },
            "body": {
                "errors": [
                    {
                        "message": "Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required.",
                        "field": "content",
                        "help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"
                    }
                ]
            }
        }
    }

ملاحظات

في الوثائق تقول templateId ولكن في رسالة الخطأ تقول template_id . لقد جربت كليهما وتلقيت نفس رسالة الخطأ. لقد أرفقت لقطة شاشة لشاشة القالب الخاصة بي كتحقق من سلامة العمل للتأكد من تهيئتي بشكل صحيح في sendgrid

تفاصيل تقنية:

  • sendgrid-nodejs الإصدار: رئيسي (أحدث التزام: [رقم التنفيذ])
  • إصدار Node.js: 6.10.3
  • تعمل في وظيفة AWS Lambda

screen shot 2017-08-30 at 6 43 57 pm

help wanted bug

التعليق الأكثر فائدة

شكرا على المساعدة. كنت أفقد عقلي حرفيا. لقد توصلت أخيرًا إلى حل بديل في وقت متأخر من الليلة الماضية وكنت قادمًا إلى هنا للإبلاغ عن مزيد من المعلومات ، ولكن يبدو أنكم جميعًا موجودون بالفعل :-)

ال 23 كومينتر

onesien ، تعد واجهة برمجة تطبيقات بريد JS البريدية الجديدة غير محددة حالة الأحرف ، لذا يمكنك استخدام كل من templateId أو template_id . حالة الجمل أكثر شيوعًا في أرض JS ، ومن ثم قمنا بإضافة دعم لذلك ، بينما تستهلك واجهة برمجة التطبيقات نفسها حالة الثعبان.

سألقي نظرة لمعرفة ما يحدث مع معرف القالب

مرحبًا onesien ،

قد ترغب أيضًا في التحقق من الدعم . أعتقد أن المشكلة قد تكون متعلقة بمحرر النموذج القديم.

هل يمكنك أيضًا محاولة إنشاء قالب جديد واستخدام هذا المعرف؟

مع أحر تحياتي،

إلمر

thinkingserious هل يمكنك التحقق من هذا على template_id بشكل صحيح مع الطلب. لقد قمت للتو بإنشاء نموذج جديد وأتلقى هذه المشكلة أيضًا.

adamreisnz ،

اختباراتي المحلية تعمل ، لكن لدي قوالب جديدة فقط في حسابنا.

سوف أتحقق مرتين

كلا ، لقد استخدمت نموذجًا جديدًا ، تم إنشاؤه حديثًا ، ويواجه نفس المشكلة

يعمل ما يلي بالنسبة لي (باستخدام الإصدار v6.1.1) باتباع المثال هنا :

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');

//build object 
var mailOptions = {
    personalizations:[{
      to: '[email protected]',
      substitutions: {'name':'John', 'city':'Denver'}
    }],
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    html: 'email text goes here',
    templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
  };

//send 
sendGridMail.send(mailOptions);

آه ، أعتقد أن لدينا ظهور خطأ قديم ، حيث تتطلب واجهة برمجة التطبيقات حظر محتوى في جميع الحالات. إذن هذا يعمل:

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');

//build object 
var mailOptions = {
    to: '[email protected]',
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
    html: ' ',
    substitutions: {
      name: 'Some One',
      city: 'Denver',
    }
  };

//send 
sendGridMail.send(mailOptions);

لكن ليس هذا:

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');

//build object 
var mailOptions = {
    to: '[email protected]',
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
    substitutions: {
      name: 'Some One',
      city: 'Denver',
    }
  };

//send 
sendGridMail.send(mailOptions);

التحقق داخليا ...

اضطررت إلى إضافة كتلة المحتوى لإرسال القالب. كنت أتلقى طلبًا سيئًا (400) "ما لم يتم توفير معرف قالب صالح ، فإن معلمة المحتوى مطلوبة. يجب أن يكون هناك كتلة محتوى محددة واحدة على الأقل ..." _ أثناء استخدام قالب تم إنشاؤه في محرر html الجديد.

sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
  to: email,
  from: '[email protected]',
  templateId: 'xxxxxxx',
  substitutions: {
     verifyUrl: createAccountLink
  }
};
sgMail.send(msg)

إذا قمت بإضافة كتلة المحتوى ، فسيتم إرسال نموذج البريد الإلكتروني بتنسيق في محرر html.

sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
  to: email,
  from: '[email protected]',
  content: [{"type":"text/html","value":"0"}],
  templateId: 'xxxxxx',
  substitutions: {
     verifyUrl: createAccountLink
  }
};
sgMail.send(msg)

حسنًا ، سأترك هذا مفتوحًا باعتباره خطأً.

أعتقد أن SDK هذه تضيف كائن محتوى فارغًا عندما لا يتم تعيينه. أعتقد أننا بحاجة إلى تخطي هذه الوظائف عندما لا يتم تعيين html و txt.

في الوقت الحالي ، سيفعل الحل البديل أعلاه ما تحتاجه.

thinkingserious أعتقد أنك على حق. قرأت مستندات API واعتقدت أن الحقل إلزامي. إنه يحتوي فقط على تعليق مخفي جيدًا (وهو مصمم بشكل مذهل!) فاتني:

image

سوف أقوم بإنشاء علاقات عامة لمعالجة هذا!

هههه شكرا آدم!

تم تحديث العلاقات العامة ، يجب أن يؤدي ذلك إلى حل المشكلة وعدم إرسال حقل المحتوى مع الطلب في حالة عدم توفر محتوى.

شكرا على المساعدة. كنت أفقد عقلي حرفيا. لقد توصلت أخيرًا إلى حل بديل في وقت متأخر من الليلة الماضية وكنت قادمًا إلى هنا للإبلاغ عن مزيد من المعلومات ، ولكن يبدو أنكم جميعًا موجودون بالفعل :-)

آسف بشأن ذلك onesien ،

تم دفع الإصلاح للتو إلى npm (v6.1.2).

لقد استخدمت الكود حرفيًا من استجابة thinkingserious وما زلت

أحاول فقط الحصول على البدائل الأساسية في قالب أنشأته في sengrid ليتم إرسالها. تظهر الاستجابة في CloudWatch (نظرًا لأن هذا يعمل في Lambda) على أنها 202 ولكن البريد الإلكتروني الذي أتلقاه هو: A message was received from this address by our systems that had errors in the smtpapi header, and cannot be processed. The error detected was: The template id must be a valid template id for your account.

ما الخطأ الذي أفعله هنا يا رفاق؟ أحاول التدقيق في مستندات sendgrid ولكن كل شيء يبدو مجزأ حقًا.

كيف ترسل معرف النموذج؟ هل يتم استدعاء لامدا من خلال متغير بيئي؟

شكرًا على الرد السريع cbilliau - لا في حالتي ، فإن سلسلة القالب مشفرة لأنني أحاول فقط الحصول على السقالات الوظيفية بين الواجهة الأمامية والخلفية. المثال الحرفي الخاص بي هو:

const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(
  "XXXXX"
);
sgMail.setSubstitutionWrappers("%", "%"); // Configure the substitution tag wrappers globally
  const msg = {
    to: formFields.toEmail,
    from: formFields.fromEmail,
    subject: "Hello world",
    text: "test",
    html: "<p>test</p>",
    templateId: "be2cc0da-5b2c-428f-8e45-c140f6cfb6eb",
    substitutions: {
      name1: formFields.fromName,
      name2: formFields.fromName,
      date: formFields.date
    }
  };

  sgMail.send(msg);

tetreault راجع إجابتي أعلاه ، أضفت content: [{"type":"text/html","value":"0"}], إلى وظيفتي وقد نجحت . غير معروف لماذا.

لقد جربتها للتو ، للأسف لم تغير النتيجة النهائية استردت نفس الرسالة الإلكترونية:

A message was received from this address by our systems that had errors in the smtpapi header, and cannot be processed. 
The error detected was: The template id must be a valid template id for your account. You provided be2cc0da-5b2c-428f-8e45-c140f6cfb6eb 

واو اسمحوا لي أن أخرج لثانية cbilliau لقد كنت أتعجل من خلال الأشياء ولم أعير المزيد من الاهتمام. أدركت أنني كنت أحصل على المعرّف الفريد لقالب قمت بإنشائه ضمن "تسويق" ولكن ليس ضمن "قوالب المعاملات". أنا لا أستخدم واجهة المستخدم sendgrid كثيرًا وفشل هذا تمامًا

كامل راحة اليد ، والتثبيت في نهايتي ثم أتوقع أن يعمل.

ك ك - تؤكد على العمل الآن، وخاصة باستخدام content: [{ type: "text/html", value: "0" }] الخط الذيcbilliau المذكورة 👍

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات