Avoid to send email to contacts with same e-mail

Question Status

Verified
Rikarddo asked a question on 16 May 2018 4:39 AM

Hi all,

Imagine this possible situation

I have contact A with e-mail X, and contact B with same e-mail X.
I'm making a a function to go though all contacts and send an email, but in this case i don't wan't that contact B receive another e-mail.

Contact.RESET;
Contact.SETRANGE("No.","No.");
Contact.SETFILTER(Contact."E-Mail",'<>%1','');

IF Contact.FINDSET THEN REPEAT 
  Recipients:=Contact."E-Mail";




//Recipients:='roliveira@sameca.com';//
SMTPMailSetup.GET;
SMTPMail.CreateMessage(
  '',
  'roliveira@sameca.com',
  Recipients,
  TestMailTitleTxt1,
  STRSUBSTNO(
    MailBody,
    USERID),TRUE);
SMTPMail.Send;

UNTIL Contact.NEXT=0;


Im thinking of using a temp table and whenever an e-mail is sent the address will be inserted in that table and then check before send the e-mail if it already exists in the table.

Would like to read some opinions. Thanks

Reply
Verified Answer
Andrey Baludin responded on 16 May 2018 6:39 AM

Hello!

I would create a temporary table with only text field (because I can't remember any existing table with key field of text or long code type).

After that i would use next conctruction:

Contact.RESET;
Contact.SETRANGE("No.","No.");
Contact.SETFILTER(Contact."E-Mail",'<>%1','');

IF Contact.FINDSET THEN REPEAT 

temprecord.init;

temprecord.keyfield := Contact."E-mail";

if temprecord.INSERT THEN BEGIN

SMTPMailSetup.GET;

SMTPMail.CreateMessage( '', 'roliveira@sameca.com', Contact."E-mail", TestMailTitleTxt1, STRSUBSTNO( MailBody, USERID),TRUE);

SMTPMail.Send;

END;

UNTIL Contact.next = 0;

Reply
Verified Answer
Suresh Kulla responded on 16 May 2018 7:49 AM

Yes you can do that, after sending the email store the contact in the temporary contact table, you don't need to create another table for this purpose and before sending the email just check in the temporary table if there is a record with that email, if not send the email and insert the record.

Reply
Verified Answer
Olister Rumao responded on 16 May 2018 10:20 PM

Hi Rikarddo,

All you need to do is sort the Contact By Email.

Store the Currently used Contact Email in a variable and then check if the variable value is not equal to the next contact record.

In the following code I've created a variable called as tempemail of the type Text and use the code

TempEmail := '';

Contact.RESET;

Contact.SETRANGE("No.","No.");

Contact.SETFILTER(Contact."E-mail",'<>%1','');

Contact.SETCURRENTKEY(Contact."E-mail");

Contact.ASCENDING(TRUE); //Sort by E-mail

IF Contact.FINDSET THEN

REPEAT

 IF TempEmail <> Contact."E-mail" THEN BEGIN

 //Your Emailing Code

  Recipients:=Contact."E-Mail";

  SMTPMailSetup.GET;

  SMTPMail.CreateMessage('','roliveira@sameca.com',Recipients,TestMailTitleTxt1,

   STRSUBSTNO(MailBody,USERID),TRUE);

 SMTPMail.Send;

 TempEmail := Recipients;

 END;

UNTIL Contact.NEXT=0;

Reply
Rikarddo responded on 17 May 2018 8:58 AM

Thanks for the help! All the answers are great and now i have the "know how" to finish it.

Reply
Verified Answer
Andrey Baludin responded on 16 May 2018 6:39 AM

Hello!

I would create a temporary table with only text field (because I can't remember any existing table with key field of text or long code type).

After that i would use next conctruction:

Contact.RESET;
Contact.SETRANGE("No.","No.");
Contact.SETFILTER(Contact."E-Mail",'<>%1','');

IF Contact.FINDSET THEN REPEAT 

temprecord.init;

temprecord.keyfield := Contact."E-mail";

if temprecord.INSERT THEN BEGIN

SMTPMailSetup.GET;

SMTPMail.CreateMessage( '', 'roliveira@sameca.com', Contact."E-mail", TestMailTitleTxt1, STRSUBSTNO( MailBody, USERID),TRUE);

SMTPMail.Send;

END;

UNTIL Contact.next = 0;

Reply
Verified Answer
Suresh Kulla responded on 16 May 2018 7:49 AM

Yes you can do that, after sending the email store the contact in the temporary contact table, you don't need to create another table for this purpose and before sending the email just check in the temporary table if there is a record with that email, if not send the email and insert the record.

Reply
Verified Answer
Olister Rumao responded on 16 May 2018 10:20 PM

Hi Rikarddo,

All you need to do is sort the Contact By Email.

Store the Currently used Contact Email in a variable and then check if the variable value is not equal to the next contact record.

In the following code I've created a variable called as tempemail of the type Text and use the code

TempEmail := '';

Contact.RESET;

Contact.SETRANGE("No.","No.");

Contact.SETFILTER(Contact."E-mail",'<>%1','');

Contact.SETCURRENTKEY(Contact."E-mail");

Contact.ASCENDING(TRUE); //Sort by E-mail

IF Contact.FINDSET THEN

REPEAT

 IF TempEmail <> Contact."E-mail" THEN BEGIN

 //Your Emailing Code

  Recipients:=Contact."E-Mail";

  SMTPMailSetup.GET;

  SMTPMail.CreateMessage('','roliveira@sameca.com',Recipients,TestMailTitleTxt1,

   STRSUBSTNO(MailBody,USERID),TRUE);

 SMTPMail.Send;

 TempEmail := Recipients;

 END;

UNTIL Contact.NEXT=0;

Reply