0

I can successfully send emails with App Engine Mail API, but i cannot find the problem for why the attachment do not get attached in email. My code looks like this:

// Pull in the raw file data of the image file to attach it to the message.
$image_data = fopen('gs://pro-sitemaps-api/file.csv');

try {
    $message = new Message();
    $message->setSender('myemail@****.com');
    $message->addTo('myemail@****.com');
    $message->setSubject('Subject Google App Engine Test');
    $message->setTextBody('Test body');
    $message->addAttachment('file.csv', $image_data);
    $message->send();
    echo 'Mail Sent';
} catch (InvalidArgumentException $e) {
    echo 'There was an error'.$e;
}

My file is hosted in Google Storage (bucket). The filetype of storage file is .csv

Can anyone tell me what i am doing wrong?

Edit:
Adding "mode":"r" gives me this error:

$image_data = fopen('gs://pro-sitemaps-api/file.csv', 'r');

Output:

error: {
code: "500",
message: "An error occurred parsing (locally or remotely) the arguments to mail.Send().",
status: "UNKNOWN",
details: [ ]
}
}





Edit: This works but this only sends the top line (header). not all lines. Tried adding this inside a array and send the array as attachment but then i get same error.:

$fpmail = fopen('gs://pro-sitemaps-api/file.csv', 'r');
    //$attach = fread($fpmail);
    $attach = fgets($fpmail);
    print_r($attach);

    try {
        $message = new Message();
        $message->setSender('asim@redperformance.no');
        $message->addTo('asim@redperformance.no');
        $message->setSubject('Test');
        $message->setTextBody('Test nyeste');
        $message->addAttachment('file.csv', $attach);
        $message->send();
        echo 'Mail Sent';
    } catch (InvalidArgumentException $e) {
        echo $e;
    }

    fclose($fpmail);
Asim
  • 936
  • 2
  • 10
  • 29
  • Try changing your `fopen` instruction call to read mode: `$image_data = fopen('gs://pro-sitemaps-api/file.csv', 'r');` – Mangu Aug 19 '18 at 08:38
  • @Mangu it did not work. Changing "mode" to "r" gives this error "An error occurred parsing (locally or remotely) the arguments to mail.Send().", And why the downvote? – Asim Aug 19 '18 at 11:21
  • 1
    I didn't downvote, the question looks interesting. Did you had any error message before? If so, do edit your question to include them. – Mangu Aug 19 '18 at 11:27
  • Without the attachment the mail is successfull and retrived in my innbox. But with the attachment i get this error above. (edited answer) – Asim Aug 19 '18 at 11:33
  • @Mangu this is a strange error, not able to find other people facing the same issue – Asim Aug 19 '18 at 13:36

1 Answers1

1

I don't ever code in PHP, but it looks like you are not actually reading the contents of the file.

In most programming languages, a call like fopen is just the first step of getting the contents of the file, but doesn't actually get the contents. You'll probably need something like this:

$f = fopen('gs://pro-sitemaps-api/file.csv', 'r');
$image_data = $f.read();

but you'll need to figure out how to read data from a file in PHP.

new name
  • 15,861
  • 19
  • 68
  • 114
  • Trying to find out how. Tried reading and adding the content in array and send as attachment but same error. – Asim Aug 19 '18 at 21:05