Last active
June 27, 2018 03:35
-
-
Save jesraygarciano/886839644382590d03058d3d67189e92 to your computer and use it in GitHub Desktop.
AWS Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
S3 | |
require 'aws-autoloader.php'; | |
use Aws\S3\S3Client; | |
use Aws\S3\Exception\S3Exception; | |
Create Bucket | |
<?php | |
$bucket_name = 'bucket_name'; | |
$s3 = new S3Client([ | |
'version' => 'latest', | |
'region' => 'us-east-1', | |
]); | |
try { | |
$result = $s3->createBucket([ | |
'Bucket' => $bucket_name, | |
'ACL' => 'public-read', | |
]); | |
} catch (S3Exception $e) { | |
echo $e->getMessage(); | |
} | |
List Buckets | |
<?php | |
$s3 = new S3Client([ | |
'version' => 'latest' | |
'region' => 'us-east-1', | |
]); | |
$buckets = $s3->listBuckets(); | |
foreach ($buckets['Buckets'] as $bucket) { | |
echo $bucket['Name']. "\n"; | |
} | |
Upload Files | |
<?php | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$s3 = new S3Client([ | |
'version' => 'latest', | |
'region' => 'us-east-1', | |
]); | |
$bucket_name = 'bucket_name'; | |
for ($i = 0; $i < $count; $i++) { | |
$file_name = $_FILES['files']['name'][$i]; | |
$file_tmp = $_FILES['files']['tmp_name'][$i]; | |
$objects = [ | |
'Bucket' => $bucket_name, | |
'Key' => $file_name, | |
'SourceFile' => $file_tmp, | |
'ACL' => 'public-read', | |
]; | |
$commands[] = $s3->putObject($objects); | |
} | |
try { | |
$commands; | |
} catch (S3Exception $e) { | |
echo "There was an error uploading the file."; | |
echo $e->getMessage(); | |
} | |
} | |
SES | |
require 'aws-autoloader.php'; | |
use Aws\Ses\SesClient; | |
use Aws\Ses\Exception\SesException; | |
Sending an email | |
<?php | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$sender = 'you@example.com'; | |
$recipient = 'them@example.com'; | |
$message = '<h1>Message body</h1>'; | |
$subject = 'Subject'; | |
$ses = SesClient::factory(array( | |
'version'=> 'latest', | |
'region' => 'us-east-1', | |
)); | |
try { | |
$result = $ses->sendEmail([ | |
'Destination' => [ | |
'ToAddresses' => [ | |
$recipient, | |
], | |
], | |
'Message' => [ | |
'Body' => [ | |
'Html' => [ | |
'Charset' => 'UTF-8', | |
'Data' => $message, | |
], | |
'Text' => [ | |
'Charset' => 'UTF-8', | |
'Data' => 'Text email', | |
], | |
], | |
'Subject' => [ | |
'Charset' => 'UTF-8', | |
'Data' => $subject, | |
], | |
], | |
'Source' => $sender, | |
]); | |
$messageId = $result->get('MessageId'); | |
echo("Email sent! Message ID: $messageId"); | |
} catch (SesException $error) { | |
echo("The email was not sent. Error message: " . $error->getAwsErrorMessage()); | |
} | |
} | |
Using PHP Mailer | |
<?php | |
use PHPMailer\PHPMailer\PHPMailer; | |
use PHPMailer\PHPMailer\Exception; | |
require_once 'PHPMailer/src/PHPMailer.php'; | |
require_once 'PHPMailer/src/SMTP.php'; | |
require_once 'PHPMailer/src/Exception.php'; | |
$mail = new PHPMailer(true); | |
$mail->IsSMTP(); | |
//$mail->SMTPDebug = 1; | |
$mail->SMTPAuth = true; | |
$mail->SMTPSecure = 'ssl'; | |
$mail->Host = "email-smtp.us-east-1.amazonaws.com"; | |
$mail->Port = 465; // or 587 | |
$mail->IsHTML(true); | |
$mail->Username = AWS_KEY; | |
$mail->Password = AWS_SECRET_KEY; | |
$mail->SetFrom("you@example.com"); | |
$mail->Subject = "Subject"; | |
$mail->Body = "<h1>Message body.</h1>"; | |
$mail->AddAddress("them@example.com"); | |
if (!$mail->Send()) { | |
echo "Mailer Error: " . $mail->ErrorInfo; | |
} else { | |
echo "Message has been sent."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment