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);
Related
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
}
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 :-)
Is there anyway in php or cakephp, to send email using smtp so that I can send email to a closed/internal mailing list. e.g:
In my company we have an email list: appsteam#company.com, which can only be send if a person also using xxx#company.com email. outside from that email it will be blocked. so in cakephp is there anyway to somehow take the user credential and send email under a company or maybe in gmail email, if it's google group
I think what I actually want will be, how to set up cakephp email so that it send just like I send directly from the email server, if I use gmail, then I want it looks like gmail (header, etc)
what the final code will do is supposedly, I will take credential from users for their email, and send the mail using that. here is my code for AbcComponent.php
/*
* Abc components
*/
class AbcComponent extends Component {
public $options = array();
/**
* Constructor
**/
public function __construct(ComponentCollection $collection, $options = array()){
parent::__construct($collection,$options);
$this->options = array_merge($this->options, $options);
}
public function send_link ($user_email = null, $recipients = null, $subject = null, $message = null, $group_id = null, $user_id = null, $email_id = null, $download_password = null) {
$final_subject = $subject;
$final_recipients = $recipients;
$final_message = $message;
App::uses('CakeEmail', 'Network/Email');
$recipients = str_replace(' ', '', $recipients);
$recipients = explode(',', $recipients);
$recipient_queue_num = 1;
//Send the email one by one
foreach ($recipients as $recipient) :
$email = new CakeEmail();
$email->delivery = 'smtp';
//$email->from($user_email);
$email->from('xxxxx#gmail.com');
$email->to($recipient);
$email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'xxxxx#gmail.com', //this will be later grab from dbase based on user
'password'=>'password',
);
$email->subject($final_subject);
$email->template('download_link_email');
$email->emailFormat('both');
$email->viewVars(array('final_message' => $final_message));
if($email->send()) {
debug('email is sent');
} else {
debug('email is not sent');
}
//queue number increase for hashing purpose
$recipient_queue_num++;
endforeach;
}
$this->Email->delivery = ...
and later
$email = new CakeEmail();
Where is your "new object" statement before trying to access it?
You cannot just use it without.
You are also using both $this->Email and $email. This is probably your mistake (copy and paste errors).
$this->Email = new CakeEmail();
$this->Email->delivery = ...
...
"Indirect modification of overloaded property" is usually always an indicator of a non declared object that you are trying to access.
I'm trying send a email when admin cancel a order manually. I'm using the order_cancel_after event and my observer method run fine.
But my email is not fired. I get the following exception (above), despite all code be run.
exception 'Zend_Mail_Protocol_Exception' with message 'No recipient forward path has been supplied' in /home/mydomain/www/loja/lib/Zend/Mail/Protocol/Smtp.php:309
I tested sending a new email on my order observer: $order->sendNewOrderEmail(), and the new order email arrived correctly, so my SMTP is ok.
My code in observer:
class Spalenza_Cancelorder_Model_Observer
{
public function enviamail(Varien_Event_Observer $observer)
{
$order = $observer->getOrder();
if ($order->getId()) {
try {
$translate = Mage::getSingleton('core/translate');
$email = Mage::getModel('core/email_template');
$template = 16;//Mage::getModel('core/email_template') ->loadByCode('Cancelamento Manual by Denis')->getTemplateId();
Mage::log('Codigo do template: '.$template,null,'events.log');
$sender = array(
'name' => Mage::getStoreConfig('trans_email/ident_support/name', Mage::app()->getStore()->getId()),
'email' => Mage::getStoreConfig('trans_email/ident_support/email', Mage::app()->getStore()->getId())
);
Mage::log($sender,null,'events.log');
$customerName = $order->getShippingAddress()->getFirstname() . " " . $order->getShippingAddress()->getLastname();
$customerEmail = $order->getPayment()->getOrder()->getEmail();
$vars = Array( 'order' => $order );
$storeId = Mage::app()->getStore()->getId();
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')
->sendTransactional($template, $sender, $customerEmail, $customerName, $vars, $storeId);
$translate->setTranslateInline(true);
Mage::log('Order successfully sent',null,'events.log');
} catch (Exception $e) {
Mage::log($e->getMessage(),null,'events.log');
}
} else {
Mage::log('Order not found',null,'events.log');
}
}
}
Magento version: 1.5.1.0
This line
$customerEmail = $order->getPayment()->getOrder()->getEmail();
should probably be
$customerEmail = $order->getPayment()->getOrder()->getCustomerEmail();
You should pay attention to what the error message says and check the output of your variables.
My situation is as follows:
I use ubercart and I have 3 modules installed for email (when a customer makes the purchase) confirmation of a purchase. The modules are: SMTP, MIMEMail and HTML MAIL.
Verifying, sending works with HTML.
However, I need to make another type of email, when a product expires.
And that I'm doing with my own module. However, when I use the function of drupal_mail or drupal_mail_send sending doesn't correct.
When I debug the function apparently everything is correct, but I don't get any email.
My code for the function is drupal_mail_send:
$message = array(
'to' => $to,
'from' => $from,
'id' => 'cuponalcubo_mailing',
'subject' => $subject,
'body' => $body,
'headers' => array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Pressflow',
)
);
drupal_mail_send($message);
And the code for the function is drupal_mail:
function ....(){
$params = array(
'subject' => $subject,
'body' => t($body)
);
drupal_mail('email_deal_vp', 'email_deal_vp_html_mail', $to, language_default(), $params, $from);
}
/*
* Implements HOOK_MAIL
*/
function email_deal_vp_mail($key, &$message, $params) {
$language = $message['language'];
switch ($key) {
case 'email_deal_vp_html_mail':
$message['subject'] = t($params['subject'], $var, $language->language);
$body = "<html><body>
{$params['body']}
</body></html>";
$message['body'][] = $body;
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
break;
}
}
Maybe this is a problem with your server (SMTP is not configured properly). Check with any existing mail code whether it is working. (If it was working before.)