MailApp not functioning reliably?

34 views
Skip to first unread message

Morris Saleh

unread,
Apr 18, 2024, 4:31:59 AMApr 18
to Google Ads Scripts Forum
Hello there,

I would appreciate your help on this. I am trying to receive a CSV export, however the MailApp does not seem to be executing correctly. Is there something I am missing or is there something wrong with the MailApp?:

Code:
function main() {  
function sendCampaignDataAsEmail() {
  var csvContent = "Account,Campaign,Start Date,End Date,Budget," +
                   "Days Spent,Days Remaining,Days Total,Spent,Average," +
                   "Yesterday,Target,Difference,Pacing,Yest. %,Updated %," +
                   "Budget %,Remaining Budget\n";

  var campaignIterator = AdsApp.campaigns().get();
  var today = new Date();

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var stats = campaign.getStatsFor("ALL_TIME");
    var campaignStartDate = new Date(campaign.getStartDate());
    var campaignEndDate = campaign.getEndDate() ? new Date(campaign.getEndDate()) : new Date();

    var daysTotal = Math.ceil((campaignEndDate - campaignStartDate) / (1000 * 60 * 60 * 24));
    var daysSpent = Math.ceil((today - campaignStartDate) / (1000 * 60 * 60 * 24));
    var daysRemaining = daysTotal - daysSpent;
    var budget = campaign.getBudget().getAmount();
    var spent = stats.getCost();
    var average = spent / daysSpent;
    var yesterdayStats = campaign.getStatsFor("YESTERDAY");
    var yesterdaySpend = yesterdayStats.getCost();
    var target = budget / daysTotal;
    var difference = budget - spent;
    var pacing = spent / budget;
    var yestPercent = (yesterdaySpend / budget) * 100;
    var updatedPercent = (spent / budget) * 100;
    var remainingBudget = budget - spent;

    var line = [
      '"' + campaign.getAccount().getName() + '"',
      '"' + campaign.getName() + '"',
      '"' + campaignStartDate.toISOString().slice(0, 10) + '"',
      '"' + campaignEndDate.toISOString().slice(0, 10) + '"',
      budget.toFixed(2), daysSpent, daysRemaining, daysTotal, spent.toFixed(2),
      average.toFixed(2), yesterdaySpend.toFixed(2), target.toFixed(2),
      difference.toFixed(2), pacing.toFixed(4),
      yestPercent.toFixed(2) + "%", updatedPercent.toFixed(2) + "%",
      updatedPercent.toFixed(2) + "%", remainingBudget.toFixed(2)
    ].join(",");
    csvContent += line + "\n";
  }

  // Email parameters
  var subject = "Example report Spain";
  var recipient = "test.e...@test.com";
  var body = "Please find the attached report.";
  var attachments = [Utilities.newBlob(csvContent, "text/csv", "Test_example.csv")];

  // Email with the attachment
  MailApp.sendEmail({
    to: recipient,
    subject: subject,
    body: body,
    attachments: attachments
  });
}}
  
After multiple tries and edits, I tried the simplest MailApp function and also did only receive a mail once out of 7 tries.

  function sendEmail(recipient, subject, body) {
MailApp.sendEmail("Test...@gmail.com",
                   "test reports",
                  "Where are the test reports?");
}}
  

Thank you for any support and kind regards,

Morris

Google Ads Scripts Forum Advisor

unread,
Apr 18, 2024, 8:03:15 AMApr 18
to adwords...@googlegroups.com

Hi Morris,

Thank you for reaching out to the Google Ads Scripts team. 

Upon checking your query, I have verified that the MailApp function is working fine. Sometimes network issues may lead to these kinds of problems. Please check your network and try again. If you are still facing the issue, please provide the below details for further investigation:

  • Google Ads account ID / CID
  • Name of the script.

You can share the requested details via Reply privately to the author option or a direct private reply to this email.

This message is in relation to case "ref:!00D1U01174p.!5004Q02tPCYh:ref" (ADR-00231664)

Thanks,
 
Google Logo Google Ads Scripts Team


Morris Saleh

unread,
Apr 22, 2024, 6:11:06 AMApr 22
to Google Ads Scripts Forum
Hello!

Thank you for getting back to me so quickly. I am unable to reply privately as the option is greyed out. 

I still do not receive any mails from the MailApp. I thought that there must be something in the code that's preventing the MailApp from functioning correctly, but again today, even just testing the MailApp by itself does not work.

Google Ads account ID: 628-368-4507
Name of script: Test 1

Thank you for your support.

Kind regards,

Morris

Nils Rooijmans

unread,
Jun 11, 2024, 2:34:21 AM (10 days ago) Jun 11
to Google Ads Scripts Forum
sounds like this could be an authorization issue. have you tried testing copy-pasting the code in a completely new script, and authorizing the new script from scratch?

Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Jun 11, 2024, 5:45:05 AM (10 days ago) Jun 11
to adwords...@googlegroups.com

Thank you Nils for providing the solution.

Hi Morris,

I would suggest that you follow the solution provided by Nils and get back to us if you still face any issues. 

Reply all
Reply to author
Forward
0 new messages