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 :-)
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
}
I wish to send HTML emails. I tried using mime.php but could not get it working. Below is my working text email code:
<?php
$subject="hello-test";
$body="<html><body><h1>message body</h1></body></html>";
$em_arr=array("email#example.com");
foreach ($em_arr as $to_address)
{
require_once '/usr/local/share/pear/Mail.php';
$headers = array (
'Content-Type:text/html;charset=UTF-8',
'From' => 'Test <test#example.com>',
'To' => $to_address,
'Subject' => $subject);
$smtpParams = array (
'host' => '<smtp host address>',
'port' => 587,
'auth' => true,
'username' => '<uname>',
'password' => '<password>'
);
// Create an SMTP client.
$mail = Mail::factory('smtp', $smtpParams);
// Send the email.
$result = $mail->send($to_address, $headers, $body);
if (PEAR::isError($result)) {
echo("Email not sent. " .$result->getMessage() ."\n");
} else {
echo("Email sent to ".$to_address."\n");
}
}
?>
Please let me know how can I send HTML emails.
I had to replace the Content header line to:
'Content-Type' => "text/html; charset=UTF-8",
That was the mistake above. It is working fine now.
I have tried many cake options to include a cc or bcc or additional email with the invoices and nothing works, I need a a workable example to the following format, I have have not found any clear solution.
public function send() {
$id = $this->request->data['Invoice']['id'];
$invoiceNumber = $this->request->data['Invoice']['invoice_number'];
$to = $this->request->data['Invoice']['to'];
$from = $this->request->data['Invoice']['from'];
$message = $this->request->data['Invoice']['message'];
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->config(array(
'from' => $from,
'transport' => 'Mail',
'charset' => 'utf-8',
'headerCharset' => 'utf-8'
));
$result = $email->emailFormat('html')
->to($to)
->addbcc('WCMBilling#icloud.com')
->from($from)
->returnPath($from)
->subject('New Invoice ('.$invoiceNumber.')')
->attachments(WWW_ROOT.'/temp/Invoice_'.$invoiceNumber.'.pdf')
->send($message);
unlink(WWW_ROOT.'/temp/Invoice_'.$invoiceNumber.'.pdf');
$this->Session->setFlash('Email Sent');
$this->redirect(array('controller'=>'invoices', 'action'=>'view', $id));
}
I found it in the email.php file.
Where there is "null" on the cc line, or bcc line, enter the desired email and all works fine.
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.)
I am trying to send an email using MIME::Lite but the email will not send and I am not getting any errors.
Code:
my $subject = $Config->{email}->{subject};
my $from_email = $Config->{email}->{from_email};
my $message = $Config->{email}->{message};
my $smtp_server = $Config->{email}->{smtp_server};
my $msg = MIME::Lite->new
(
Subject => $subject,
From => $from_email,
To => $email,
Type => 'text/html',
Data => $message
);
$msg->send('smtp' ,$smtp_server );
Not much to go on there ...
Maybe try debugging?
$msg->send( 'smtp', $smtp_server, Debug=>1 );
And check all the values in $Config->{email} are as you would expect.