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 !
Related
I'm trying to dynamically change the sender of the email through config but it doesn't work, it always sends from the email configured in the .env.
$config = array(
'driver' => 'smtp',
'host' => 'smtp.googlemail.com',
'port' => '465',
'from' => array('address' => optional($model->account)->email,
'name' => optional($model->account)->from),
'encryption' =>'ssl',
'username' => optional($model->account)->email,
'password' => optional($model->account)->password,
);
Config::set('mail', $config);
Mail::to($customer_email)->send(new DinamicMail());
How can I achieve to send the mail of an account dynamically.
This was the code I had used for multiple emails
// Backup your default mailer
$backup = Mail::getSwiftMailer();
// Setup your gmail or other mailer
$transport = (new \Swift_SmtpTransport(env('MAIL_HOST'), env('MAIL_PORT'), env('MAIL_ENCRYPTION')))
->setUsername(env('MAIL2_USERNAME'))
->setPassword(env('MAIL2_PASSWORD'));
// Any other mailer configuration stuff needed...
$custom_mail = new \Swift_Mailer($transport);
// Set the mailer
Mail::setSwiftMailer($custom_mail);
// Send your message
Mail::to('test#test.com')->send(new InsuranceMail( (object)$mail_data) );
// Mail::to(config('mail.insurance'))->send(new InsuranceMail( (object)$mail_data) );
// Restore your original mailer
Mail::setSwiftMailer($backup);
Note: MAIL2_USERNAME or MAIL_HOST are also added in .env, the rest I think you get the idea
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!
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!
Is there an easy way to "override" the destination of an email using CakeEmail?
I have a model method that send an email using this piece of code:
$Email = new CakeEmail();
$Email->config('default');
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($foo['User']['email'])
->send();
And this configuration:
class EmailConfig
{
public $default = array(
'host' => 'smtp.server.com',
'port' => 587,
'username' => 'user#domain.com',
'password' => 'pwdAsYouKnow',
'from' => array('noreply#domain.com' => 'Company'),
'transport' => 'Smtp'
);
}
As you can see, I send the email for a dynamically defined user's email.
My objective is when I'm developing locally on my machine, to force every call to ->send() to force a destination like developer#domain.com.
I can easily detect if I'm on development, but I don't know how to force in a "master" way to CakeEmail only send to a defined account overriding the one set on the call.
I already tried to set an
'to' => array('developer#domain.com' => 'Dev'),
inside $default but, no success.
I appreciate any help, thanks!
i'm assuming when you are on local machine you run in debug mode, so you can check if debug mode is on then use that to send to different email
if(Configure::read('debug')){
$emailTo = 'developer#domain.com'
}
else{
$emailTo = $foo['User']['email'];
}
then you just use variable for email address:
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($emailTo)
->send();
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.