Save email sent APP control and error message

Hi, I have a control application that sends email providers entered into a database, the applicaizone sends the same email to one or more selected addresses and passed by a grid.

The control application works perfectly, send emails correctly, but I would like to make it more complete with two important functions.

  1. I WANT THAT COMMUNICATIONS SENT TO SUPPLIERS HAVE REGISTERED IN databese comunicazioni_fornitori

  2. I would like to enter a confirmation message sending email or error on error and relative addressing a grid_fornitori

The problem for point 1) is that an email can be sent to multiple contacts and in that case you can record in the DB of comunicazioni_fornitori records separated?

Here is the code that has the control application

$ var_email_to = [var_email_to];
$ var_email_invio email_invio = {};
$ var_oggetto = {object};
$ var_messaggio = {text};

/ **

  • Send a simple email
  • /

// Email parameters
$ mail_smtp_server = ‘smtp.AAAAAA.com’; // SMTP server name or IP address
$ mail_smtp_user = ‘info@AAAAAA.com’; // SMTP user name
$ mail_smtp_pass = ‘bbbbbbbbb’; // SMTP password
$ = $ mail_from var_email_invio; // From email
$ = $ mail_to var_email_to; // To email
$ = $ mail_subject var_oggetto; // Message subject
$ = $ mail_message var_messaggio; // Message body
$ mail_format = ‘H’; // Message format: (T) ext or (H) tml

// Send email ";
sc_mail_send ($ mail_smtp_server,
$ mail_smtp_user,
$ mail_smtp_pass,
$ mail_from,
$ mail_to,
$ mail_subject,
$ mail_message,
$ mail_format);

Go to your grid and change your run button.
Run OnRecord:
[arr_email_to][] = {supplier_id};

Now change your onFinish code

