I'm using AWS SDK for PHP (https://github.com/aws/aws-sdk-php) to send emails using Amazon SES.
Here's the code:
<?php
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
$client = SesClient::factory(array(
'key' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'region' => 'eu-west-1'
));
$result = $client->sendEmail(array(
// Source is required
'Source' => 'Télécom Co <email#address.com>',
// Destination is required
'Destination' => array(
'ToAddresses' => array('Grégory Smith <another_email#address.com>')
),
// Message is required
'Message' => array(
// Subject is required
'Subject' => array(
// Data is required
'Data' => 'The subject',
'Charset' => 'utf-8',
),
// Body is required
'Body' => array(
'Text' => array(
// Data is required
'Data' => 'The message',
'Charset' => 'utf-8',
)
),
)
));
?>
The problem is that in the email clients "Télécom" appears like "T�l�com" and "Grégory" like "Gr�gory".
Are there any solutions for this problem?
Here's the solution:
<?php
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
$client = SesClient::factory(array(
'key' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'region' => 'eu-west-1'
));
$from_name = base64_encode("Télécom Co");
$from = "=?utf-8?B?$from_name?= <email#address.com>";
$to_name = base64_encode('Grégory Smith');
$to = array("=?utf-8?B?$to_name?= <another_email#address.com>");
$result = $client->sendEmail(array(
// Source is required
'Source' => $from,
// Destination is required
'Destination' => array(
'ToAddresses' => $to
),
// Message is required
'Message' => array(
// Subject is required
'Subject' => array(
// Data is required
'Data' => 'The subject',
'Charset' => 'utf-8',
),
// Body is required
'Body' => array(
'Text' => array(
// Data is required
'Data' => 'The message',
'Charset' => 'utf-8',
)
),
)
));
?>
Related
Currently I'm sending email through AWS SDK SES signature 3 and got an email from Amazon to upgrade it to SES signature version 4. But where to add signature in AWS SDK? below is the current code that is being used to send emails.
<?php
//SES
$SESCredentials = array(
'key' => awsSESAccessKey,
'secret' => awsSESSecretKey
);
$SESClient = new SesClient([
// 'profile' => 'default',
'version' => AWS_SDK_VERSION,
'region' => AWS_REGION,
'http' => [
'verify' => AWS_CERT_PATH
],
'credentials' => $SESCredentials
]);
$result = $SESClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body
]
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
]
]
]);
?>
You can use this package as it supports signature 4
https://github.com/daniel-zahariev/php-aws-ses
$signature_version = SimpleEmailService::REQUEST_SIGNATURE_V4;
$ses = new SimpleEmailService('AccessKey', 'SecretKey', $region_endpoint,
$trigger_error, $signature_version);
$m->addTo('Recipient Name <recipient#example.com>');
$m->setFrom('Sender <user#example.com>');
$m->setSubject('Hello, world!');
$m->setMessageFromString('This is the message body.');
$ses = new SimpleEmailService('AccessKey', 'SecretKey');
print_r($ses->sendEmail($m));
I can't find what's wrong with the attachment. The email is sent empty.
This is for drupal 7 customized webform. Here is my code:
<?php
module_load_include('inc', 'print_pdf', 'print_pdf.pages');
$file_content = module_invoke('print_pdf', 'generate_path', '/****.pdf');
$attachment = array(
'filecontent' => $file_content,
'filename' => '****.pdf',
'filemime' => 'application/pdf',
'list' => TRUE
);
$message = array(
'headers' => array('Content-Type' => 'text/html'),
'key' => 'test',
'to' => '****#****.com',
'from' => '****#****.com',
'attachments' => $attachments,
'subject' => 'Test email',
'body' => 'test'
);
$system = drupal_mail_system('mimemail', 'test');
$system->format($message);
$result = $system->mail($message);
?>
Try to check path and access to file.
I use this code to send unmanaged files as attachments with Mimemail and MailSystem:
/**
* Send email with attachment
*/
mymodule_send('default', array(
'params' => array(
'subject' => 'Mail suject',
'body' => '<p>Mail body</p>',
'attachments' => array(
array(
'filename' => 'HumanreadableFileName.xls',
'filemime' => 'application/vnd.ms-excel', // xls mime
'filepath' => 'public://2016-04-18_report.xls', // path to file
),
),
),
), 'john#doe.com');
/**
* Implements hook_send().
*/
function mymodule_send($key, $params = array(), $to = NULL, $from = NULL) {
if (!valid_email_address($to)) {
$to = variable_get('site_mail', ini_get('sendmail_from'));
}
drupal_mail('snabmail', $key, $to, language_default(), $params, $from);
}
<?php
namespace api\models;
require '../../vendor/autoload.php';
use Yii;
use Aws\Ses\SesClient;
class Mail
{
public function sendSesEmail($to, $subject, $body="", $bodyHtml="")
{
try
{
$client = SesClient::factory(array(
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => array(
'key' => '**********',
'secret' => '**********',
),
));
$emailSentId = $client->sendEmail(array(
// Source is required
'Source' => 'test#test.com',
// Destination is required
'Destination' => array(
'ToAddresses' => array($to)
),
// Message is required
'Message' => array(
// Subject is required
'Subject' => array(
// Data is required
'Data' => 'SES Testing',
'Charset' => 'UTF-8',
),
// Body is required
'Body' => array(
'Text' => array(
// Data is required
'Data' => 'My plain text email',
'Charset' => 'UTF-8',
),
'Html' => array(
// Data is required
'Data' => '<b>My HTML Email</b>',
'Charset' => 'UTF-8',
),
),
),
'ReplyToAddresses' => array( 'replyTo#email.com' ),
'ReturnPath' => 'bounce#email.com'
));
return $emailSentId;
}
catch (SesException $exec)
{
echo $exec->getmessage();
}
}
}
?>
Getting this error when i use send mail function:
Aws\Ses\Exception\SesException
Error executing "SendEmail" cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
↵
Caused by: GuzzleHttp\Exception\RequestException
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Is it just me or is there almost no documentation on how to do this? Mandrill's site isn't much help. Anyway...here's the problem...
try {
$mandrill = new Mandrill('API_KEY');
$attachment = file_get_contents("../template.xls");
$attachment_encoded = base64_encode($attachment);
$message = array(
'html' => '<p>Some html text</p>',
'text' => 'Some text',
'subject' => 'Subject',
'from_email' => 'email#domain.com',
'from_name' => 'Name',
'to' => array(
array(
'email' => 'myemail#domain.com',
'name' => 'Name',
'type' => 'to'
)
),
'attachments' => array(
array(
'type' => 'application/vnd.ms-excel',
'name' => 'New_Features_Submission.xls',
'path' => $attachment_encoded
)
)
);
$async = false;
$result = $mandrill->messages->send($message, $async);
print_r($result);
} catch (Mandrill_Error $e) {
echo 'A Mandrill error occured: ' . get_class($e) . '-' . $e->getMessage();
throw $e;
}
The email sends correctly, but I can't even open the .xls file. I tried using 'application/xls' for the type, that didn't work either. I'm not familiar with encoding, help please!
This line:
'path' => $attachment_encoded
Needs to be this:
'content' => $attachment_encoded
I want to use the decorator to send custom messages to users.
For some reason, the same message gets send.
Why?
$replacements = array();
$replacements['f1#d.net'] = array(
'{v1}' => 'valoare1',
'{v2}' => 'valoare2',
);
$replacements['f2#d.net'] = array(
'{v1}' => 'valoare21',
'{v2}' => 'valoare22',
);
$replacements['f3#d.net'] = array(
'{v1}' => 'valoare31',
'{v2}' => 'valoare32',
);
$replacements['f4#d.net'] = array(
'{v1}' => 'valoare41',
'{v2}' => 'valoare42',
);
$replacements['f5#d.net'] = array(
'{v1}' => 'valoare51',
'{v2}' => 'valoare52',
);
echo count($replacements);
$decorator = new \Swift_Plugins_DecoratorPlugin($replacements);
$mailer = \Swift_Mailer::newInstance(
\Swift_SmtpTransport::newInstance('smtp', 25)
);
$mailer->registerPlugin($decorator);
$message = \Swift_Message::newInstance()->setSubject('title {v1}')->setBody('layout {v2}');
foreach ($replacements as $email => $replacement) {
$message->setFrom(array($email => 'to me'));
// $message->setTo($email);
$message->addTo($email);
$mailer->send($message);
}
Add following under your component in confi/main.php file.
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport'=>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'host_name',
'username' => 'user_name',
'password' => 'password',
'port' => '587',
'encryption' => 'tls'
],
]
And send mail using
\Yii::$app->mailer->compose()
->setHtmlBody("mail_content")
->setFrom('from_email_id')
->setTo('to_email_id')
->setSubject("Subject")
->send();