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);