OctoberCms send message using SMTP server - laravel

I have some problem. I need send message using SMTP, because I want to set up а password when sending. I read this manual https://octobercms.com/docs/services/mail
But I didn't find how I can set up my password, in this manual sets up only from adress
$message->from('us#example.com', 'October');
Can your help me?

Thanks for your answer. But this solution does not solve my problem. In my system I use two address when sending. I use plugin for mail setting from admin panel. This plugin overwrites my settings from config. Accordingly, I must specify all the settings for the second mail dynamically so that they overwrite the settings of the first mail that the administrator specifies in the admin panel.
Sorry for my English

You need to set SMTP details inside config folder mail.php file config/mail.php * (you can find config folder in root directory where you setup your october CMS)
<?php
return [
'driver' => 'smtp', // drive need to be smtp
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => ['address' => 'noreply#domain.tld', 'name' => 'OctoberCMS'],
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
];
set all details here as you like and start using SMTP
it will automatically use that driver details when you send mail and it will send mail using that SMTP Details
To use multiple mail setting you can use this snippet
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
// Backup your default mailer so we replace it after
// sending our custom mail
$backup = Mail::getSwiftMailer();
// Setup your gmail mailer
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_password');
// other config if needed...
$gmail = new Swift_Mailer($transport);
// Set the mailer as gmail
Mail::setSwiftMailer($gmail);
// Send your message
Mail::send(-- function arguments --);
// Restore your original mailer
Mail::setSwiftMailer($backup);
try this and let me know

$backup = Mail::getSwiftMailer();
$smtp_host_ip = gethostbyname('my_host');
$transport = SmtpTransport::newInstance($smtp_host_ip, 465, 'ssl')->setUsername('my_username')->setPassword('my_password');
$mail = new Swift_Mailer($transport);
Mail::setSwiftMailer($mail);
Mail::send($templateMessage, $params, function($message) use ($userSendMes){
$message->from('my_address_from', 'my_name');
$message->to($userSendMes["attributes"]["email"], $userSendMes["attributes"]["name"]);
});
all connection settings are correct

Related

Laravel 9 Symfony mailer not working - mail sent from app but not received in gmail

In Laravel 8 I was able to send an email verification email. And now in Laravel 9 email sent message appears but I don't receive an email.
I have used my correct mail host provided by domain with port, username, and password as previously and checked enough times. I have also changed ssl to tls but no success.
.env file
MAIL_MAILER=smtp
MAIL_HOST=mail.getalow.com
MAIL_PORT=465
MAIL_USERNAME=********
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS="support#getalow.com"
MAIL_FROM_NAME="${APP_NAME}"
I also added 'verify_peer' => false in mail.php config file but no success.
mail.php
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'verify_peer' => false, // <------ IT HAS TO BE HERE
],
my User.php model also has implements MustVerifyEmail as I do everytime.
class User extends Authenticatable implements MustVerifyEmail
In registration controller event(new Registered($user)); is presented
RegistrationController.php
// Register a new user
public function register(RegistrationRequest $request) {
$user = User::create($request->validated())->assignRole(['User', 'Commenter']);;
event(new Registered($user));
Auth::login($user);
return to_route('home')
->with(Helpers::Toast(
'Welcome ' . $user->name,
'Your account has been created.',
'success',
'5000'
));
}
I tried it from localhost to mailtrap it works. But when I upload it to server with all details of server in env file as above. I receive success message as in registration controller, but I don't receive activation email on my registered email.
I am using shared hosting, cpanel as hosting provider
What am I missing please help. I stucked on it for 4 days.
You said you are using cPanel shared hosting server.
There is no problem in your Laravel 9 code. The problem is on your DNS setting of your email in cPanel. You have to change CNAME, MX, TXT records on cPanel as provided by your hosting provider.
For example:
If you are using ohodomain hosting provider.
Go to account settings.
Select your domain from list of your domains.
Go to Manage Email.
There you will see the error with its solution that you have to solve.
Add the provided CNAME, MX, TXT records on your cPanel's Zone Editor.

Send Mail Using Lumen

