Class '\Zend_Mail_Transport_Smtp' not found in Zend - php

I am trying to send an email via Zend Mail Transport SMTP
I tried to browse sample codes and use it. What I have now is this code
$config = array('auth' => 'login',
'username' => 'sample#gmail.com',
'password' => 'samplepassword',
'port' => 587,
'ssl' => 'tls');;
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
$mail->setBodyHtml('Hi From Dave.');
$mail->setFrom('noreply.networklabs#networklabs.com.ph');
$mail->addTo('john.decena#nokia.com', 'john.decena#nokia.com');
$mail->setSubject('Profile Activation');
$mail->send($transport);
Apparently, this code gives me error
Class 'User\Controller\Zend_Mail_Transport_Smtp' not found
May I know what I'm missing? because I tried to study their codes but they didn't have that error. And what credentials did I have to use.? Thanks in advance.

This really looks like a namespace problem.
I don't know which version of ZF2 you are using, but you need to add an use statement at the beginning of your file, something like
use Zend\Mail\Transport\Smtp;
and the create the new instance with
$transport = new Smtp('smtp.gmail.com', $config);

Related

Codeigniter 4 Email sending with HTML as message

can you help me figure out a way to use the email class of CI4 to send an email with an HTML as the message?
in Codeigniter 3 it is simple and goes like this:
$email->message($this->load->view('html_page_to_send_from_view_folder',$data,true));
but in Codeigniter 4 I tried doing this:
$email->setMessage(echo view('html_page_to_send_from_view_folder',$data,true));
It gives out an error:
syntax error, unexpected echo(T_ECHO), expecting ')'
I also tried putting the view in a variable like so:
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
but it just throws the same error, I was searching all over the internet and the documentation but couldn't find a way to do this. Help please.
I tried this but got no error, and also got no email :'(
$echo_page = view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
According to this, if you want to use some template as your message body, you should do something like this:
// Using a custom template
$template = view("email-template", []);
$email->setMessage($template);
CodeIgniter 4 documentation states:
setMessage($body)
Parameters: $body (string) – E-mail message body
Returns: CodeIgniter\Email\Email instance (method chaining)
Return type: CodeIgniter\Email\Email
Sets the e-mail message body:
$email->setMessage('This is my message');
Okay I got it now I made it work by adding this code $email->setNewLine("\r\n"); at the end just after the setMessage:
$email->setMessage($my_message);
$email->setNewLine("\r\n");
and also I set the SMTP port 587 instead of 465:
$config['SMTPPort']= 587;
ALSO, for the setMessage, I did it like this:
$my_message = view('html_page_to_send_from_view_folder',["id" => $data['id']]);
$email->setMessage($my_message);
really weird man....
A. Firstly,
Instead of:❌
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
Use this:✅
$echo_page = view('html_page_to_send_from_view_folder',$data);
Notice the luck of an echo statement and not passing true as the third argument of the view(...) hepler function.
B. Secondly, to submit HTML-based emails, ensure that you've set the mailType property to html. This can be achieved by using the setMailType() method on the Email instance. I.e:
$email->setMailType('html');
Alternatively, you could set the "mail type" by passing an array of preference values to the email initialize() method. I.e:
public function sendMail(): bool
{
$email = \Config\Services::email();
$email->initialize([
'SMTPHost' => 'smtp.mailtrap.io',
'SMTPPort' => 2525,
'SMTPUser' => '479d7c109ae335',
'SMTPPass' => '0u6f9d18ef3256',
'SMTPCrypto' => 'tls',
'protocol' => 'smtp',
'mailType' => 'html',
'mailPath' => '/usr/sbin/sendmail',
'SMTPAuth' => true,
'fromEmail' => 'from#example.com',
'fromName' => 'DEMO Company Name',
'subject' => 'First Email Test',
]);
$email->setTo('to#example.com');
$email->setMessage(view('blog_view'));
$response = $email->send();
$response ? log_message("error", "Email has been sent") : log_message("error", $email->printDebugger());
return $response;
}

How to fix "Undefined property: Register::$encrypt" in PHP codeigniter framework

When I am submitting my form in CodeIgniter, I get this error Message: Undefined property: Register::$encrypt. I want to hash the password that's the reason I used the encrypt.
I have tried to include encrypt library in autoload.php but still another error pops up.
this is where the error is popping up.
function validation(){
$this->form_validation->set_rules('user_name','Name','required|trim');
$this->form_validation->set_rules('user_email','Email Address','required|trim|valid_email|is_unique[codeigniter_register.email]');
$this->form_validation->set_rules('user_password','Password','required|trim');
if($this->form_validation->run()){
$verification_key=md5(rand());
$encrypted_password = $this->encrypt->encode($this->input->post('user_password'));
$data = array(
'name' => $this->input->post('user_name'),
'email' => $this->input->post('user_email'),
'password' => $encrypted_password,
'verification_key' => $verification_key
);
$id=$this->register_model->insert($data);
if($id > 0){
$subject='Please verify email for login';
$message="
<p>Hi".$this->input->post('user_name')."</p>
<p>Verify your email for login to this system. Click this <a href='".base_url()."register/verify_email/".$verification_key."'>link</a>.</p>
<p>Use this link to log in in to this system.</p>
<p>Thanks You.</p>
";
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtpout.secureserver.net',
'smtp_port' => 80,
'smtp_user' => 'root',
'smtp_pass' => 'root',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' =>TRUE
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('info#icode.info');
$this->email->to($this->input->post('user_email'));
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send()){
$this->session->set_flashdata('message','Check in your email for verification mail');
redirect('register');
}
}
}
else{
$this->index();
}
I expected after submitting the form to give an alert or send the data I have filled in my form to the database,
Load the encryption libarary
$this->load->library('encryption');
$encrypted_password = $this->encrypt->encode($this->input->post('user_password'));
https://www.codeigniter.com/user_guide/libraries/encryption.html
Once loaded, the Encryption library object will be available using:
$this->encryption
Update:
As you mentioned your loading it in the constructor I took another glance at it:
It looks like a spelling error
$this->load->library('encryption');
...
$this->encrypt->encode($this->input->post('user_password'));
instead of
$this->load->library('encryption');
...
$this->encryption->encode($this->input->post('user_password'));
Also in the documentation it's presented this way
$ciphertext = $this->encryption->encrypt($plain_text);
Simple mistake, took me a few minutes of looking at it too see it too. I never type things out if I can avoid it, I copy/paste everything. Mostly that is because my spelling and stuff is bad (Dyslexia) but it can avoid some issues like this... lol
Glad we got it sorted for you!

