I am trying to sent emails with laravel but somehow it does not work I get following message:
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 220 but got code "421", with message "421
Cannot connect to SMTP server 52.20.34.166 (52.20.34.166:587), connect
error 10060 "
my .env file looks like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=rFhHPTfEg4nxYEeqVzoQWX70a0AA5uw3
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=test#test.de
MAIL_PASSWORD=123456789
MAIL_ENCRYPTION=tls
my mail.php looks like this:
<?php
return [
'driver' => env('MAIL_DRIVER', 'mail'),
'host' => env('MAIL_HOST', ''),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'test#test.de', 'name' => 'test'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
And inside of my Controlller I called it like this:
# Send Mail
Mail::send('emails.email', $data, function($message) use ($data)
{
$message->from($data['email'] , $data['title']);
$message->to('test#test.de', 'my name')->subject('contact request');
});
I cannot find the mistake in my code. Thanks for help.
Other thing I notices that mailtrap gives you specific username and password. Did you try to use them instead of test#test.de;123456789 ?
You need put a valid email on .env, find your personal mail configuration and try again with this.
Maybe you want to change the config from on config/mail.php to show your name and mail.
I say this because I see that you have the host mailtrap.io, but test#test.de is not a valid user for this host.
And maybe 587 to the MAIL_PORT is not the correct value.
You need to use this command:
php artisan config:cache
It works for it before your changed env file and type this command and run it. It will work.
Related
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.googlemail.com',
'smtp_port' => 587,
'smtp_user' => 'example#gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->email->from('example#gmail.com', 'easyfact');
$this->email->to('example#gmail.com');
$this->email->cc('example#gmail.com');
$this->email->bcc('example#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
reulst website
Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
and
Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
i use wamppserver helf me
You are not using the $config variable anywhere. Call
$this->email->initialize($config);
That should solve your problem.
See also: Setting Email Preferences part of Codeigniter Email class documentation.
I'm using Cake\Network\Email\Email class to send some emails.
I have successfully send an email with my smtp configuration that I put on config/app.php
The problem is that I don't want the config to be there.
I have this piece of code:
function enviacorreo($email, $asunto, $content, $cc=null) {
Email::configTransport('gmail', [
'port'=>$smtpport,
'timeout'=>'30',
'host' => $smtp,
'username'=>$smtpuser,
'password'=>$smtppasswd
]);
$email_obj = new Email();
$email_obj->template('default')
->emailFormat('html')
->to($email)
->from([$smtpuser => $nombreSistema])
->subject($asunto)
->transport('gmail');
if($email_obj->send($content))
return array('exito'=>1,'error'=>'Ninguno');
else
return array('exito'=>0,'error'=>'El correo no pudo ser enviado');
}
I'm following the official book here
The error that I get is this:
Transport config "gmail" is missing.
I know that ->transport('gmail'); is looking for a key inside the EmailTransport array definded in app.php
But. How can I have this 'configuration' in my code?
Hope to explained well.
The error message is a little misleading, as the exception is not only being thrown in case the configuration missing, but also in case the className option is missing, which is the case in your example (#7204).
If you look closely at the docs, you should see that you haven't followed it correctly.
[...]
// Sample smtp configuration.
Email::configTransport('gmail', [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'className' => 'Smtp' // <------ there it is
]);
[...]
Cookbook > Email > Configuring Transports
I can send email configuring smtp options from email.php file and it works fine. But I want to send emails, where the smtp options, like host, port, username and password are taken from database.
I tried to use this, but does not work, just gives an error No connection could be made because the target machine actively refused it. The smtp options are correct.
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('smtp');
$Email->smtpOptions = array(
'host' => $smtpAccount['host'],
'port' => $smtpAccount['port'],
'username' => $smtpAccount['username'],
'password' => $smtpAccount['password'],
);
$Email->to($email);
$Email->template('sending')->emailFormat('both');
$Email->subject($subject);
$Email->viewVars (
array(
'content' => $content
)
);
return $Email->send();
trying to set the host like $Email->host($smtpAccount['host']);
gives an error Call to undefined method CakeEmail::host()
Thanks
instead of $Email->smtpOptions should be used
$Email->config(array(
'host' => $smtpAccount['host'],
'port' => $smtpAccount['port'],
'username' => $smtpAccount['username'],
'password' => $smtpAccount['password'],
));
I figured that I'll try to find out if I can send mails via cmd line before writing a question here. So I followed these steps and sent an e-mail by cmd to myself and a collegue of mine who's working on the same app I am. The message from cmd line came in the format it should have come.
( internal app, should send mails only to my collegues' OUTLOOK accounts )
Everything seems to be connected fine so I figured I have an error in my cakePHP code:
Everything on mails code I have so far is this:
app/config/email.php
public $smtp = array(
'transport' => 'Smtp',
'from' => array('from#test.sk'),
'host' => 'ip_address_of_my_host',
'port' => 25,
'timeout' => 30,
'username' => 'from#test.com',
'password' => 'password',
'client' => null,
'log' => true,
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
UsersController.php (test send mail function)
public function test_send_email() {
$this->autoRender = false;
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('smtp');
$email->from(array('from#test.com' => 'APP TEST'));
$email->to('me#test.com');
$email->subject('Subject of testing');
$email->send('Message of testing');
}
After 5 seconds of loading I get: SMTP Error: 504 5.7.4 Unrecognized authentication type
Solved:
I guess the authentication wasnt required on EXCHANGE, so I just removed the password parameter and works fine.
During research I found out this article, so the next step would be to contact EXCHANGE admins.
http://blogs.technet.com/b/exchange/archive/2006/12/28/3397620.aspx
I was getting the same error when using telnet on port 25. After changing to port 587, I was able to use authenticated SMTP.
I am able to send SMTP Emails from my local server machine in CakePHP while I am not able to do the same on my GoDaddy live server, in CakePHP.
Any ideas for the same?
Thanks
Answer is below as per my experience with GoDaddy:
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
Update your code to check for an error message:
if(!$this->Email->send()) {
CakeLog::write('debug', $this->Email->smtpError);
}
Then check the /app/tmp/logs/debug file on the server.