Zend smtp mailJet template - php

I'm trying to send email on my zend app Via mailjet.
It works but cannot load the desired template.
Code:
$config = array('ssl' => 'ssl',
'port' => 465,
'auth' => 'login',
'username' => 'mailjet api username',
'password' => '8mailjet api key');
$transport = new Zend_Mail_Transport_Smtp('in-v3.mailjet.com', $config);
$mail = new Zend_Mail();
$mail->addHeader('X-MJ-TemplateLanguage',true);
$mail->addHeader('X-MJ-TemplateID','validationV2');
// This is the template I wanna use.(above)
$mail->setFrom('test#test.org', 'You');
$mail->addTo('test#test.org', 'Anybody');
$mail->setSubject('My first email by Mailjet');
$mail->setBodyHtml('wxxxxxxxxxxxxxxxxxxxxxxxxx');
$mail->send($transport);
If I could get any help woudl be great!

You could try putting the numerical ID of the template in X-MJ-TemplateID. If it still doesn't work, file a support ticket

I encountered the same problem. It could be that Mailjet v3 for SMTP Relay is not Zend compatible.
However, the workaround I have tested is to use directly the Mailjet API after importing 'mailjet/mailjet-apiv3-php' via composer, like so :
$mail = [
'FromEmail' => 'test#test.org',
'FromName' => 'You',
'To' => 'Anybody <test#test.org>,
'MJ-TemplateID' => 1,
'MJ-TemplateLanguage' => true
];
$mj = new Client('mailjet api username', '8mailjet api key');
$mj->post(Resources::$Email, ['body' => $mail]);
I'm curious what Mailjet's answer was for your support ticket, however!

Related

Cakephp 3 email with SMTP not working

I am trying to send an email from my CakePHP 3 application. But every time it is using the localhost SMTP and I am getting the error.
So here is my code.
public function sendEmail($email, $subject, $message){
// Sample SMTP configuration.
$this->loadModel('Generalsettings');
$query = $this->Generalsettings->find('all')->where(['meta_key' => 'smtp_details'])->applyOptions(['default' => false]);
$smtpdetail = $query->first();
$detail = json_decode($smtpdetail->value);
Email::configTransport('gmail', [
'host' => $detail['host'], //value is 'ssl://smtp.gmail.com'
'port' => $detail['port'], //value is 465
'username' => $detail['username'],
'password' => $detail['password'],
'className' => 'Smtp'
]);
$emailClass = new Email();
$emailClass->from(['er.dhimanmanoj#gmail.com' => "Sender"])
->to($email)
->subject($subject)
->send($message);
}
Please tell me if I am doing something wrong. Thanks in advance.
You haven't specified the transport you just created using configTransport() method. So it is taking the default settings from config/app.php.
You can setup transport like this:
$emailClass = new Email();
$emailClass->transport('gmail');
NOTE: Deprecated since version 3.4.0: Use setTransport() instead of transport().
For more info please refer to this link #https://book.cakephp.org/3.0/en/core-libraries/email.html
Hope this helps!

How to conditionally change mail transporter in laravel 5?

