Setting mail template in ses client - email

I am developing a web application using cakephp 3.0 and I have an email sending function earlier I was use php mail function. Now I am using AWS ses client. So in that ses client how I can render a template. In cake php email function it was possible. But I don't know how to do it in aws ses client.
My code is
public function testMail() {
if ($this->request->is('post')) {
$client = SesClient::factory(Configure::read('AWScredentials'));
$formData = $this->request->data;
$body = $formData['body'];
$ToAddresses = $formData['ToAddresses'];
$request = array();
$request['Source'] = 's#gmail.com';
$request['Destination']['ToAddresses'] = array($ToAddresses);
$request['Destination']['CcAddresses'] = 'ss#gmail.com';
$request['Message']['Subject']['Data'] = 'Test mail from vcollect ses';
$request['Message']['Subject']['Charset'] = 'ISO-2022-JP';
$request['Message']['Body']['Text']['Data'] = $body;
$request['Message']['Body']['Text']['Charset'] = 'ISO-2022-JP';
try {
$result = $client->sendEmail($request);
$messageId = $result->get('MessageId');
$this->log("Email sent! Message ID: $messageId", "info");
} catch (Exception $e) {
$this->log("The email was not sent. Error message:" . $e->getMessage(), "error");
}
} }

You must create a debug sender in app.php, just add it to your EmailTransport array like this, it's allow you to do a fake email without send it
'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,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'debug' => [
'className' => 'Debug',
],
],
after to generate the email with your your template you can use this function for exemple
public function setTemplate($template,$layout = null,$viewVars = array(),$debug = 0)
{
if(empty($layout)) $layout = 'default';
$Email = new Email();
$Email->template($template, $layout)
->transport('debug') //<---- Allows you to simulate the message without sending it
->to(array('foo#bar.org'=>'toto'))
->emailFormat('html');
->viewVars($viewVars);
$Email->send();
/* Once email is generate, personally i duplicate the object for access to the protected proprety ( it's not very clean but i don't have a greatest solution)
The idea is to get _htmlMessage in the object */
$refObj = new ReflectionObject($Email);
$refProp1 = $refObj->getProperty('_htmlMessage');
$refProp1->setAccessible(TRUE);
if($debug == 1)
{
die(debug($refProp1->getValue($Email)));
}
return $refProp1->getValue($Email); // <---- your html code
}

Related

Message: Call to undefined method CI_Email::get_emails() codeigniter 3

I try build function in controller Codeigniter 3 to read emails from inbox and insert to database.
`public function read_emails() {
// Load the email and database libraries
$this->load->library('email');
// Connect to the POP3 server
$config = array(
'protocol' => 'pop3',
'pop3_host' => 'pop.gmail.com',
'pop3_user' => 'xxxxxxxxxxxx',
'pop3_pass' => 'xxxxxxxxxxxxxxxx',
'pop3_port' => 995,
'pop3_encryption' => 'ssl'
);
$this->email->initialize($config);
// Retrieve emails from the inbox
$emails = $this->email->get_emails();
// Loop through the emails
foreach ($emails as $email) {
// Check if the email was received within the last 24 hours
$received_time = strtotime($email['received_time']);
$current_time = time();
if ($current_time - $received_time <= 86400) {
// Check if the email already exists in the database
$this->db->where('email', $email['email']);
$query = $this->db->get('contacts');
if ($query->num_rows() == 0) {
// Insert the email into the database
$data = array(
'email' => $email['email'],
'subject' => $email['subject'],
'message' => $email['message']
);
$this->db->insert('contacts', $data);
}
}
}
}`
When I run this controller I get output:
Message: Call to undefined method CI_Email::get_emails()
Can anyone please help me debug this issue? Very important.
line 43:
// Retrieve emails from the inbox
$emails = $this->email->get_emails();
I don't know if codeigniter3's email class has such a get_emails() function, or if you can retrieve emails from the inbox.
Please check the official documentation: https://codeigniter.com/userguide3/libraries/email.html
If it is a custom class/plugin/addon, you should either publish its code or submit a link to the repo

How to send an email to the user when performing login in cakephp