Run OnFinish
if (count ([arr_email_to])> 0)
{
$var_email_to = “(”.implode (’,’, [arr_email_to]).")";
sc_redir(control_email_fornitori,var_email_to = $var_email_to);
}

In the control app:

$email_sql = “SELECT GROUP_CONCAT(email SEPARATOR ‘;’) FROM supplier WHERE supplier_id IN [var_email_to]”;
sc_lookup(rs,$email_sql);
if(isset({rs[0][0]}))
{
$var_mail_to = {rs[0][0]};
}

After your email has been sent (or not). http://www.scriptcase.net/docs/en_us/v8/scriptcase-macros/scriptcase-macros#sc_mail_send

if ({sc_mail_ok})
{
$result = “Email sent with sucess!!”;
}
else
{
$result = {sc_mail_erro};
}

Than update your table(s) with: UPDATE … WHERE supplier_id IN [var_email_to]

jsb

Hello, thank you again for the information.
But one question, in the database of communications sent I want to save separately the following fields;
email provider = email_fornitore
subject of the communication object =
communication sent text = communication

Please show me how to change the code?

Who can help me please ???

Excuse me, where in the application code control I need to enter this code:

$ email_sql = “SELECT GROUP_CONCAT (email SEPARATOR ‘;’) FROM WHERE supplier supplier_id IN [var_email_to]”;
sc_lookup (rs, $ email_sql);
if (isset (rs {[0] [0]}))
{
var_mail_to $ rs = {[0] [0]};
}

Right in front of your email stuff.
Delete the line ‘$var_email_to = [var_email_to]’ and replace it with the code provided.

$email_sql = “SELECT GROUP_CONCAT(email SEPARATOR ‘;’) FROM supplier WHERE supplier_id IN [var_email_to]”;
sc_lookup(rs,$email_sql);
if(isset($rs) && !emtpy($rs))
{
$var_mail_to = {rs[0][0]};
}
else
{
sc_error_message(‘something went wrong!’);
sc_error_exit();
}
$ var_email_invio email_invio = {};
$ var_oggetto = {object};
$ var_messaggio = {text};

/ **

  • Send a simple email
  • /

After your email has been sent you can do something like the following to keep track of emails sent.

if ({sc_mail_ok})
{
$result = “Email sent with sucess!!”;
}
else
{
$result = {sc_mail_erro};
}

$ins_sql = “INSERT INTO comunicazioni_fornitori (supplier, email, subject, result) SELECT supplier_id, supplier_email, $mail_subject, $result FROM supplier WHERE supplier_id IN [var_mail_to”;
sc_exec_sql($ins_sql);

You might of course adapt the code to your needs but this should point you in the right direction.

jsb

Hello, please help me … it is very important.
I entered the settings you specified, but the application does not work, does not print in the application control the emails that I have selected in the grid.

That’s all my code:

$email_sql = “SELECT GROUP_CONCAT(email SEPARATOR ‘;’) FROM supplier WHERE supplier_id IN [var_email_to]”;
sc_lookup(rs,$email_sql);
if(isset($rs) && !emtpy($rs))
{
$var_mail_to = {rs[0][0]};
}
else
{
sc_error_message(‘Messaggio non inviato!’);
sc_error_exit();
}
$var_email_invio = {email_invio};
$var_oggetto = {oggetto};
$var_messaggio = {text};

/**

  • Send a simple email
    */

// Email parameters
$mail_smtp_server = ‘smtp.aaaaaa.com’; // SMTP server name or IP address
$mail_smtp_user = ‘info@aaaaa.com’; // SMTP user name
$mail_smtp_pass = ‘vvvvvvvvvv’; // SMTP password
$mail_from = $var_email_invio; // From email
$mail_to = $var_email_to; // To email
$mail_subject = $var_oggetto; // Message subject
$mail_message = $var_messaggio; // Message body
$mail_format = ‘H’; // Message format: (T)ext or (H)tml

// Send email";
sc_mail_send($mail_smtp_server,
$mail_smtp_user,
$mail_smtp_pass,
$mail_from,
$mail_to,
$mail_subject,
$mail_message,
$mail_format);

if ({sc_mail_ok})
{
$result = “Email Inviata con Successo!!”;
}
else
{
$result = {sc_mail_erro};
}

$ins_sql = “INSERT INTO fornitoricomunicazioni (supplier, email, oggetto_comunicazione, comunicazione) SELECT supplier_id, supplier_email, $var_oggetto, $var_messaggio FROM supplier WHERE supplier_id IN [var_mail_to”;
sc_exec_sql($ins_sql);

$email_sql = “SELECT GROUP_CONCAT(email SEPARATOR ‘;’) FROM supplier WHERE supplier_id IN [var_email_to]”;
sc_lookup(rs,$email_sql);
if(isset($rs) && !emtpy($rs)) <-----

May be you should try !empty instead of !emtpy. :slight_smile:

And don’t try everything at the same time. Step by step.
Start with the grid, does the Run button work? Did you apply the suggestions posted earlier?
Then see if the email_sql statement delivers what you expect.
Also, try it without the insert statement at the end, you can add it when everything else works.
Are there any error messages? ‘Won’t work’ doesn’t help very much.

jsb

Hello, the mistake that I printed before loading the control application is:
Undefined Variable: supplier_id

And in the grid in the RUN button I;
OnRecord
[arr_email_to] [] = {} supplier_id;

OnFinish
if (count ([arr_email_to])> 0)
{
$ var_email_to = “(” .implode (’,’, [arr_email_to]). “)”;
sc_redir (control_email_fornitori, var_email_to = $ var_email_to);
}

it is as if he can not understand that must pass the email.

Ok, let’s do some testing.
Change this:

OnFinish
if (count ([arr_email_to])> 0)
{
$ var_email_to = “(” .implode (’,’, [arr_email_to]). “)”;
sc_redir (control_email_fornitori, var_email_to = $ var_email_to);
}

to this:


OnFinish
if (count ([arr_email_to])> 0)
{
    $ var_email_to = "(" .implode (',', [arr_email_to]). ")";
    //sc_redir (control_email_fornitori, var_email_to = $ var_email_to);
    print_r($var_email_to);
    sc_exit();
}

Do you get what you want?

jsb

Hello, I tried the code but the error remains the same.
the code I added to

OnFinish is:
if (count ([arr_email_to])> 0)
{
$ var_email_to = “(” .implode (’,’, [arr_email_to]). “)”;
sc_redir (control_email_fornitori, var_email_to = $ var_email_to);
print_r ($ var_email_to);
sc_exit ();
}

Run OnRecord:
[arr_email_to][] = {supplier_id};

Do you have the field name {supplier_id} (it should be the primay key) in your grid/table or is it named differently?

jsb

In my onRecord the grid have exactly

Run OnRecord:
[arr_email_to] [] = {} supplier_id;

… Unfortunately it does not work

in the grid in OnScriptlnit I code:
[arr_email_to] = array ();

Perhaps here is the error.

[QUOTE=peraziende;32197]in the grid in OnScriptlnit I code:
[arr_email_to] = array ();

Perhaps here is the error.[/QUOTE]

Nope, that’s correct.
The error is related to the field {supplier_id}. That’s what SC is complaining about.

jsb

Hello, I think I’m one step away from the solution, I took part in the code that records the data in the database of the historical communications and I modified some code.
Now grig communicating properly to control application selected emails and printing in the field of recipients in this way;
(info @ aaa.com, max @ bbb.com)

But when I click send email me error and tells me;

Fatal error: Uncaught exception ‘Swift_RfcComplianceException’ with message ‘Address in mailbox Given [SELECT GROUP_CONCAT (email SEPARATOR’] does not comply with RFC 2822, 3.6.2. 'In/web/htdocs/www.AAAAAAA.com/home/admin/ammingestionale/_lib/prod/third/swift/classes/Swift/Mime/Headers/MailboxHeader.php:308 Stack trace: # 0/web/htdocs/www.AAAAAAA.com/home/admin/ammingestionale/_lib/prod/third/swift/classes/Swift/Mime/Headers/MailboxHeader.php(238): Swift_Mime_Headers_MailboxHeader -> _ assertValidAddress (‘SELECT GROUP_CO …’) # 1/web/htdocs/www.AAAAAAA.com/home/admin/ammingestionale/_lib/prod/third/swift/classes/Swift/Mime/Headers/MailboxHeader.php(96): Swift_Mime_Headers_MailboxHeader-> normalizeMailboxes (Array) # 2/web/htdocs/www.AAAAAAA.com/home/admin/ammingestionale/_lib/prod/third/swift/classes/Swift/Mime/Headers/MailboxHeader.php(60): Swift_Mime_Headers_MailboxHeader-> setNameAddresses (Array) # 3/web/htdocs/www.AAAAAAA.com/home/admin/ammingestionale/_lib/prod/third/swift/classes/Swift/Mime/SimpleHeaderFactory.php(6 in/web/htdocs/www.AAAAAAA.com/home/admin/ammingestionale/_lib/prod/third/swift/classes/Swift/Mime/Headers/MailboxHeader.php on line 308
Here is all the code in onValida of my control;

$ email_sql = “SELECT GROUP_CONCAT (email SEPARATOR ‘;’) FROM WHERE email fornitorischede IN [var_email_to]”;
sc_lookup (rs, $ email_sql);
if (isset (rs {[0] [0]}))
{
var_mail_to $ rs = {[0] [0]};
}
$ var_email_invio email_invio = {};
$ var_oggetto = {object};
$ var_messaggio = {text};

/ **

  • Send a simple email
  • /

// Email parameters
$ mail_smtp_server = ‘smtp.aaaaaa.com’; // SMTP server name or IP address
$ mail_smtp_user = ‘info@aaaaaa.com’; // SMTP user name
$ mail_smtp_pass = ‘aaaaaaaaa’; // SMTP password
$ = $ mail_from var_email_invio; // From email
$ = $ mail_to email_sql; // To email
$ = $ mail_subject var_oggetto; // Message subject
$ = $ mail_message var_messaggio; // Message body
$ mail_format = ‘H’; // Message format: (T) ext or (H) tml

// Send email ";
sc_mail_send ($ mail_smtp_server,
$ mail_smtp_user,
$ mail_smtp_pass,
$ mail_from,
$ mail_to,
$ mail_subject,
$ mail_message,
$ mail_format);

if ({} sc_mail_ok)
{
$ result = “Email Sent Successfully !!”;
}
else
{
$ result = {} sc_mail_erro;
}

Change this line:
$ = $ mail_to email_sql; // To email <-----

To
$mail_to = $var_mail_to;

jsb

Hello, I have modified and made the test is selecting a single recipient with two recipients, the answer I have is always the same;
Undefined variable: var_mail_to

Yet in the field email_to emails that I select from the grid are inserted properly …

Where can I hide the error ???

$ email_sql = “SELECT GROUP_CONCAT (email SEPARATOR ‘;’) FROM WHERE email fornitorischede IN [var_email_to]”;
sc_lookup (rs, $ email_sql);
if (isset (rs {[0] [0]}))
{
$var_mail_to = rs{[0] [0]};
}

Check your SQL statement after the FROM keyword. You don’t get a result if it is like the above, hence $var_mail_to is not defined.

jsb

Hello, thank you so much for your patience, unfortunately the error remains, I believe that is in the application code control.

The database name doce the grid passes the email is: fornitorischede
the email field is called email

and my application code control is:

$email_sql = “SELECT GROUP_CONCAT (email SEPARATOR ‘;’) FROM fornitorischede WHERE email IN [var_email_to]”;
sc_lookup(rs,$email_sql);
if(isset({rs[0][0]}))
{
$var_mail_to = {rs[0][0]};
}
$var_email_invio = {email_invio};
$var_oggetto = {oggetto};
$var_messaggio = {text};

/**

  • Send a simple email
    */

// Email parameters
$mail_smtp_server = ‘smtp.AAAAAAA.com’; // SMTP server name or IP address
$mail_smtp_user = ‘info@AAAAAAA.com’; // SMTP user name
$mail_smtp_pass = ‘VVVVVVVVVVVV’; // SMTP password
$mail_from = $var_email_invio; // From email
$mail_to = $var_mail_to; // To email
$mail_subject = $var_oggetto; // Message subject
$mail_message = $var_messaggio; // Message body
$mail_format = ‘H’; // Message format: (T)ext or (H)tml

// Send email";
sc_mail_send($mail_smtp_server,
$mail_smtp_user,
$mail_smtp_pass,
$mail_from,
$mail_to,
$mail_subject,
$mail_message,
$mail_format);