bcc emails are not delivered - Drupal 9 SMTP - email

Below is my code to send out email using plugin.manager.mail in Drupal.
$params['context']['subject'] = t('[#site] A new event has been created');
$params['context']['message'] = $mode_info . '' .
'Hello User !' . '<br />' .
'<p class="notif-email">'.'A new event has been added to the PREP website. Below are the details of the new event: '. '<br />' .
'Event details: '.$event_details;
$s_recipients = implode(', ', $event_recipients);
$to = 'test#test.com';
$params['headers']['bcc'] = $s_recipients;
\Drupal::service('plugin.manager.mail')->mail('system', 'mail', $to, $langcode, $params);
I am not using hook_mail hook. rather, I call a custom method within node_presave hook to send out the email:
function mymodule_node_presave(EntityInterface $entity) {
if($entity->bundle() == 'events') {
notify_new_events($entity, 'created'); //email piece is detailed within this method
}
}
Problem: Email is not sent out for bcc recipients. doing dd($params) shows params array with bcc addresses correctly. Any help on how this can be fixed pls?!
I am using SMTP authentication module to send out the emails.

I got a similar error and turns out a user had an invalid email on DB, like you have "test#test.com".
Try to use real emails to validate if it is because of that or not.
I am not seeing any error on your php code.

Related

Sendgrid change text and style for group unsubscribe links

I'm facing one problem while sending emails with SendGrid. I'm sending normal email with assigning unsubscribe ground id. And when sending email then getting two links Unsubscribe From This List and Manage Email Preferences. Now my requirement is that I want to change these link texts from
Unsubscribe From This List => Unsubscribe
Manage Email Preferences => Update Preferences
And also links are not coming in the bottom of email. I want these after footer texts.
I'm not using marketing emails. Below is the one email screen-shot which I got:-
And below is the code for sending email. I'm using Laravel for sending emails:-
public function sendMail()
{
$status = \Mail::send('emails.demo', ['name' => "testdata"], function($message)
{
$args = [
'asm_group_id' => 3169
];
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode($args));
$message->to('test#gmail.com', 'Test User')->subject('This is a demo!');
});
}
Please suggest me if anyone have idea how can I achieve this.
Check this package. https://github.com/s-ichikawa/laravel-sendgrid-driver .
This is the correct format for the above package:
$send = \Mail::send('emails.tests.tests', compact([]),
function (Message $message) use (
$subject, $from_email, $from_name, $to_emails, $to_name ) {
$message->subject($subject)
//->attach($pathToFile)
//->to($to_emails)
->to($to_emails, $to_name)
//->cc($cc_email)
->from($from_email, $from_name)
->embedData([
'asm' => ['group_id' => 123],
],
'sendgrid/x-smtpapi');
});
if you are not using this package then
$status = Mail::send('emails.test', compact('data_var'), function($message)
{
$args = [
'asm_group_id' => 123
];
$message->from('testfrom#gmail.com','from name');
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode($args));
$message->to('testto#gmail.com', 'Test User')
->subject('This is a demo!');
});

Store email body in table using Laravel 5.3 Mailable

I want to store the email body in my communications table. My controller:
Mail::to($user->email)->send(new WelcomeEmail($subscription));
Communication::create([
'to' => $user->email,
'subject' => 'Welcome Email',
'body' => '???'
]);
My email goes out (successfully) and I am able to create a Communication record, but have no idea how to retrieve the email body.
After reading the Mail manual, I thought I could work with an event:
protected $listen = [
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\LogSentMessage',
],
];
But here I get only the full plain text email. If I create an instance of the mail, with:
$email = Mail::to($user->email)->send(new WelcomeEmail($subscription));
the outcome of dd($email); is null.
Some extra info, in my WelcomeEmail.php, I am using a view:
public function build()
{
return $this->view('emails.welcome_email');
}
You can directly do this to render Mailable and store it in a variable
$html = (new WelcomeEmail($subscription))->render();
I found a solution, hopefully it can help other people.
$body = View::make('emails.welcome_email')
->with('subscription', $this->subscription)
->with('template', $this->template)
->render();
I rendered the view and saved it in the body variable.

cakephp2 how to send email using gmail smtp dynamically to a closed mailing list

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.

Sent email appears in recipient's spam folder in cakePHP using gmail account?

I am Using cakephp for one of my project. for sending email I am using cakeEmail. For that I created one gmail account for sending emails(i.e. used in code for send mail from that account).Mail sending works but sent mail appears recipient's spam folder.
Also in gmail account that mail dosen't appear sent mail folder.
code is as bellow :
In /app/Config/email.php file is :
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'username#gmail.com',
'password' => 'password',
'transport' => 'Smtp'
);
}
and code in my controller file for password recovery is :
public function _sendemail($user_data,$temporary_password){
$email = new CakeEmail();
$email->config('gmail');
$email->template('welcome', 'password_recovery_email'); //template
$email->emailFormat('html');
$email->viewVars(array(
'temporary_password'=>$temporary_password,
'user_data'=>$user_data
));
$email->from(array('username#gmail.com' => 'Password Recovery'));
$email->to($user_data['User']['email_address']);
$email->subject('password recovery email');
$result=$email->send();
}
Please tell me what should I do to get all sent emails to appears in inbox rather than spam folder.
Thanks
A lot of reasons can make your email be considered a SPAM.
Maybe Gmail can't check the sender (smtp source/your developing machine) as a trusted sender. Have you tried uploading your php script to another server (production) then sending it from there?
Have you tried changing the message content/subject to something different from "Recover your password"?
Have you tried sending the same message to another recipient?
Gmail uses a lot of rules to check whether an email is a spam or not, check out this link: https://webapps.stackexchange.com/questions/5773/how-gmail-and-other-mail-services-detects-a-mail-as-a-spam
Try using this code rather than cakephp's methods:
$subject = "Your Email Subject";
$to = "Recipient's Email Address";
$from = "Your Email Address";
$additional_headers = "From: $from\r\n";
$additional_headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$additional_headers .= "MIME-Version: 1.0\r\n";
$additional_headers .= "Return-Path: $from\r\n";
$additional_headers .= "X-Priority: 3\r\n";
$additional_headers .= "X-Mailer: PHP\r\n";
$message = "Your Email Body";
mail($to, $subject, $message, $additional_headers, "-f $from")

send email via Magento

I've seen on a few blog articles that this is a common way to send an email in Magento, but I have for the life of me, no idea why this email isnt being sent in 1.10! This is my method:
protected function _emailCode($code, $invoice) {
$order = $invoice->getOrder();
// Transactional Email Template's ID
$templateId = 1;
// Set sender information
$senderName = Mage::getStoreConfig('trans_email/ident_support/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
$sender = array('name' => $senderName,
'email' => $senderEmail);
// Set recepient information
$recepientEmail = $order->getCustomerEmail();
$recepientName = $order->getCustomerName();
// Get Store ID
$storeId = Mage::app()->getStore()->getId();
// Set variables that can be used in email template
$vars = array('voucherCode' => $code);
$translate = Mage::getSingleton('core/translate');
// Send Transactional Email
Mage::getModel('core/email_template')
->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
$translate->setTranslateInline(true);
}
I should note that emails works in other parts of Magento so sendmail is working properly and all that, also all my variables here are defined correctly and not empty when going through this.
Thanks!
Are you sure that transactional email with ID=1 exist?
try setting $templateId='sales_email_order_template'
this is a default template, should fit working script.
Check exception.log also.