Skip to content

Instantly share code, notes, and snippets.

@Wysie
Last active October 27, 2024 17:33
Show Gist options
  • Save Wysie/03934b6a79a715772abd to your computer and use it in GitHub Desktop.
Save Wysie/03934b6a79a715772abd to your computer and use it in GitHub Desktop.
Upload to Amazon S3 Instead of Server for Sendy Email Image Uploads
//Using S3.php from https://github.com/tpyo/amazon-s3-php-class. Place it in includes/helpers
//Replaces Sendy's includes/create/upload.php
<?php
include('../functions.php');
include('../login/auth.php');
require_once('../helpers/S3.php');
//Init
$file = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$extension_explode = explode('.', $fileName);
$extension = $extension_explode[count($extension_explode)-1];
$time = time();
$allowed = array("jpeg", "jpg", "gif", "png");
if(in_array($extension, $allowed)) {
$awsAccessKey = get_app_info('s3_key');
$awsSecretKey = get_app_info('s3_secret');
$bucketName = 'BUCKETNAME'; //Change accordingly
$endpoint = 's3-ap-southeast-1.amazonaws.com'; //Change accordingly
$s3 = new S3($awsAccessKey, $awsSecretKey, false, $endpoint);
$s3Filename = $time.baseName($fileName);
if ($s3 -> putObject($s3->inputFile($file), $bucketName, $s3Filename, S3::ACL_PUBLIC_READ)) {
$array = array(
'filelink' => 'http://'.$endpoint.'/'.$bucketName.'/'.$s3Filename
);
echo stripslashes(json_encode($array));
}
else exit;
}
else exit;
?>
@wechta
Copy link

wechta commented Oct 16, 2014

Hi, is this solution working with latest version, 1.1.9.5? I cant seem to get it working.. :(

@ssoulless
Copy link

Hi, I have a question, I do not understand how you are getting get_app_info('s3_key');
and get_app_info('s3_secret'); I mean in sendy you do not set this data, please tell me how to proceed. Thanks

@sverdier
Copy link

sverdier commented Aug 19, 2016

for sendy 2.1.0.2
Add in includes/config.php =>

    define('AWS_ACCESS_KEY', 'xxxx');
    define('AWS_SECRET_KEY', 'xxxx');
    define('S3_ENDPOINT', 's3.amazonaws.com');
    define('S3_BUCKET_NAME', 'xxxx');

//Using S3.php from https://github.com/tpyo/amazon-s3-php-class. Place it in includes/helpers
//Replaces Sendy's includes/create/upload.php
=>

<?php
    include('../functions.php');
    include('../login/auth.php');
    require_once('../helpers/S3.php');

    //Init
    $file = $_FILES['upload']['tmp_name'];
    $fileName = $_FILES['upload']['name'];
    $extension_explode = explode('.', $fileName);
    $extension = $extension_explode[count($extension_explode)-1];
    $time = time();
    $allowed = array("jpeg", "jpg", "gif", "png");
    if(in_array($extension, $allowed)) {
        $awsAccessKey = AWS_ACCESS_KEY;
        $awsSecretKey = AWS_SECRET_KEY;
        $bucketName = S3_BUCKET_NAME;
        $s3 = new S3($awsAccessKey, $awsSecretKey, false, S3_ENDPOINT);
        $s3Filename = 'sendy/'.$time.baseName($fileName);
        if ($s3 -> putObject($s3->inputFile($file), $bucketName, $s3Filename, S3::ACL_PUBLIC_READ)) {

            // Required: anonymous function reference number as explained above.
            $funcNum = $_GET['CKEditorFuncNum'] ;
            // Optional: instance name (might be used to load a specific configuration file or anything else).
            $CKEditor = $_GET['CKEditor'] ;
            // Optional: might be used to provide localized messages.
            $langCode = $_GET['langCode'] ;

            $url = 'http://'.S3_ENDPOINT.'/'.$bucketName.'/'.$s3Filename;
            // Usually you will only assign something here if the file could not be uploaded.
            $message = '';

            echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";
        }
        else exit;
    }
    else exit;
?>

@pengjiayou
Copy link

@sverdier It works for Sendy 2.1.2, thank you so much.
BTW, I use DreamObjects instead of S3, as it's compatible.

@planetahuevo
Copy link

Does this work in 2024? :) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment