zend mail smtp options - php

I am trying to send email by smtp options using zend/Mail
Here is my code:
//use the base class
use Zend\Mail;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
and here is is my mail functionality code in controller
//mail functionality
$email = "xxxx#gmail.com";
$pas = "xxxx";
// setup SMTP options
$options = new Mail\Transport\SmtpOptions(array(
'name' => 'localhost',
'host' => 'smtp.gmail.com',
'port'=> 587,
'connection_class' => 'login',
'connection_config' => array(
'username' => $email,
'password' => $pas,
'ssl'=> 'tls',
),
));
$this->renderer = $this->getServiceLocator()->get('ViewRenderer');
$content = $this->renderer->render('patient-register/email/template', null);
// make a header as html
$html = new MimePart($content);
$html->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($html,));
// instance mail
$mail = new Mail\Message();
$mail->setBody($body); // will generate our code html from template.phtml
$mail->setFrom('xxxx#xxxx.com','mub');
$mail->setTo('xxxx#gmail.com');
$mail->setSubject('TESTING THE EMAIL ACCCOUNT');
$transport = new Mail\Transport\Smtp($options);
$transport->send($mail);
So when i try to send mail from my localhost, its get sucess
But when i try to send the email from the remote server, 128.199.123.145 and the server is a centos server from digitalocean.
Is that anything to change in remote server.
Thanks,

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 conditionally change mail transporter in laravel 5?

I use laravel 5.3.
I need to send mail with different credentials (host,port,username, password).
I can send with default laravel config(.env).
But i need dynamic level implementation.
I make array of config,
// Pre-Mail Setup Config.
$store_config = [
'list' =>
//SET 1
['from_name' => 'sender1',
'from_address' => 'from_adderss1',
'return_address' => 'reply1',
'subject' => 'subject1',
'host' => 'host1',
'port' => 'post1',
'authentication' => 'auth1',
'username' => 'uname1',
'password' => 'pass1'],
//SET 2
[.........],
//SET 3
[.........]
];
I try the following to send mail, but it won't work.
// Inside Foreach.
$transporter = \Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($config['username'])
->setPassword($config['password']);
$mailer = \Swift_Mailer::newInstance($transporter);
$message->from($config['from_address'], $config['from_name']);
$message->to('To_Email, 'Name')
->subject('My Subject')
->setBody('My Content', 'text/html');
$mailer->send($message);
What's wrong with my code?
Is it possible?
Or any other solution?
Finally i find the way to solve this.
Actually Laravel 5 is not fully support this multi transporter config.
so i use alternative package to achieve it.
My Code is ,
foreach ($store_configs['list'] as $store_config) {
// Create Custom Mailer Instances.
$mailer = new \YOzaz\LaravelSwiftmailer\Mailer();
$transport = \Swift_SmtpTransport::newInstance(
$store_config['host'],
$store_config['port'],
$store_config['authentication']);
// Assign Dynamic Username.
$transport->setUsername($store_config['username']);
// Assign Dynamic Password.
$transport->setPassword($store_config['password']);
$smtp = new \Swift_Mailer($transport);
$mailer->setSwiftMailer($smtp);
$mailer->send('template', ['data'], function ($message) use ($queue) {
// Default Response goes here
$message->from('From Address', 'From Name');
$message->to($email, 'Name')->subject('My Subject')
->setBody('My HTML', 'text/html');
$message->getSwiftMessage();
//
});
}
Its works fine with multiple and dynamic transporter.
Thanks to All !

Email is not send in CakePHP

I am working with CakePHP version 1.3.13.
Here, I want to send mail. So I have written code like this :
function send_mail_to_client()
{
$text = "sending mail";
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('sachingarala.v2047#gmail.com' => 'My Site'));
$Email->to('nisargbhavsar24#gmail.com');
$Email->subject('Test mail');
$Email->send($text);
}
But, mail is not send to the destination. So what is the wrong in this code?
How can I resolve this problem?
Add the configs after initialization i.e. $Email = new CakeEmail(); like
$Email->config(array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);

Zend Mail - Email is not sent

I have a problem sending a registration email via zend_mail. The mail is transmitted only to mails that have a #gmail.com.
$email = "test#gmx.net";
$mail = new Zend_Mail ();
$mail->setBodyText ( 'some text' );
$mail->setBodyHtml ( 'some text' );
$mail->setFrom ( 'support#mysite.net', 'MySite.net' );
$mail->addTo ( $email, $email );
$mail->setSubject ( 'test' );
$mail->send ();
If the user has another email provider the email is not sent.
Any ideas?
I use smtp now and it works:
$config = array('auth' => 'login',
'username' => '****#gmail.com',
'password' => '****',
'port' => '25',
'ssl' => 'tls');
$transport = new Zend_Mail_Transport_Smtp('smtp.googlemail.com', $config);

Categories