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
Related
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 !
I am trying to send email by smtp options using zend/Mail
Here is my code:
//use the base class
use Zend\Mail;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
and here is is my mail functionality code in controller
//mail functionality
$email = "xxxx#gmail.com";
$pas = "xxxx";
// setup SMTP options
$options = new Mail\Transport\SmtpOptions(array(
'name' => 'localhost',
'host' => 'smtp.gmail.com',
'port'=> 587,
'connection_class' => 'login',
'connection_config' => array(
'username' => $email,
'password' => $pas,
'ssl'=> 'tls',
),
));
$this->renderer = $this->getServiceLocator()->get('ViewRenderer');
$content = $this->renderer->render('patient-register/email/template', null);
// make a header as html
$html = new MimePart($content);
$html->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($html,));
// instance mail
$mail = new Mail\Message();
$mail->setBody($body); // will generate our code html from template.phtml
$mail->setFrom('xxxx#xxxx.com','mub');
$mail->setTo('xxxx#gmail.com');
$mail->setSubject('TESTING THE EMAIL ACCCOUNT');
$transport = new Mail\Transport\Smtp($options);
$transport->send($mail);
So when i try to send mail from my localhost, its get sucess
But when i try to send the email from the remote server, 128.199.123.145 and the server is a centos server from digitalocean.
Is that anything to change in remote server.
Thanks,
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.
I am currently not receiving emails using the below configuration and was wondering if's it something to do with my set up maybe missing something or it doesnt work on MAMP localhost?
main-local.php in common config directory
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
And then to send the email (which does display a success message)
public function submitreview($email)
{
//return Yii::$app->mailer->compose(['html' => '#app/mail-templates/submitreview'])
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->title)
->setTextBody($this->description)
->attachContent($this->file)
->send();
}
You can send mail through localhost in Yii2 with following config.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'ENTER_EMAIL_ADDRESS_HERE',
'password' => 'ENTER_PASSWORD',
'port' => '587',
'encryption' => 'tls',
],
]
and in your controller
\Yii::$app->mail->compose('your_view', ['params' => $params])
->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
->setTo('to_email#xx.com')
->setSubject('This is a test mail ' )
->send();
I simply m using gmail for testing, used this php file to send mail from local host.
When you're going for production, replace the transport file with your original credentials.
the $result will echo 1, if the mail is successfully sent
<?php
$subject="Testing Mail";
$body="<h2>This is the body</h2>";
$to="*******#gmail.com"; //this is the to email address
require_once 'swiftmailer/swift_required.php';
// Create the mail transport configuration
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('######')->setPassword('######');
//$transport = Swift_MailTransport::newInstance();
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom(array('#######gmail.com'))
->setTo(array($to))
->setBody($body,'text/html');
//send the message
$mailer = Swift_Mailer::newInstance($transport);
$result=$mailer->send($message);
?>
When useFileTransport is set to true (Default in development environment) then mails are saved as files in the 'runtime' folder.
For example, if you are using the advanced starter template and signup as a user when in the backend of the site (And using an extension that sends user registration emails), the registration email will be saved in /backend/runtime/mail
I am trying to send a email message using CakePhp 2.0. in my controller i use this code (i know it's fine, i took it from the cookbook) :
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail("myConfig");
$email->from(array('from#example.com' => 'From Example'));
$email->to($to);
$email->subject($msgtitle);
$ok = $email->send($content);
and in app/config/email.php i have this config :
<?php
class EmailConfig {
public $myConfig = array(
'host' => 'mail.myServer.com',
'port' => 587,
'username' => 'mYaccount',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
the problem is the server answers with :
SMTP Error: 530 5.7.0 Must issue a STARTTLS command first.
the account name is correct, as is the password. The config works when loading it up in thunderbird, the connection to the smtp server is set up as :
server name : mail.myServer.com
port : 587
connection security : STARTTLS
authentication : normal password
user name : mYaccount
Are you certain your SMTP supports tls? Try sending the ehlo command:
telnet 1.2.3.4 25
ehlo testing
You should see something like:
250-STARTTLS
in the list.
If you see it, then it is most likely not enabled. You will need to enable it. If you do not see it, you will need to add it.
public $smtp = array(
.................,
'tls' => true
);
Below code is working for me over GoDaddy server using CakePHP SMTP Email:
Email.php file inside config folder - CakePHP 2.4 MVC version:
// for Live Server GoDaddy.com domain
public $smtp = array(
'transport' => 'Smtp',
'host' => 'ssl://smtpout.asia.secureserver.net', <-- important
'port' => 465, <-- important
#'timeout' => 30,
'username' => 'no-reply#godaddy-domain.com',
'password' => 'password',
#'tls' => false,
#'log' => false,
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
And here is the controller file code below:
// Controller Code to Send Actual Email
// email configuration
$Email = new CakeEmail('smtp');
$Email->from(array('no-reply#godaddy-domain.com' => 'App Name'))
->sender('no-reply#godaddy-domain.com', 'App Name')
->to(array($email))
->bcc(array('xyz#xyz.com'))
->subject('Test Email from GoDaddy')
->emailFormat('both')
->send($hash.'<br><strong>My</strong> message 45 قبل الميلاد، مما يجعله أكثر من');
Hope it helps !
Thanks
From the CakePHP Cookbook:
You can configure SSL SMTP servers, like GMail. To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. Example:
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
(...)
Give the following a try:
<?php
class EmailConfig {
public $myConfig = array(
'host' => 'ssl://mail.myServer.com',
'port' => 465,
'username' => 'mYaccount',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
Make sure your
php_openssl.dll
extension is running.
You can check it on thephp.ini file.
If you are using XAMPP php.ini should be on C:\xampp\php
php.ini:
;extension=php_oci8.dll ; Use with Oracle 10gR2 Instant Client
;extension=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client
extension=php_openssl.dll
;extension=php_pdo_firebird.dll