I do not have any knowledge about cakephp mail so explain the solution briefly I mean what to do and how to do from the beginning.
From the cakephp official side I just used this "use Cake\Mailer\Email;" and then the mail function but a error message shows like as shown below
Could not send email: mail(): Failed to connect to mailserver at
quot;server.com" port 25, verify your "SMTP" and
quot;smtp_port" setting in php.ini or use ini_set()
MY users controller login function
public function login() {
$this->viewBuilder()->setLayout('');
if ($this->request->is('post')) {
$data = $this->request->getData();
$query = $this->Users->find()->where(['email' => $data['email'], 'password' => md5($data['password'])]);
if ($query->count()) {
$user = $query->first()->toArray();
$this->Auth->setUser($user);
//FOR MAIL START
ini_set('SMTP', "server.com");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "restrange5#gmail.com");
$email = new Email('default');
$email->setFrom(['restrange5#gmail.com' => 'My Site'])
->setTo('ramakantasahoo835#gmail.com')
->setSubject('About')
->send('My message');
//FOR MAIL END
$this->Flash->success(_('Login Successfull'));
$this->redirect(['action' => 'dashboard']);
} else {
$this->Flash->error(__('Username/Password not found!!'));
return $this->redirect($this->referer());
}
}
}
How much I know as I have just changed in users controller only. What else I have do please suggest.
**Your cofiguration in app.php file is something like this **'
EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'email here',
'password' => 'password here',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
]

Drupal how to send a mail

I'm trying to send custom mail to users, in a module I create these two methods :
/**
* Implements hook_theme().
*/
function ga_planning_theme() {
return array(
'ga_planning_mail_status_change' => array(
'template' => 'templates/ga_planning_mail_status_change',
'variables' => array(),
)
);
}
/**
* Implements hook_mail().
*/
function ga_planning_mail($key, &$message, $params) {
switch ($key) {
case 'status_change_mail':
$message['subject'] = t('Changement de statut');
$message['body'][] = theme('ga_planning_theme', $params);
break;
}
}
And I try to send the mail with drupal_mail :
drupal_mail('ga_planning', 'ga_planning_mail_status_change', "myadress#mail.com", NULL, $params, variable_get('site_mail'), TRUE);
But the mail is not sending, it send a mail to the webmaster mail with the default template and with this subject :
DEBUG - FROM MyWebsite.com
What I am doing wrong ?
First,please make sure you can send emails and second:
You need to define your $params array. For example:
$params['test'] = 'ok';
And also, try to declare the headers:
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['subject'] = t('Changement de statut');
$message['body'][] = theme('ga_planning_theme', $params);

How to set multiple CC email recipients one message using webform in drupal 7

My website using webform in drupal 7 application.
I have two queries
I can added a list for To email recipients one message. Email will receiving all the recipients but mails sending individual. I cant see in the to list group.
How to set multiple CC email recipients one message ? Here am using 'CC' => 'xx#yyyy.com, xx1#yyyy1.com, xx2#yyyy2.com' in theme_mail_headers under "$headers.
function MODULENAME_mail($key, &$message, $params) {
if($key === 'MODULEcase') {
$header = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'Cc' => implode(',', $params['Cc']),
);
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
break;
}
}
Then prepare data and pass to drupal_mail function
$mail_content = "Hello There!";
$params = array('body' => $mail_content);
$params['cc'] = array(
'cc_user1#example.com',
'cc_user2#example.com',
'cc_user3#example.com',
);
$to = 'touser#example.com';
$from = 'no-reply#example.com';
$mail = drupal_mail('MODULENAME', 'MODULEcase', $to, language_default(), $params, $from);
This is it. Enjoy :-)

Send mail to multiple recipients from data base using zf2

This is my first application using Zend Framework 2. I want to send an email to recipients retreived from a database, but each time my code displays an error. This is my code:
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$idconnected = $user->offsetGet('id');
$mail = $db->query("SELECT email FROM user WHERE id =" . $idconnected)->execute()->current();
$message = new Message();
$message->addTo($mail, 'eee#web.com')
->addFrom('xxxx#gmail.com')
->setSubject('Invitation à l’événement : Soirée Latino');
$message->addCc('xxxx#hotmail.com')
->addBcc("xxxx#hotmail.com");
// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'host' => 'smtp.gmail.com',
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => 'xxxx#gmail.com',
'password' => '*********'
),
'port' => 587,
));
$html = new MimePart('<b>Invitation for the event: Latin Night, orgonized by Mr. Jony Cornillon <i>Date : 06/04/2015</i></b>');
$html->type = "text/html";
$body = new MimeMessage();
$body->addPart($html);
//$body->setParts(array($html));
$message->setBody($body);
$transport->setOptions($options);
$transport->send($message);
}
And this is the error:
5.1.2 We weren't able to find the recipient domain. Please check for any
5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
5.1.2 or other punctuation after the recipient's email address. a13sm19808164wjx.30 - gsmtp
I tried with this code, and now it works fine :
foreach ($mail as $recip) {
$message->addTo($recip);
$message->addTo('samehmay#hotmail.com', 'eee#web.com')
->addFrom('maysameh0#gmail.com')
->setSubject('Invitation à l’événement : Soirée Latino');
}