daniel-zahariev / php-aws-ses

PHP classes that interfaces Amazon Simple Email Service
307 stars 100 forks source link

addAttachmentFromFile not working in CLI mode #73

Closed aa6my closed 4 years ago

aa6my commented 4 years ago

hello, sorry for take your time. Attachment not working using CLI, array are empty. It will send an email without attachment.

image image image

daniel-zahariev commented 4 years ago

Hi @aa6my, is the value of $file->path the actual real path with the filename included?

aa6my commented 4 years ago

Hi @aa6my, is the value of $file->path the actual real path with the filename included?

yup, here the censored string for $file->path C:\Users\xxxxxx\Code\xxxxx_xxxxxx

daniel-zahariev commented 4 years ago

Can you check if addAttachmentFromFile returns false?

aa6my commented 4 years ago

yes it is, after i dd($m->addAttachmentFromFile$file->name, $file->path, $file->type)); its show false

aa6my commented 4 years ago

@daniel-zahariev here my extra information

Authentication

$region = SimpleEmailService::AWS_US_EAST_1;
$version = SimpleEmailService::REQUEST_SIGNATURE_V4;
$debug = true;

$ses = new SimpleEmailService(
    $_ENV['AWS_ACCESS_KEY_ID'], 
    $_ENV['AWS_SECRET_ACCESS_KEY'], 
    $region,
    $debug,
    $version
);
$ses->setHost($_ENV['SES_REGION']);

ENV

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
AWS_DEFAULT_REGION=ap-southeast-1
SES_REGION=email.ap-southeast-1.amazonaws.com
aa6my commented 4 years ago

Okay, it works with another method which is addAttachmentFromUrl, here the example

index.php

<?php

require_once 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$recipientName = $_ENV['RECIPIENT_NAME'];
$recipientEmail = $_ENV['RECIPIENT_EMAIL'];

$senderName = $_ENV['SENDER_NAME'];
$senderEmail = $_ENV['SENDER_EMAIL'];

$file = 'https://test.test.asia/test/file_2020-08-26-15-11-32.csv';
$type = wapmorgan\FileTypeDetector\Detector::detectByFilename($file)[2];

$m = new SimpleEmailServiceMessage();
$m->addTo("$recipientName <$recipientEmail>");
$m->setFrom("$senderName <$senderEmail>");
$m->setSubject('Hello, world!');
$m->setMessageFromString('This is the message body.');
$m->addAttachmentFromUrl(basename($file), $file, $type);

$region = SimpleEmailService::AWS_US_EAST_1;
$version = SimpleEmailService::REQUEST_SIGNATURE_V4;
$debug = true;

$ses = new SimpleEmailService(
    $_ENV['AWS_ACCESS_KEY_ID'], 
    $_ENV['AWS_SECRET_ACCESS_KEY'], 
    $region,
    $debug,
    $version
);
$ses->setHost($_ENV['SES_REGION']);

// dd($m);

try {
    $ses->sendEmail($m,$use_raw_request = true);
} catch (Exception $e) {
    echo $e;
}

.env

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
AWS_DEFAULT_REGION=ap-southeast-1
SES_REGION=email.ap-southeast-1.amazonaws.com

RECIPIENT_NAME="Mark R"
RECIPIENT_EMAIL=your@recipient.com

SENDER_NAME="Johnny S"
SENDER_EMAIL=your@sender.com

FILENAME=test.txt

composer.json

{
    "name": "aa6my/php-aws-ses-cli",
    "description": "php-aws-ses in CLI",
    "require": {
        "daniel-zahariev/php-aws-ses": "^0.9.2",
        "vlucas/phpdotenv": "^5.1",
        "wapmorgan/file-type-detector": "^1.1",
        "wanfeiyy/dd": "^1.0"
    }
}

test.txt

test
TomS- commented 3 years ago

Thanks for this, I was running into the same problem and I couldn't figure out why. Using addAttachmentFromUrl worked. Glad I searched closed issues.

EDIT: How come addAttachmentFromUrl isn't documented?

aa6my commented 3 years ago

Thanks for this, I was running into the same problem and I couldn't figure out why. Using addAttachmentFromUrl worked. Glad I searched closed issues.

EDIT: How come addAttachmentFromUrl isn't documented?

Btw, its not documented. So I just check it available & test it working. REF SimpleEmailServiceMessage.php - https://github.com/daniel-zahariev/php-aws-ses/blob/master/src/SimpleEmailServiceMessage.php#L467