How can i send dynamic data in bootstrap file for Email in zend framework

I am sending email to user while registration. For that i am using Zend's default function.
For that i added code in my Bootstrap file.
protected function _initMail()
{
try {
$config = array(
'auth' => 'login',
'username' => 'username#gmail.com',
'password' => 'password',
'ssl' => 'tls',
'port' => 587
);
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($mailTransport);
} catch (Zend_Exception $e){
}
}
Now my need is username,password and port i want to make this field dynamic. I want to fetch this record from the database. Can anyone please tell me how can i solve that problem. Any help will be appreciated.
I would say that you do not put these into the database. A better way would be to put them into a config file and then set them up from there. Doing a database call EVERY SINGLE time your application is called is not really optimal.
The way you should be doing it is:
protected function _initMail()
{
try {
$config = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$config = $config->getOption("mail");
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($mailTransport);
} catch (Zend_Exception $e){
}
}
That is assuming that your application.ini has this:
mail.auth="login"
mail.username="username#gmail.com"
mail.password="password"
mail.ssl="tls"
mail.port=587
However if you decide you want to do this, and have set up your database in the application.ini file (assuming you use ini files for config) something like this:
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "your.database.host"
resources.db.params.dbname = "database_name"
resources.db.params.username = "username"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true
You can then do:
protected function _initMail()
{
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
$select=$db->select()->from("emailSetup");
$credentials=$db->fetchOne($select);
[..email part here..]
}
P.S. I have not tested the select, but you should get the gist of it.

Is there a cleaner code for the PHPmailer in wordpress..?

As a extended version to this question : PHPMailer(): Called Mail() without being connected
Can't we use all the configurations for the PHPmailer in an array, Some thing like this:
$mail = array(
'SMTPAuth' => true,
'Host' => 'smtp.millicenthotel.com',
'Port' => 25,
'Username' => 'info#millicenthotel.com',
'Password' => 'gboskeritysoldier1'
);
I tried something like this and it not work. I'm looking for a cleaner way of doing this.
Thanks in Advance.
you have an action hook called phpmailer_init
from codex :
The wp_mail function relies on the PHPMailer class to send email
through PHP's mail function. The phpmailer_init action hook allows you
to hook to the phpmailer object and pass in your own arguments.
example ( codex again ):
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
$phpmailer->IsSMTP(); //switch to smtp
$phpmailer->Host = 'smtp.example.com';
$phpmailer->Port = 25;
$phpmailer->Username = 'yourusername';
$phpmailer->Password = 'yourpassword';
}

Gmail smtp 5.5.2 error with PHP ZEND and Xoauth2

I trying to send emails with Gmail using gmail api with xoauth authentication.
The IMAP example from google works fine.
I am trying to implement the thing based on these articles:
http://www.boxuk.com/blog/php-smtp-xoauth2-gmail/
http://www.chargenevier.com/2013/02/google-smtp-and-oauth2-authentication/
I get the following error when i try to send the email:
Fatal error: Uncaught exception 'Zend_Mail_Protocol_Exception' with message '5.5.2 Syntax error. e5sm17066681bkg.3 - gsmtp
I also tried the openssl method on the first article, after write in XOAUTH2 i get the same error from the server and connection is closed.
Interesting when i do echo base64_encode("user=\1auth=Bearer \1\1") i got a string whitch is more shorter than that when i echo the $smtpInitClientRequestEncoded variable what is generated with constructAuthString method, what is working fine in the IMAP example.
And mail.google.com gives 5.5.2 Cannot decode response for the shorter encoded string.
function constructAuthString($email, $accessToken) {
return base64_encode("user=$email\1auth=Bearer $accessToken\1\1");
}
function sendEmail($email,$accessToken){
$smtpInitClientRequestEncoded = constructAuthString($email, $accessToken);
echo '<br/><br/>BASE64: ' . $smtpInitClientRequestEncoded;
$config = array('ssl' => 'ssl',
'port' => '465',
'auth' => 'xoauth',
'xoauth_request' => $smtpInitClientRequestEncoded
);
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('some body text');
$mail->setFrom($email, 'Some Sender');
$mail->addTo("myemail#gmail.com", 'Some Recipient');
$mail->setSubject('Test sending by smtp');
$mail->send($transport);
}
What do you think, what is wrong? Why dont this authenticates?
$accesToken was bad. I passed $client->getAccessToken(), but you need just the access_token string from this string, not the whole.

Categories