Mail.php
return [
'driver' =>'smtp',
'host' => 'smtp.gmail.com',
//'port' => 587,
'port' =>465,
//'encryption' =>'tls',
'encryption' =>'ssl',
'username' => 'xxxxxxxx#gmail.com',
'password' => 'xxxxxxx',
// 'sendmail' => '/usr/sbin/sendmail -bs',
'sendmail' => '/usr/sbin/sendmail -t',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Controller
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Error
Swift_TransportException Connection could not be established with host
smtp.gmail.com [A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
I Tried...
Change ssl / tls
Change the ports
Add "guzzlehttp/guzzle": "~5.3|~6.0" in composer.json
Add a new line in StreamBuffer.php
$options = array_merge($options, array('ssl' => array('verify_peer' =>
false,'verify_peer_name' => false,'allow_self_signed' => true )));
Please help .
Thank you.
1. Require illuminate/mail
Make sure you’re using the same version as your underlying framework (i.e. if you’re on Lumen v. 5.3, use composer require illuminate/mail "5.3.*").
composer require illuminate/mail "5.5.*"
2. Set up Lumen bootstrap/app.php
First, open your bootstrap.php and uncomment the following lines:
$app->withFacades();
$app->register(App\Providers\AppServiceProvider::class);
Also, add the following line below the last line you uncommented:
$app->configure('services');
This will allow you to define a ‘services’ config file and setup your mail service. Now I know that normally configuration is done in the .env file with Lumen, and we’ll use that shortly, but first we’ll need to write a small config file to map to the .env file.
3. Create your configuration files
Create a new folder at the root level of your install called config(if it doesn’t already exist). In the config folder, create two new files, one named services.php and another named **mail.php**.
In the services.php file paste the following:
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
];
Lastly, add the following to your .env file:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=<your-mailgun-domain>
MAILGUN_SECRET=<your-mailgun-api-key>
Make sure you replace those sneaky placeholders with your actual key and domain. If you’re not using Mailgun, you can always use the other mail providers Mail comes with; have a look at the docs if you plan on using a different provider, they are all easy to set up once you’re at this point.
4. Send Email!
To send an email, use one of the following in your classes (depending on your preference):
use Illuminate\Support\Facades\Mail;
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Finally, don’t forget to read the Laravel Mail docs for more info on how to use this great library.
Have you turned on 2 layers security in your Google account (email address you config in .env file) which uses to send email.

Laravel mail from not set

For my website I try to send emails from an alias (created alias from google for business). My mail function:
Mail::send('emails.tracktrace', ['text'=>$mailtext,'tracking' => $code,'email' => $email, 'name' => $name],
function ($m) use ($code, $email, $name) {
$m->from('info#mydomain.eu', 'Mydomain');
$m->to($email, $name)->subject('Track your package!');
});
Also in my config/mail.php I have:
'from' => ['address' => 'noreply#mydomain.eu', 'name' => 'mydomain'],
But both are ignored and the MAIL_USERNAME value set in my .env file (with the mail configuration) is used.
I ran into the same problem.. Turned out I tried to use a Gmail account which doesn't let you change the 'from' headers.
(see problem with php mail 'From' header)

Sending Email in Laravel Using Swift Mailer without Gmail

Hello i'm new to laravel and still getting familiar with certain features, i would like to know how i can send an email from my laravel application without using gmail smtp settings. Like the way the mail function works in basic PHP. Is this possible? I have tried googling it but i have been unable to find a solution, they all use gmail.
Go to your app/config/mail.php
change driver, host and from
'driver' => 'mail',
'host' => '',
'from' => array('address' => 'us#example.com', 'name' => 'Laravel'),
or you can set 'from' within
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});

Sending mail with SwiftMailer in Laravel 4

I am receiving a the following error:
Cannot send message without a recipient
This is while trying to send a email using swiftmailer. My code works in localhost and has all the parameters needed from sender to receiver but it throws an error saying it cannot send without recipient.
Here is my code:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message){
$message = Swift_Message::newInstance();
$email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $msg = $_POST['msg'];
$message = Swift_Message::newInstance()
->setFrom(array($email => $name))
->setTo(array('name#gmail.com' => 'Name'))
->setSubject($subject)
->setBody($msg);
$transport = Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl');
//$transport->setLocalDomain('[127.0.0.1]');
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
if($result){
var_dump('worked');
}else{
var_dump('Did not send mail');
}
}
}
You can do this without adding your the SMTP information in your Mail::send() implementation.
Assuming you have not already, head over to app/config/mail.php and edit the following to suit your needs:
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your_username',
'password' => 'your_password',
Then your code should be as simple as:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message)
{
$message->from( Input::get('email'), Input::get('name') );
$message->to('name#gmail.com', 'Name')->subject( Input::get('subject') );
});
}
So, hopefully the issue is one of configuration. Do you have other environments setup that might be over-riding the app/config/mail.php configuration settings in your server where it's not working?
Please make sure you have configured all the necessary configuration correctly on
/app/config/mail.php. Ensure all the configuration is correct for the environment where the email is not working correctly.
If you want to use your Gmail account as an SMTP server, set the following in app/config/mail.php:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email#gmail.com',
'password' => 'your-password',
When switching to online server, you want to protect this file or switch provider so as not to expose your gmail credentials. Port 587 is for mailgun not gmail.

Resources