cakephp 2.0 smtp email - php

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

Related

Why does not change the sending credentials of the email in laravel

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

Cakephp 3 email with SMTP not working

I am trying to send an email from my CakePHP 3 application. But every time it is using the localhost SMTP and I am getting the error.
So here is my code.
public function sendEmail($email, $subject, $message){
// Sample SMTP configuration.
$this->loadModel('Generalsettings');
$query = $this->Generalsettings->find('all')->where(['meta_key' => 'smtp_details'])->applyOptions(['default' => false]);
$smtpdetail = $query->first();
$detail = json_decode($smtpdetail->value);
Email::configTransport('gmail', [
'host' => $detail['host'], //value is 'ssl://smtp.gmail.com'
'port' => $detail['port'], //value is 465
'username' => $detail['username'],
'password' => $detail['password'],
'className' => 'Smtp'
]);
$emailClass = new Email();
$emailClass->from(['er.dhimanmanoj#gmail.com' => "Sender"])
->to($email)
->subject($subject)
->send($message);
}
Please tell me if I am doing something wrong. Thanks in advance.
You haven't specified the transport you just created using configTransport() method. So it is taking the default settings from config/app.php.
You can setup transport like this:
$emailClass = new Email();
$emailClass->transport('gmail');
NOTE: Deprecated since version 3.4.0: Use setTransport() instead of transport().
For more info please refer to this link #https://book.cakephp.org/3.0/en/core-libraries/email.html
Hope this helps!

How to configure CakePHP 3.x to use Amazon SES SMTP to send out emails?

I have successfully created my SMTP credentials in AWS SES.
I have lifted my restrictions.
I have used the command line to test if my credentials are okay.
They are.
I am using CakePHP 3.2 but still unable to send out my emails.
The region I use is US West Oregon. The host is email-smtp.us-west-2.amazonaws.com
How to test if credentials are okay via Command Line
Open up your server terminal and type echo -n "YOUR SMTP USERNAME" | base64
Copy paste the output somewhere. You will need it. The output should end with =
Repeat step 1 and 2 for YOUR SMTP PASSWORD
Copy paste the following into a text file but replace <whatever> as you deem fit.
Like this:
AFTER 220 .... PASTE THE LINE BELOW:
EHLO <example.com>
AFTER 250 Ok PASTE THE LINE BELOW:
AUTH LOGIN
AFTER 334 VXNlcm5hbWU6:
<YOUR SMTP USERNAME encoded as base64 from step 1>
AFTER 334 UGFzc3dvcmQ6:
<YOUR SMTP PASSWORD encoded as base64 from step 3>
AFTER 235 Authentication successful.
MAIL FROM:<yourverifiedemail#example.com>
AFTER 250 Ok
RCPT TO:<yourverifiedemail#example.com>
AFTER 250 Ok
DATA
AFTER 354 End data with <CR><LF>.<CR><LF>
Subject:Hello from Amazon SES!
This email was sent using the Amazon SES SMTP interface.
.
Type openssl s_client -crlf -quiet -connect email-smtp.us-west-2.amazonaws.com:465 into your terminal
Follow the instructions in the text file.
Once ascertained that credentials are good, now configure your cakephp 3.x
Configure Cake 3.x
Open up your config/app.php
Find EmailTransport and add a new transport below default
Like this:
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
],
// START of what you need to add!!
'AWS_SES' =>[
'className' => 'Smtp',
'host' => 'email-smtp.us-west-2.amazonaws.com',
'port' => 587, // this is very important to be 587!!
'timeout' => 30,
'username' => 'YOUR SMTP USERNAME',
'password' => 'YOUR SMTP PASSWORD',
'tls' => true, // this is also very important!!
]
// END of what you need to add!!
],
Now look for Email in app.php and add a new profile below default
Like this:
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'you#localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
// START of what you need to add!
'production' => [
'transport' => 'AWS_SES',
//'log' => true,
]
// END of what you need to add!
],
That's all for configuration!
Just call $email = new Email('production'); at the appropriate place that you want.

Silex + Swift Mailer not working

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.

Can swiftMailer be used on localhost to send emails?

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

Categories