0
私は単純なSendGrid API設定をしていますが、送信される電子メールにどのように添付ファイルを含めることができますか?添付ファイルのURLを$ _POST変数に渡しているので、完全なパスを返すことができますが、セットアップにどこにインクルードするかはわかりません。添付ファイル付きSendGrid(PHPライブラリ)
//
$i = 1; foreach ($cart as $download) {
$file . $i = $download['download'];
$file_encoded . $i = base64_encode(file_get_contents($file));
$attachment . $i = new SendGrid\Attachment();
$attachment . $i->setContent($file_encoded);
$attachment . $i->setType("application/zip");
$attachment . $i->setDisposition("attachment");
$attachment . $i->setFilename("test.zip");
$mail->addAttachment($attachment . $i);
i++;
}
// Set email confirmation settings
$email_subject = 'New order from ' . $user_details['first-name-billing'] . ' ' . $user_details['last-name-billing'] . ' via www.***.com';
$email_admin = '******@***.com'; // Client
$email_customer = $user_details['email'];
ob_start();
require_once './email-confirmation.php';
$email_body = ob_get_contents();
ob_end_clean();
// Send to confirmation to admin
if ($email_admin) {
send_email($email_admin, $email_admin, $email_subject, $email_body);
}
// Send confirmation to customer
if ($email_customer) {
send_email($email_admin, $email_customer, $email_subject, $email_body);
}
// SendGrid init
function send_email($from_email, $to_email, $subject, $body) {
global $sgAPIKey;
$from = new SendGrid\Email(null, $from_email);
$to = new SendGrid\Email(null, $to_email);
$content = new SendGrid\Content("text/html", $body);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($sgAPIKey);
$response = $sg->client->mail()->send()->post($mail);
}
ありがとうございました!完璧。私はこれを見つけることができませんでした。複数の添付ファイルを送信できるかどうか知っていますか? –
'addAttachment()'を複数回呼び出すだけです。 – Barmar
私は各ダウンロード '$ downloads'のURLの配列を作りましたが、私のsend_mail関数の中でこれをループできるとは思わないのでちょっと混乱しています... –