Sending mail with SwiftMailer in Laravel 4 - laravel

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.

Related

Mailer Error: SMTP Error: Could not connect to SMTP host

How to resolve this error? I do all possible things to resolve this issue but it cannot resolve. Here is my .env file code
MAIL_DRIVER=smtp
MAIL_HOST=fluorine.cloudhosting.co.uk
MAIL_PORT=465
MAIL_USERNAME=contacts#cleansafeltd.com
MAIL_PASSWORD=Hggjgjgghv123
MAIL_ENCRYPTION=ssl
Here is the code of mail.php.
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'fluorine.cloudhosting.co.uk'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'contacts#cleansafeltd.com'),
'name' => env('MAIL_FROM_NAME', 'Contact'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Here is the mailController
public function sendMail($to_email, $template_id, $users_insertId = 0) {
set_time_limit(0);
$mail = new PHPMailer(true);
$mail->IsSendmail();
$mail->IsSMTP(true);
$mail->Host = "fluorine.cloudhosting.co.uk";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "website#cleansafeltd.com";
$mail->Password = "Star9000!";
$mail->Port = "465";
$mail->IsHTML(true);
}
I do all thin with tls or port 587 also but not solved.
Using your credentials (by the way, you shouldn't post your password in public places, you better change it), I was able to send myself a mail using the following code
Mail::to('mymail#domain')->send(new \App\Mail\TestMail);
\App\Mail\TestMail is a Mailable class, defined as follows
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
// Optional params are commented out.
// replyTo changes the address one gets when clicking the reply button
public function build()
{
return $this->from(env('MAIL_USERNAME'), 'An alias name')
// ->to('mail#domain', 'alias name for recipient')
// ->cc('mail2#domain', 'alias name for cc')
// ->bcc('mail3#domain', 'alias name for bcc')
// ->replyTo('mail4#domain', 'alias for replyTo')
->subject('This is a test')
->view('mails.sample_mail'); // This is a laravel view.
}
}
Try this one:
Setup your gmail account
Go to settings -> Forwarding and POP/IMAP
- Check -> POP download: Enable POP for all mail (even mail that's already been downloaded)
- Check -> IMAP access: Enable IMAP
- Save
Go to Google Account -> security -> Less secure app access
Set to on instead of off
Set this to your .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=your email username
MAIL_PASSWORD=your email password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=your email address
MAIL_FROM_NAME=Your email name
Now you can send email with your smtp gmail account

In plesk server Codeigniter Mail Function not working with gmail on server side but good in localhost?

Everything going great on Codeigniter localhost but on particuler Plesk Linux Server I get the different type of error as per changing the port number and protocols. I'm really hanging on this problem, also read the documentation of Plesk but I didn't understand. Hope on StackOverflow someone gives a solution.
I tried most of the all port and protocols to get the result but not succeded. Used code
function sendMail()
{
$this->load->library('email');
// ini_set('SMTP', "smtp.rediffmailpro.com");
// ini_set('smtp_port', "25");
// ini_set('sendmail_from', "myemailid");
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$config = array(
// for rediff mail setup
// 'protocol' => 'smtp',
// 'smtp_host' => 'smtp.rediffmailpro.com',
// 'smtp_port' => 587,
// 'smtp_user' => 'email.com',
// 'smtp_pass' => 'pass',
// 'mailtype' => 'html',
// 'charset' => 'iso-8859-1', //'UTF-8'
// 'wordwrap' => TRUE
// for gmail setup
'protocol' => 'smtp', //'mail', 'sendmail', 'tls' tried these but get different type of errors
'smtp_host' => 'smtp.gmail.com', /*'smtp.googlemail.com',*/
'smtp_port' => 587, //465
'smtp_user' => 'user email',
'smtp_pass' => 'user pass',
//'smtp_crypto' => 'smtp',
'mailtype' => 'html',
'smtp_timeout' => 5,
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->from('email','AG'); // change it to yours
$this->email->to('your email');// change it to yours
$this->email->subject('Test Mail');
$this->email->message('Dont be pannic.');
$this->email->set_newline("\r\n");
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
At this condition I get this error
Failed to authenticate password. Error: 534-5.7.14 Please log
534-5.7.14 in via your web browser and then try again. 534-5.7.14
Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754
k11sm2916535wmk.45 - gsmtp Unable to send email using PHP SMTP. Your
server might not be configured to send mail using this method.
But on given google url didn't get any idea about my problem or I didn't get it.
After changing
port 465,
smtp_hot "ssl://email.com",
Get this error
Failed to authenticate password. Error: 534-5.7.14 Please log
534-5.7.14 in via your web browser and then try again. 534-5.7.14
Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754
y66sm2047426wmd.37 - gsmtp Unable to send email using PHP SMTP. Your
server might not be configured to send mail using this method.
I don't understand what am I doing wrong.
Want to successfully send mail. On Plesk server

i cant send mail codeigniter

I can't send mail from codeigniter to yandex mail when i using this code:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.yandex.com',
'smtp_port' => 465,
'smtp_user' => 'blablabla#yandex.com',
'smtp_pass' => 'blablabla',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'newline' => "\r\n"
);
$this->load->library('email');
$this->email->initialize($config);
$msg = $this->messages($f2, $f3);
$this->email->to($f1);
$this->email->subject('Here is Subject');
$this->email->message($msg);
if($this->email->send()){
echo "Masuk!";
}else{
echo "Gagal!";
}
echo $this->email->print_debugger();
And the result always says:
Gagal!Cannot send mail with no "From" header.
Cannot send mail with no "From" header.
You simply need to add a From address to your email, by calling
$this->email->from('your#example.com', 'Your Name');
or
$this->email->from('your#example.com');
Depending on the sending and receiving provider, that might get you in trouble though if you try and send the email under another From address, than the one you authenticated against the SMTP server with - it might get classified as spam then (or even rejected by the SMTP server right away) - so in this case here you should use blablabla#yandex.com as From.
you have to set a name and email as a sender.
for example if you set different email and name for sender it shows in inbox mail (Gmail for example) as like below image

OctoberCms send message using SMTP server

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

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');
});

Resources