简体   繁体   中英

How to send file as attachment using AWS SES latest SDK 3.33 and PHP7?

I was trying to send file(s) as attachment using PHP AWS SES latest SDK 3.33.

After doing search for solution, I got answer from here but this works on older version of AWS SDK not on current one.

AWS provide two API method sendEmail and sendRawEmail , sendRawEmail is used to send file as attachment to email.

So below is updated code for latest AWS SDK 3.33

require __DIR__ . '/aws/vendor/autoload.php';
$s3_config = [
    'region' => 'us-east-1',
    'version' => 'latest',
    'credentials' => [
        'key' => '<keyxxx>',
        'secret' => '<secretxxx>'
    ]
];
$aws = new \Aws\Sdk($s3_config);
$client = $aws->createSes();
$content = "";
$filename = "";
$mailto = "to@test.com"
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$filename = basename($file);
$subject = "Test Email";
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$FILEINFO_MIME_TYPE = finfo_file($finfo, $file);
try {
    $separator = md5(time());
    $separator_multipart = md5($subject . time());
    $message = "MIME-Version: 1.0\n";
    $message .= "Subject: $subject\n";
    $message .= "From: Test Name <from@test.com>\n";
    $message .= "To: $mailto\n";
    $message .= "Content-Type: multipart/mixed; boundary=\"$separator_multipart\"\n";
    $message .= "\n--$separator_multipart\n";

    $message .= "Content-Type: multipart/alternative; boundary=\"$separator\"\n";
    $message .= "\n--$separator\n";
    $message .= "Content-Type: text/plain; charset=\"UTF-8\"\n";
    $message .= "\n$body\n";
    $message .= "\n--$separator--\n";

    $message .= "--$separator_multipart\n";
    $message .= "Content-Type: $FILEINFO_MIME_TYPE; name=\"$filename\"\n";
    $message .= "Content-Disposition: attachment; filename=\"$filename\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "$content\n";
    $message .= "--$separator_multipart--";

    $result = $client->sendRawEmail([
        'RawMessage' => [
            'Data' => $message
        ]
    ]);
    echo "\nEmail Sent\n";
} catch (\Exception $e) {
    echo $e->getMessage();
}

For those not willing to handle the raw message creation manually, a good alternative is to use PHPMailer to do that for them.

Step 1: Compile the raw message using PHPMailer

$mail=new PHPMailer();
$mail->From="john.doe@example.com";
$mail->FromName="John Doe";
$mail->Subject="SES Email With Attachments";
$mail->Body="<p>Some HTML content</p>";
$mail->isHTML(true);
$mail->addAddress("jane.doe@example.com", "Jane Doe");

//Add attachments one by one
$mail->addStringAttachment(file_get_contents("/path/to/attachment1"), "Attachment1");
$mail->addStringAttachment(file_get_contents("/path/to/attachment2"), "Attachment2");

Step 2: Get the raw MIME message from PHPMailer

if(!$mail->preSend())
{
  die($mail->ErrorInfo);
}

$rawMessage=$mail->getSentMIMEMessage();

Step 3: Send the message using SES

try
{
  $result=$sesClient->sendRawEmail(array
  (
    'RawMessage'=>array
    (
      'Data'=>$rawMessage
    )
  ));

  echo "Success! Message has been sent!";   
}
  catch(\InvalidArgumentException $e)
  {
    echo "The email was not sent. Error message: ".$e->getMessage();
  }
  catch(\Aws\Ses\Exception\SesException $e)
  {
    echo "The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n";
  }
  catch(\AwsException $e)
  {
    echo "The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM