I need to send mails from multiple mails
Ex. support#email.com, info#email.com
in .env file I entered my default settings
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mymail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
I use markdown email
public function build()
{
return $this->markdown('emails.users.resetpassword',[
'url' => url(config('app.url').route('password.reset', $this->token, false)),
'user' => $this->notifiable,
]);
}
this works fine
I try to change sender mail on the fly
Config::set('mail.encryption','ssl');
Config::set('mail.host','smtps.example.com');
Config::set('mail.port','465');
Config::set('mail.username','youraddress#example.com');
Config::set('mail.password','password');
Config::set('mail.from', ['address' => 'youraddress#example.com' , 'name' => 'Your Name here']);
but it still sending from main account!!
how can I change the sender mail ?
thanks in advance
You can override the config during runtime
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
$transport = (new SmtpTransport(config('mail.host_other'), config('mail.port_other'), config('mail.encryption_other')))->setUsername(config('mail.username_other'))->setPassword(config('mail.password_other'));
$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
Mail::send(...);
In your mail.php config file, add
<?php
return [
....
....
'host_other' => env('HOST_OTHER'),
'port_other' => env('PORT_OTHER'),
'encryption_other' => env('MAIL_ENCRYPTION_OTHER'),
'username_other' => env('MAIL_USERNAME_OTHER'),
'password_other' => env('MAIL_PASSWORD_OTHER'),
....
....
]
Get more information here https://swiftmailer.symfony.com/docs/sending.html
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
The application has no problem, I do not change the configuration.
A month later i tried the program gets an error.
Error messages :
Swift_TransportException in StreamBuffer.php line 269: Connection
could not be established with host smtp.gmail.com [ #0]
This configuration of the env:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mr.xxxxxxx#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
This configuration of mail.php :
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 465),
'from' => ['address' => 'muhamadramadhan95#gmail.com', 'name' => 'Ramadhan'],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
Please help, thanks a lot.
You can use sendgrid. Its very simple.
step-1:
Add SendGrid to your composer.json
"require":
{
"sendgrid/sendgrid": "~6.0"
}
step-2:
in .env file set your sendgrid api key
SENDGRID_API_KEY= Your Sendgrid API key
step-3:
Add following code in your controller
$from = new \SendGrid\Email(null, "your email id");//place senders email id
$subject = "checking Email service"; //*your subject goes here*
$to = new \SendGrid\Email("Example User", 'example#gmail.com'); //*place reciever email id*
$content = new \SendGrid\Content("text/html", $otp);
$mail = new \SendGrid\Mail($from, $subject, $to, $content);
$apiKey = env('SENDGRID_API_KEY');// set in .env file
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
return json_encode(['code' => 200, 'status' => 'Success', 'message' => 'mail sent Sucessfully]);
for better understanding follow below link
https://github.com/sendgrid/sendgrid-php
Try using mailgun. Here i've post the steps to use it:
Step 1:
Get the Mailgun API, Sign up to Mailgun, Add Your Domain
Step 2: Configure Laravel Application
In your config/services.php file, add the following:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
Next, you will need to go to your .env file, and replace the “MAIL_USERNAME”, “MAIL_PASSWORD”, “MAILGUN_DOMAIN” AND “MAILGUN_SECRET” with your own.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
MAIL_USERNAME=postmaster#yourdomain.com
MAIL_PASSWORD=yourmailgunpassword
MAIL_ENCRYPTION=null
MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=key-YourMailGunSecret
Last step, in the same file, you will need to also add, and replace the value with your own:
MAIL_FROM_ADDRESS: hello#yourdomain.com
MAIL_FROM_NAME: John
That's it! Hope this will helps you!
Try changing encryption and port in your .env file:
MAIL_ENCRYPTION=tls
MAIL_PORT=587
and then run:
$php artisan config:clear
Or, If that doesn't work either then try the hack below:
Add the following lines to _establishSocketConnection() method in Swift/Transport/StreamBuffer.php on line 263:
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
Note: The hack specified above is just a workaround and could be overwritten anytime the Swift package updates. So keep that in mind if you try to use this method.
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();
My code is like this :
public function sendMail(array $data)
{
$data = explode('#', $data['id']);
$email_from = Auth::user()->email;
$email_to = $data[4];
$subject = 'Send Email Test';
$data_user = ['user_name' => $data[1], 'full_name' => $data[2].' '.$data[3] ];
$sent = Mail::send('backend.auth.success_approved', $data_user, function ($mail) use ($email_to, $email_from, $subject)
{
$mail->from($email_from)
->to($email_to)
->subject($subject);
});
}
My configuration in mail.php :
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'ssl://secure.emailsrvr.com'),
'port' => env('MAIL_PORT', 465),
'from' => ['address' => 'myemail#chel.com', 'name' => 'myname'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'myemail#chel.com'),
'password' => env('MAIL_PASSWORD', 'mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
There is error message :
Swift_TransportException in StreamBuffer.php line 265: Connection could not be established with host ssl://secure.emailsrvr.com [php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found. #0].
How to solve this problem?
Thank you.
From the error message I believe that for some reason the domain (secure.emailsrvr.com) of the mail server cannot be resolved.
If you are on a shared hosting you should ask your hosting provider, if you are on a dedicated server or vps you should ping the hostname and see if it can be resolved.
I've done the following things, it worked for me.
Create an email account on the server. Now we have MAIL_USERNAME and MAIL_PASSWORD.
Make the following changes to create global variables in .env file of Laravel framework.
MAIL_DRIVER=smtp
MAIL_HOST=yourhost
MAIL_PORT=465
MAIL_ENCRYPTION=ssl
MAIL_USERNAME=youremail#something.com
MAIL_PASSWORD=yourpassword
Or else you can add the above changes in your config/mail.php. It'll work.
some email parameters in laravel 5+, are defined in .env file, sometimes laravel dont recognize other parameters outside .env file,
check first if your parameters are sent, if not try to send an email to your personal account and try to change the parameters in the .env file
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