I can send a message correctly , but I have a problem with variable subject , how I can I send the variable $subject into Mail method.
->subject($subject)
I recieve an undefined variable , this is my code.
public function enviarmensaje()
{
$email = Request::input('email');
$subject= Request::input('subject');
$name = Request::input('nombre');
$message = Request::input('message');
$data = array( 'name' => $name , 'correo' => $email , 'mensaje' => $message,'subject'=> $subject );
Mail::send('contact', $data , function ($message) {
$message->to('email#gmail.com','To anyone')->subject("Contact form");
});
}
You have to use the $subject variable:
Mail::send('contact', $data , function ($message) use ($subject) {
$message->to('email#gmail.com', 'To anyone')->subject($subject);
});
Alternatively, you can call Request::input('subject') from within the closure itself:
Mail::send('contact', $data , function ($message) {
$message->to('email#gmail.com', 'To anyone');
$message->subject(Request::input('subject'));
});
Related
I have created a custom registration form in Drupal 8, and now i want to sent a mail from the submission of the form. So I have did it like this
This is my .module file
/**
* Implements hook_mail().
*/
function Registration_form_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'contact_form':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
break;
}
}
public function submitForm(array &$form, FormStateInterface $form_state){
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'Registration_form';
$key = 'contact_form'; // Replace with Your key
$to = $form_state->getValue('Email');
$params = array(
'body' => 'test',
'subject' => 'Website Information Request',
);
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$message['subject'] = t('nouveau contact ');
$message['body'][] = t('test');
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] != true) {
$message = t('There was a problem sending your email notification to #email.', array('#email' => $to));
drupal_set_message($message, 'error');
\Drupal::logger('mail-log')->error($message);
return;
}
else{
$message = t('An email notification has been sent to #email ', array('#email' => $to));
drupal_set_message($message);
\Drupal::logger('mail-log')->notice($message);
}
}
So my question is i'm using localhost in xampp and i want to sent a mail after submission of the form, but i'm getting this error
Unable to send email. Contact the site administrator if the problem persists.
There was a problem sending your email notification to ABC#gmail.com
So how I can resolve my problem, i have gone through whole sites but not able to find answer.
The problem may be with the email configuration. See if this article helps you:
https://stevepolito.design/blog/drupal-configure-smtp-module-work-gmail-updated/
My code works, but for some strange reason when I return a string it adds extra space and I think a new line as well.
The code below works fine. However the length of this when fetched with Ajax is 11, the string success has a length of seven so this isn't right and it also doesn't equal to 'success' when comparing.
I've looked through a few other questions but all the solutions they offer don't seem to be working for me. No trailing spaces anywhere as far as I can see. And it's also not coming from any of the files I'm requiring.
<?php
/*
Plugin Name: Quotation Rest API
Plugin URI:
Description: A rest API for sending quotation request through to email using Sendgrid.
Version: 1.0
Author: Niels
Author URI:
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'offerte', '/send',array(
'methods' => 'POST',
'callback' => 'send_mail',
) );
} );
include('MailChimp.php');
use \DrewM\MailChimp\MailChimp;
function send_mail( WP_REST_Request $request) {
$data = $request['data'];
$name = htmlspecialchars($request['name']);
$email = htmlspecialchars($request['email']);
$comment = htmlspecialchars($request['comment']);
$website = htmlspecialchars($request['url']);
$company = htmlspecialchars($request['company']);
$tel = htmlspecialchars($request['tel']);
$mailchimp = htmlspecialchars($request['mailchimp']);
$dataArray = json_decode($data, true);
ob_start();
include('mail.php');
$mailHTML = ob_get_clean();
$success = 'success';
if (!empty($mailchimp)) {
// add email to mailchimp list
$client = new MailChimp('-us14');// get from setttings
$list_id = ' ';// get from setttings
$result = $client->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
}
$to = " #icloud.com";// get from setttings
$subject = "Offerte van " . $name;// // get from setttings (maybe...)
$message = $mailHTML;
if(wp_mail( $to, $subject, $message)){
return $success;
} else {
// return 'error';
}
}
?>
i am trying to set html on the output of the email send by joomla. my file is located in the joomla core.
i know i have to add something like ->isHTML(true); but i do not know where and how.
here is the code:
class MailtoController extends JControllerLegacy
{
/**
* Show the form so that the user can send the link to someone.
*
* #return void
*
* #since 1.5
*/
public function mailto()
{
$session = JFactory::getSession();
$session->set('com_mailto.formtime', time());
$this->input->set('view', 'mailto');
$this->display();
}
public function send()
{
// Check for request forgeries
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$app = JFactory::getApplication();
$session = JFactory::getSession();
$timeout = $session->get('com_mailto.formtime', 0);
if ($timeout == 0 || time() - $timeout < 1)
{
JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));
return $this->mailto();
}
$SiteName = $app->get('sitename');
$link = MailtoHelper::validateHash($this->input->get('link', '', 'post'));
// Verify that this is a local link
if (!$link || !JUri::isInternal($link))
{
// Non-local url...
JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));
return $this->mailto();
}
// An array of email headers we do not want to allow as input
$headers = array (
'Content-Type:',
'MIME-Version:',
'Content-Transfer-Encoding:',
'bcc:',
'cc:'
);
// An array of the input fields to scan for injected headers
$fields = array(
'mailto',
'sender',
'from',
'subject',
);
/*
* Here is the meat and potatoes of the header injection test. We
* iterate over the array of form input and check for header strings.
* If we find one, send an unauthorized header and die.
*/
foreach ($fields as $field)
{
foreach ($headers as $header)
{
if (strpos($_POST[$field], $header) !== false)
{
JError::raiseError(403, '');
}
}
}
/*
* Free up memory
*/
unset ($headers, $fields);
$email = $this->input->post->getString('mailto', '');
$sender = $this->input->post->getString('sender', '');
$from = $this->input->post->getString('from', '');
$subject_default = JText::sprintf('COM_MAILTO_SENT_BY', $sender);
$subject = $this->input->post->getString('subject', $subject_default);
// Check for a valid to address
$error = false;
if (!$email || !JMailHelper::isEmailAddress($email))
{
$error = JText::sprintf('COM_MAILTO_EMAIL_INVALID', $email);
JError::raiseWarning(0, $error);
}
// Check for a valid from address
if (!$from || !JMailHelper::isEmailAddress($from))
{
$error = JText::sprintf('COM_MAILTO_EMAIL_INVALID', $from);
JError::raiseWarning(0, $error);
}
if ($error)
{
return $this->mailto();
}
// Build the message to send
$msg = JText::_('COM_MAILTO_EMAIL_MSG');
$link = $link;
//$body = sprintf($msg, $SiteName, $sender, $from, $link);
$body = "<p>Hello Test F,</p><br/><p>Thank you for registering at Deals&offers. Your account is created and activated.</p><br/>You may login to ".$SiteName." using the following username and password:</br><p>Username: ".$sender."</p><p>Password: ".$from."/p><br/><p><b>Note:</b> It is recomended to change your password after first login. ".$link."</p>";
// Clean the email data
$subject = JMailHelper::cleanSubject($subject);
$body = JMailHelper::cleanBody($body);
// To send we need to use punycode.
$from = JStringPunycode::emailToPunycode($from);
$from = JMailHelper::cleanAddress($from);
$email = JStringPunycode::emailToPunycode($email);
// Send the email
if (JFactory::getMailer()->sendMail($from, $sender, $email, $subject, $body) !== true)
{
JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));
return $this->mailto();
}
JFactory::getApplication()->enqueueMessage('ok!', '');
$this->input->set('view', 'sent');
$this->display();
}
}
thank you very much
you can add before body or between subject and body . however, it must be before the submit command !!
here is an example of PhpMailler firstly, you need to call the class like this and you can use it
$this->mail= new PHPMailer();
$this->mail->IsSMTP();
$this->mailIsHTML(true);
$subject = JMailHelper::cleanSubject($subject);
$body = JMailHelper::cleanBody($body);
however if the function is static also you call the function in same class
you can call the function by sef command
self::mailIsHTML(true)
I trying to send mail using yii simple mailer ...
I followed the step :
http://www.yiiframework.com/extension/yii-simple-mailer/
And download the extension and put in yii extension folder :
https://github.com/tlikai/YiiMailer
Then put the code to config/main.php:
'mailer' => array(
// for smtp
'class' => 'ext.mailer.SmtpMailer',
'server' => 'theserver',
'port' => '25',
'username' => 'theadmin',
'password' => 'thepassword',
// for php mail
'class' => 'ext.mailer.PhpMailer',
),
Then in my controller I wrote this code to send mail:
$to = 'wahaha#gmail.com';
$subject = 'Hello Mailer';
$content = 'Some content';
Yii::app()->mailer->send($to, $subject, $content);
Then the browser gave me the error :
Property "PhpMailer.server" is not defined.
Did I miss something in my code?
In config/main.php
'Smtpmail'=>array(
'class'=>'ext.smtpmail.PHPMailer',
'Host'=>"localhost",
'Username'=>'thesmile1019#gmail.com',
'Password'=>'wakakaka',
'Mailer'=>'smtp',
'Port'=>25,
'SMTPAuth'=>true,
),
In Component/controller.php
public function mailsend($to,$from,$from_name,$subject,$message)
{
$mail = Yii::app()->Smtpmail;
$mail->SetFrom($from,$from_name);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
// Add CC
if(!empty($cc)){
foreach($cc as $email){
$mail->AddCC($email);
}
}
// Add Attchments
if(!empty($attachment)){
foreach($attachment as $attach){
$mail->AddAttachment($attach);
}
}
if(!$mail->Send()) {
return false; // Fail echo "Mailer Error: " . $mail->ErrorInfo;
}else {
return true; // Success
}
}
In controller
public function actionSendMail(){
$token = $_POST['YII_CSRF_TOKEN'];
if ($token !== Yii::app()->getRequest()->getCsrfToken()){
Yii::app()->end();
}
$to = 'thesmile1019#gmail.com';
$from = 'localhost';
$from_name = 'mface';
$subject = 'testing';
$message = 'testing';
if($token == true){
$util = new Utility();
$util->detectMobileBrowser();
$util->checkWebSiteLanguageInCookies();
$this->layout = "masterLayout";
$this->render('mailsend');
$this->mailsend($to,$from,$from_name,$subject,$message);
}else{
print_r("Not Sent");
die();
}
}
Process not problem going correct but didn't receive the mail
Change your port in pathtowebroot/protected/config/main.php
'Smtpmail'=>array(
'class'=>'ext.smtpmail.PHPMailer',
'Host'=>"smtp.gmail.com",
'Username'=>'thesmile1019#gmail.com',
'Password'=>'wakakaka',
'Mailer'=>'smtp',
'Port'=>465,
'SMTPAuth'=>true,
'SMTPSecure' => 'ssl'
),
i have written this code for sending emails
Route::post('contact', function(){
$inputs = Input::all();
$rules = array(
'email' => 'required|email',
'name' => 'required|min:2',
'message' => 'required',
'recaptcha_response_field' => 'required|recaptcha',
);
$validator = Validator::make($inputs, $rules);
if($validator->passes()){
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('message' => Input::get('message'));
$toEmail = 'info#danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
return Redirect::to('/');
}
return Redirect::to('/#contact')->withInput()->withErrors($validator);
});
and my view emails.contact is
<html>
<body>
<p><b>Email From:</b> {{$fromName}} of {{$fromEmail}}</p>
<p><b>Subject:</b> {{$subject}}</p>
<b> Message:</b> <br>
</html> {{$data}}
</body>
but whenever i send email all i get is :
Email From:{{$fromName}} of {{$fromEmail}}
Subject: {{$subject}}
Message:
{{$data}
why my view does not pick up the data from Mail Class?
Make sure your view has the blade extension:
views/emails/contact.blade.php
Your $data array consists of only the variable message. So, in your blade template only {{$message}} will work. Ensure that you are passing all the variables required in your view into the $data array.
$data = array(
'message' => Input::get('message'),
'fromName' => Input::get('name'),
'fromEmail'=> Input::get('email')
);
Now, in your contact.blade.php you can use them as normal variables {{$formName}}, {{$fromEmail}} etc;
Also, since you are using blade templating, ensure that your view file is having proper extension i.e contact.blade.php
i have found a solution to my problem:
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('content' => Input::get('message'));
$toEmail = 'info#danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
and in the view has to be:
<html>
<body>
{{$content}}
</html>
</body>
Important:
Never use {{$message}} in contact view file, according to Taylor Otwell, that is reserved in email view files
Hope this will help someone one day!