I use laravel 5.3.
I need to send mail with different credentials (host,port,username, password).
I can send with default laravel config(.env).
But i need dynamic level implementation.
I make array of config,
// Pre-Mail Setup Config.
$store_config = [
'list' =>
//SET 1
['from_name' => 'sender1',
'from_address' => 'from_adderss1',
'return_address' => 'reply1',
'subject' => 'subject1',
'host' => 'host1',
'port' => 'post1',
'authentication' => 'auth1',
'username' => 'uname1',
'password' => 'pass1'],
//SET 2
[.........],
//SET 3
[.........]
];
I try the following to send mail, but it won't work.
// Inside Foreach.
$transporter = \Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($config['username'])
->setPassword($config['password']);
$mailer = \Swift_Mailer::newInstance($transporter);
$message->from($config['from_address'], $config['from_name']);
$message->to('To_Email, 'Name')
->subject('My Subject')
->setBody('My Content', 'text/html');
$mailer->send($message);
What's wrong with my code?
Is it possible?
Or any other solution?
Finally i find the way to solve this.
Actually Laravel 5 is not fully support this multi transporter config.
so i use alternative package to achieve it.
My Code is ,
foreach ($store_configs['list'] as $store_config) {
// Create Custom Mailer Instances.
$mailer = new \YOzaz\LaravelSwiftmailer\Mailer();
$transport = \Swift_SmtpTransport::newInstance(
$store_config['host'],
$store_config['port'],
$store_config['authentication']);
// Assign Dynamic Username.
$transport->setUsername($store_config['username']);
// Assign Dynamic Password.
$transport->setPassword($store_config['password']);
$smtp = new \Swift_Mailer($transport);
$mailer->setSwiftMailer($smtp);
$mailer->send('template', ['data'], function ($message) use ($queue) {
// Default Response goes here
$message->from('From Address', 'From Name');
$message->to($email, 'Name')->subject('My Subject')
->setBody('My HTML', 'text/html');
$message->getSwiftMessage();
//
});
}
Its works fine with multiple and dynamic transporter.
Thanks to All !

send email by Mandrill

I need to send an email for mandrill. This Mandrill API implemented in my project, and send a test mail by apikey provided by the client. I have two options for sending mail, one is by my account of Mandrill and the other is that the user can login by MailChimp. Then insert the API KEY mandrill your account.
I have a default variable in conf file like this:
public $default = array(
'transport' => 'Smtp',
'from' => array('noreply#example.com' => 'Example'),
'host' => 'smtp.mandrillapp.com',
'port' => 587,
'timeout' => 30,
'username' => 'example#example.com',
'password' => '12345678',
'client' => null,
'log' => false
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
To send mail through my account mandrill I do this:
$email = new FrameworkEmail();
$email->config('default');
$email->emailFormat('html');
And I give my account Mandrill data. But if the user chooses to identify with MailChimp and also add your API KEY Mandrill mails should be sent from your account transactional emails and not mine. Any idea if this is possible?
I do not believe this is possible to authenticate with MailChimp and then send through Mandrill. The Mandrill API uses a different set of keys than the MailChimp API, so you won't have access to a user's Mandrill API keys if they're logged in through MailChimp.
EDIT: If you have a user's Mandrill API key, you should be able to feed it directly into the Mandrill send function from the Mandrill SDK:
<?php
$mandrill = new Mandrill('USER_PROVIDED_API_KEY_GOES_HERE');
$message = array(
'html' => '<p>html content here</p>',
// other details here
)
$async = False;
$ip_pool = null;
$send_at = null;
$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
?>
More details on the Message function in the SDK can be found here.
EDIT #2: Same approach can be achieved using CakeEmail - you just need to instantiate the $email class at the time that you receive the user's API key, rather than before.
CakeEmail recommends in a standard setup that you do this in your initial configuration:
class EmailConfig {
public $mandrill = array(
'transport' => 'Mandrill.Mandrill',
'from' => 'from#example.com',
'fromName' => 'FromName',
'timeout' => 30,
'api_key' => 'YOUR_API_KEY',
'emailFormat' => 'both',
);
}
But we can't set this up with default values and then change it per user, using their API key. We need to instantiate this at the time that we receive the user's email, like so:
App::uses('CakeEmail', 'Network/Email');
// PHP code that takes in user email
$user_api_key = // this would come from a form, or from a user's record in the database
$email = new CakeEmail(array(
'transport' => 'Mandrill.Mandrill',
'from' => 'from#example.com',
'fromName' => 'FromName',
'timeout' => 30,
'api_key' => $user_api_key,
'emailFormat' => 'both',
));
// add CakeEmail details like $email->template, $email->subject(), etc.
$email->send();
So the principles are the same regardless of what framework is used. Rather than instantiating Mandrill configuration details in a global setting (like most email frameworks recommend), you will need to instantiate them at the time the user wants to send an email, using user-specific details.

Silex + Swift Mailer not working

I have a Silex app with Swift Mailer, but it seems like the configuration was not loaded from $app['swiftmailer.options'].
I registered the service in my bootstrap file
$app->register(new SwiftmailerServiceProvider());
And in my configuration file
$app['swiftmailer.options'] = array(
'host' => 'smtp.mandrillapp.com',
'port' => '587',
'username' => 'MY_USERNAME',
'password' => 'MY_PASSWORD',
'encryption' => null,
'auth_mode' => null
);
And then I send my email with
$message = \Swift_Message::newInstance()
->setSubject($this->app['forum_name'] . ' Account Verification')
->setFrom(array('no-reply#domain.com'))
->setTo(array('recipient#example.com'))
->setBody('My email content')
->setContentType("text/html");
$this->app['mailer']->send($message);
The send function returns 1 but the email was never sent. But, when I try manually creating an instance of Swift_SmtpTransport, the email would send.
$transport = \Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587)
->setUsername('MY_USERNAME')
->setPassword('MY_PASSWORD');
...
$mailer = \Swift_Mailer::newInstance($transport);
$mailer->send($message);
So the problem is the $app['swiftmailer.options'] is not read or loaded. Am I missing something here?
I'm following the instructions from here.
By default the SwiftmailerServiceProvider uses a spooled transport to queue up emails and sends them all during the TERMINATE stage (after a response is sent back to the client). If you don't call Application->run(), you are bypassing this process. Your mail will stay in the spool and never get sent.
If you want to sent mail outside of the normal Silex flow, you can flush the spool manually with
if ($app['mailer.initialized']) {
$app['swiftmailer.spooltransport']
->getSpool()
->flushQueue($app['swiftmailer.transport']);
}
That's taken directly from the SwiftmailerServiceProvider.
Or you can simply turn off spooling with
$app['swiftmailer.use_spool'] = false;
Try this:
$app->register(new \Silex\Provider\SwiftmailerServiceProvider(), array(
'swiftmailer.options' => array(
'sender' => 'sender',
'host' => 'host',
'port' => 'port',
'username' => 'username',
'password' => 'password'
)
));
It is not in the documentation.

CakePHP mail not working on server

I recently uploaded a CakePHP 2.3.8 app to a Ubuntu 12.04 EC2 instance and now I cannot send emails when using $Email->send() but I can do it with the "fast" method of CakeEmail::deliver('to#gmail.com', 'Subject', 'Content');, however I have a layout that I want to use.
When I try to send an email I get an error stating "Could not send email.
Error: An Internal Error Has Occurred."
Here is my code for sending the email.
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('myemailaddress#gmail.com' => 'Me'));
$Email->to('person#gmail.com');
$Email->subject('Test Email');
$Email->template('layout_1');
$Email->emailFormat('html');
$testvalues = array('item1' => 'test1', 'item2' => 'test2');
$Email->viewVars(array('tesvalues'=> $testvalues));
$Email->send();
$this->Session->setFlash('Email has been sent');
$this->redirect($this->referer(), null, true);
In /App/Config/email.php here is what I have for smtp
public $smtp = array(
'transport' => 'Smtp',
'from' => array('me#gmail.com' => 'Me'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'me#gmail.com',
'password' => '****',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
The exact line that the stack trace flags is in CORE/Cake/Network/Email/MailTransport.php
$this->_mail($to, $email->subject(), $message, $headers, $params);
I checked the error log and it says
Error: [SocketException] Could not send email.
I am going to shoot in the dark here.
It seems to me that you are not setting the layout correctly, according to the documentation you need to tell cake the layout and the view you want to use, for example:
$Email = new CakeEmail();
$Email->template('welcome', 'fancy')
->emailFormat('html')
->to('bob#example.com')
->from('app#domain.com')
->send();
The above would use app/View/Emails/html/welcome.ctp for the view, and app/View/Layouts/Emails/html/fancy.ctp for the layout.
Anyways, I recommend taking a look at Cake logs (app/tmp/logs) and see the cause of your error

Categories