|null $body * @return array{status: int, body: array|null, raw: string} */ function sendpier_request(string $method, string $path, ?array $body = null): array { $key = getenv('SENDPIER_API_KEY'); $domain = getenv('SENDPIER_DOMAIN'); if ($key === false || $key === '' || $domain === false || $domain === '') { fwrite(STDERR, "set SENDPIER_API_KEY and SENDPIER_DOMAIN first\n"); exit(78); } $headers = [ 'Authorization: Bearer '.$key, 'X-Domain-Name: '.$domain, 'Accept: application/json', ]; $handle = curl_init(API_BASE.$path); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method); // An attachment makes the upload larger; give it room without waiting for // ever on a stalled connection. curl_setopt($handle, CURLOPT_TIMEOUT, 120); if ($body !== null) { $headers[] = 'Content-Type: application/json'; curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($body, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)); } curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); $raw = curl_exec($handle); if ($raw === false) { $error = curl_error($handle); curl_close($handle); fwrite(STDERR, "connection failed: {$error}\n"); exit(1); } $status = (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE); curl_close($handle); $decoded = json_decode((string) $raw, true); return [ 'status' => $status, 'body' => is_array($decoded) ? $decoded : null, 'raw' => (string) $raw, ]; } /** * One entry of the attachments list. * * `content_type` may be left out; it is then derived from the file name. Add a * `content_id` to turn the file into an inline part, and point at it from your * HTML with ``. * * @return array */ function sendpier_attachment(string $path, ?string $contentType = null, ?string $contentId = null): array { $contents = @file_get_contents($path); if ($contents === false) { fwrite(STDERR, "cannot read {$path}\n"); exit(66); } $attachment = [ // Only the file name travels; a path in front of it is cut off anyway. 'filename' => basename($path), 'content' => base64_encode($contents), ]; if ($contentType !== null) { $attachment['content_type'] = $contentType; } if ($contentId !== null) { $attachment['content_id'] = $contentId; } return $attachment; } $recipient = $argv[1] ?? ''; $file = $argv[2] ?? ''; $from = getenv('SENDPIER_FROM'); if ($recipient === '' || $file === '' || $from === false || $from === '') { fwrite(STDERR, "usage: SENDPIER_FROM=invoices@example.com php send-with-attachment.php \n"); exit(64); } $response = sendpier_request('POST', '/v1/messages', [ 'from' => ['email' => $from, 'name' => 'Example'], 'to' => [['email' => $recipient]], 'subject' => 'Your invoice', 'text' => 'Your invoice is attached.', 'html' => '

Your invoice is attached.

', 'attachments' => [ sendpier_attachment($file, 'application/pdf'), ], ]); echo $response['raw'], "\n"; if ($response['status'] !== 202) { // 413 message_too_large means the whole message went over 25 MB; send big // files as a download link instead. 422 attachment_type_blocked means the // file type would never reach an inbox anyway -- the last extension counts, // so invoice.pdf.exe is an .exe. fwrite(STDERR, "request failed with HTTP {$response['status']}\n"); exit(1); } foreach ($response['body']['data'] ?? [] as $message) { fwrite(STDERR, sprintf("queued %s for %s\n", $message['id'] ?? '?', $message['to'] ?? '?')); }