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/
Related
i have two rest api end points for the Calculator and another for the email submission.
i want to include the data from calculator to the email handler function.
Calculator Handler:
// function for the endpoint for form
add_action('rest_api_init', 'create_rest_endpoint');
function create_rest_endpoint() {
// Create endpoint for front end to connect to WordPress securely to post form data
register_rest_route('bohio/v1', 'submit', array(
'methods' => 'POST',
'callback' => 'handle_query',
'permission_callback' => '__return_true'
));
}
// callback method
function handle_query($data) {
// Handle the form data that is posted
// Get all parameters from form
$output = $data->get_params();
// form fields : service, bed, bath,square_feet,cadence
}
Email handler:
function handle_email($data) {
// Handle the form data that is posted
// Get all parameters from form
$emailData = $data->get_params();
// Set fields from the form
$field_name = sanitize_text_field($emailData['name']);
$field_email = sanitize_email($emailData['email']);
//Check if nonce is valid, if not, respond back with error
if (!wp_verify_nonce($emailData['_wpnonce'], 'wp_rest')) {
return new WP_Rest_Response('Message not sent', 422);
}
else {
// Remove unneeded data from paramaters
unset($emailData['_wpnonce']);
unset($emailData['_wp_http_referer']);
// Send the email message
$headers = [];
// $admin_email = get_bloginfo('admin_email');
$admin_email = get_option('bh_email_sub');
// var_dump($admin_email);
$from_email = get_option('bh_email_sub');
$admin_name = get_option('bh_admin_sub_name');
// $admin_name = get_bloginfo('name');
// Set admin email as recipient email if no option has been set
$recipient_email = $admin_email;
$headers[] = "From: {$admin_name} <{$from_email}>";
$headers[] = "Reply-to: {$field_name} <{$field_email}>";
$headers[] = "Content-Type: text/html";
$subject = "New email submission from {$field_name}";
$message = '';
$message = "<h2>New Email submission from {$field_name}</h2>";
// Loop through each field posted and sanitize it
foreach ($emailData as $label => $value) {
switch ($label) {
case 'message':
$value = sanitize_textarea_field($value);
break;
case 'email':
$value = sanitize_email($value);
break;
default:
$value = sanitize_text_field($value);
}
$message .= '<strong>' . sanitize_text_field(ucfirst($label)) . ':</strong> ' . $value . '<br />';
}
wp_mail($recipient_email, $subject, $message, $headers);
$confirmation_message = "The message was sent successfully!!";
// return $confirmation_message;
return $confirmation_message;
}
}
How would i access the data from handle_query to handle_email and send via email. Both functions lies in the same file of a plugin.
when I try to send a whatsapp message to a mobile number that isn't registered on whatsapp, how do I know that it failed? because I want to send the message using regular SMS instead. but my code below doesn't give any different result between success and failed process:
public function sendMessage($to, $msg, $params=[])
{
$client = new Client($this->sid, $this->token);
$from = $this->from_number; // my twilio number e.g. +1786xxxx
if ( ! empty($params['via']) && $params['via'] == 'whatsapp') {
$to = 'whatsapp:'.$to;
$from = 'whatsapp:'.$from;
}
$options = [
// A Twilio phone number you purchased at twilio.com/console
'from' => $from,
// the body of the text message you'd like to send
'body' => $msg,
];
// Use the client to do fun stuff like send text messages!
$response = $client->messages->create(
$to,
$options,
);
return $response;
}
// end public function sendMessage
public function do_send_msg()
{
$to = '+628123456789';
// this message already uses the same format as the approved message template
$msg = "Your otp code for Login Process is 123456";
$params = [
'via' => 'whatsapp',
];
$send = $this->twilio->sendMessage('+628123456789', $msg, $params);
var_dump($send->status);
}
I wanted to make the code like this instead but this code is unable to differentiate the value of $send->status whether it's successful or failed:
public function do_send_msg()
{
$to = '+628123456789';
// this message already uses the same format as the approved message template
$msg = "Your otp code for Login Process is 123456";
$params = [
'via' => 'whatsapp',
];
$send = $this->sendMessage($to, $msg, $params);
// if sending via whatsapp failed, try sending via regular SMS instead
if ( ! $send->status ) {
$params['via'] = 'SMS';
$send = $this->sendMessage($to, $msg, $params);
}
}
I'm afraid Meta/WhatsApp doesn't expose this information at this point in time. Therefore, I'd recommend that you let the users choose whether they want to receive a WhatsApp message or a regular SMS.
I have built a PHP form, but want an email to be sent to whatever country the user chooses on a dropdown.
E.g. If they choose UK on dropdown, send an email to our UK account. If they choose US, send to our US account etc...
The entire form is working perfectly at the moment, I just need this little feature to work then it would be perfect. Thank you for looking, its appreciated!
This is my code so far:-
<?php
// require ReCaptcha class
require('recaptcha-master/src/autoload.php');
// configure
// an email address that will be in the From field of the email.
$from = 'A new client has registered their details <noreply#emailaddress.com>';
// an email address that will receive the email with the output of the form
$sendTo = '<scott#emailaddress.com>';
// subject of the email
$subject = 'New Registered Form:';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = [
'firstname' => 'First Name', 'lastname' => 'Last Name', 'company' => 'Company', 'email' => 'Email Address', 'jobrole' => 'Job Role',
'postcode' => 'Postcode', 'country' => 'Country',
];
// message that will be displayed when everything is OK :)
$okMessage = 'Thank you for registering.';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
// ReCaptch Secret
$recaptchaSecret = 'AAAA';
// let's do the sending
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if ( ! empty($_POST))
{
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if ( ! isset($_POST['g-recaptcha-response']))
{
throw new \Exception('ReCaptcha is not set.');
}
// do not forget to enter your secret key from https://www.google.com/recaptcha/admin
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost);
// we validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if ( ! $response->isSuccess())
{
throw new \Exception('ReCaptcha was not validated.');
}
// everything went well, we can compose the message, as usually
$emailText = "This person has registered their details \n=============================\n";
foreach ($_POST as $key => $value)
{
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key]))
{
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = [
'Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
];
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = ['type' => 'success', 'message' => $okMessage];
}
}
catch (\Exception $e)
{
$responseArray = ['type' => 'danger', 'message' => $e->getMessage()];
}
if ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else
{
echo $responseArray['message'];
}
?>
Thank you very much in advance!!
Scott Geere
Personally I would do something like this:
switch ($_POST['country']):
case 'UK':
$sendTo = '<UK#emailaddress.com>';
break;
case 'US';
$sendTo = '<US#emailaddress.com>';
break;
default:
$sendTo = '<scott#emailaddress.com>';
endswitch;
Which means you could change:
// an email address that will receive the email with the output of the form
//$sendTo = '<helena#dropbox.com>,<l.stone#emeraldcolour.com>';
$sendTo = '<scott#emailaddress.com>';
To:
// an email address that will receive the email with the output of the form
//$sendTo = '<helena#dropbox.com>,<l.stone#emeraldcolour.com>';
switch ($_POST['send_to']):
case 'UK':
$sendTo = '<UK#emailaddress.com>';
break;
case 'US';
$sendTo = '<US#emailaddress.com>';
break;
default:
$sendTo = '<scott#emailaddress.com>';
endswitch;
Please do not forget: never trust the user. So do not just do stuff on $_POST data, make sure you validate the given input before you use it.
Another side note:
Instead of using this raw code in yours, you could make it a function (so you can reuse it somewhere else as well).
For example:
function getSendToEmail($country)
{
switch ($country):
case 'UK':
return '<UK#emailaddress.com>';
break;
case 'US';
return '<US#emailaddress.com>';
break;
default:
return '<scott#emailaddress.com>';
endswitch;
}
// an email address that will receive the email with the output of the form
//$sendTo = '<helena#dropbox.com>,<l.stone#emeraldcolour.com>';
$sendTo = $this->getSendToEmail($_POST['country']);
Documentation:
http://php.net/manual/en/control-structures.switch.php // Switch
http://php.net/manual/en/functions.user-defined.php // Functions
http://php.net/manual/en/filter.examples.validation.php // Validation
if (isset($_POST['country'])) {
$country = $_POST['country'];
if ($country === 'France') {
$sendTo = 'france#emailadress.com';
} elseif ($country === 'England') {
$sendTo = 'england#emailadress.com';
}
}
You can put it before the mail function.
You can also use an array like that:
$emailList = [
'France' => 'france#emailadress.com',
'England' => 'england#emailadress.com'
];
if (isset($_POST['country'])) {
// Get email from the key
$sendTo = $emailList[$_POST['country']];
}
How can I check the receiver email is real or not on mail send in CodeIgniter3? And if it is not available or not real [fake] mail address then show the alert box with Mail is not available.
For example : When we send mail in gmail with fake mail address [which is not available or not real] then gmail reply with below text.
Google tried to deliver your message, but it was rejected by the server for the recipient domain example.com by mta5.am0.example.net.
EDIT 1
Here is my email send code. Please guide me where to add that code. I added as you said but it show
undefined email variable.
My email send code below :
<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
* SENDS EMAIL WITH GMAIL
*/
class Emailsend extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('emailsend_model');
}
function index()
{
$mail_setting = $this->emailsend_model->getMailSetting();
$config = Array(
'protocol' => 'smtp',
//'smtp_host' => $mail_setting->smtp_server_name,
'smtp_host' => 'ssl://smtp.googlemail.com',
//'smtp_port' => 465,
'smtp_port' => 465,
//'smtp_user' => $mail_setting->smtp_user_name,
'smtp_user' => 'kzwkyawzinwai#gmail.com',
//'smtp_pass' => $mail_setting->smtp_password,
'smtp_pass' => 'mypassword',
'smtp_timeout' => '7',
'mailtype' => 'text',
'validation' => TRUE,
'charset' => 'utf-8',
'newline' => "\r\n",
'wordwrap' => TRUE,
'crlf' => "\r\n",
'newline' => "\r\n",
'dsn' => TRUE
);
$this->load->library('email', $config);
date_default_timezone_set("Asia/Tokyo");
$day = date('Y-m-d');
//$hour = date('H:i:00');
//$mail_informs = $this->emailsend_model->getSendMailInfo($day, $hour);
$mail_informs = $this->emailsend_model->getSendMailInfo($day);
if(!empty($mail_informs)){
function calculate_string($mathString) {
$mathString = trim($mathString);
$mathString = preg_replace('/[^0-9.\+\-\*\/\(\)]/', '', $mathString);
$compute = create_function("", "return (". $mathString .");");
return 0 + $compute();
}
foreach($mail_informs as $mail_inform)
{
$message = $mail_inform->subject;
$search_word = array("(氏名)", "(メールアドレス)", "(登録日)");
$mail_date = str_replace("-", '/', $mail_inform->insert_date);
$replace_word = array($mail_inform->user_name, $mail_inform->mail_addr, $mail_date);
$item_informs = $this->emailsend_model->getAllOriginalItemsById($mail_inform->user_plan_detail_id);
foreach($item_informs as $item_inform){
$item_name = "(". $item_inform->item_name . ")";
if($item_inform->data_type==2){
$item_value_date = $item_inform->item_value;
$item_value = str_replace("-", "/", $item_value_date);
}
else if($item_inform->data_type==1){
$item_value = $item_inform->item_value;
$item_name = str_replace(str_split('()'), '', $item_name);
}
else if($item_inform->data_type==0){
$item_value = $item_inform->item_value;
}
array_push($search_word, $item_name);
array_push($replace_word, $item_value);
}
$result_message = str_replace($search_word, $replace_word, $message);
preg_match_all("/\(([^)]*)\)/", $result_message, $matches);
$search_matches = $matches[0];
$cal_arr = $matches[1];
$cal_count = count($cal_arr);
$replace_matches = array();
for($i=0;$i<$cal_count;$i++)
{
$cal_val = calculate_string($cal_arr["$i"]);
array_push($replace_matches, $cal_val);
}
$final_message = str_replace($search_matches, $replace_matches, $result_message);
$this->email->set_newline("\r\n");
$this->email->from( $mail_setting->sender_mail_addr);
$this->email->to($mail_inform->mail_addr);
$this->email->subject(mb_convert_encoding($mail_inform->title, "UTF-8"));
$this->email->message(mb_convert_encoding($final_message, "UTF-8"));
$path = __DIR__;
$file = $path . '/../../uploads/'.$mail_inform->tpl_id.'/'.$mail_inform->file_attachment;
if(!empty($mail_inform->file_attachment)) {
$this->email->attach($file);
}
$r=#$this->email->send();
if (!$r) {
?>
<script type="text/javascript">
alert("Send Failed");
</script>
<?php
show_error($this->email->print_debugger());
}
else{
?>
<script type="text/javascript">
alert("Send Successfully");
</script>
<?php
echo $this->email->print_debugger();
//show_error($this->email->print_debugger());
}
/*if($r) {
$status = 2; // 送信済み
$id = $mail_inform->user_plan_detail_id;
$this->emailsend_model->setStatus($id, $status);
?>
<script type="text/javascript">
alert("Send Successfully");
</script>
<?php
}
else {
$status = 1; // 送信失敗
$id = $mail_inform->user_plan_detail_id;
$this->emailsend_model->setStatus($id, $status);
?>
<script type="text/javascript">
alert("Send Failed");
</script>
<?php
show_error($this->email->print_debugger());
}*/
$this->email->clear(TRUE);
}
}
}
}
The other possible solution (outside codeigniter) to consume public api like https://mailjagger.com/api/validate/rajehs#mta5.am0.example.net that would return true or false. And just execute mail sending section once received true.
Update:
Create a php file in your library folder named Genuinemail.php. contents as follows
class Genuinemail {
public function __construct() {
// Do your stuff with $arr
}
/**
* verify email format, dns and banned emails
* #param string $email
* #return mixed bool true if correct / string
*/
public static function check($email)
{
//get the email to check up, clean it
$email = filter_var($email,FILTER_SANITIZE_STRING);
// 1 - check valid email format using RFC 822
if (filter_var($email, FILTER_VALIDATE_EMAIL)===FALSE)
return 'No valid email format';
//get email domain to work in nexts checks
$email_domain = preg_replace('/^[^#]++#/', '', $email);
// 2 - check if its from banned domains.
if (in_array($email_domain,self::get_banned_domains()))
return 'Banned domain '.$email_domain;
// 3 - check DNS for MX records
if ((bool) checkdnsrr($email_domain, 'MX')==FALSE)
return 'DNS MX not found for domain '.$email_domain;
// 4 - wow actually a real email! congrats ;)
return TRUE;
}
/**
* gets the array of not allowed domains for emails, reads from json stores file for 1 week
* #return array
* #see banned domains https://github.com/ivolo/disposable-email-domains/blob/master/index.json
* #return array
*/
private static function get_banned_domains()
{
//where we store the banned domains
$file = 'banned_domains.json';
//if the json file is not in local or the file exists but is older than 1 week, regenerate the json
if (!file_exists($file) OR (file_exists($file) AND filemtime($file) < strtotime('-1 week')) )
{
$banned_domains = file_get_contents("https://rawgit.com/ivolo/disposable-email-domains/master/index.json");
if ($banned_domains !== FALSE)
file_put_contents($file,$banned_domains,LOCK_EX);
}
else//get the domains from the file
$banned_domains = file_get_contents($file);
return json_decode($banned_domains);
}
}
This library can be used like this sample method
function checkSpam($email)
{
$this->load->library('genuinemail');
$check = $this->genuinemail->check($email);
if($check===TRUE) return true;
return false;
}
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)