In my app there is a little form with subscription to newsletter.
The form has just one field: email.
I want that when the entry is not in a proper email format, instead of throwing a laravel error: Address in mailbox given [fghfghfhf] does not comply with RFC 2822, 3.6.2.
I want to, more elegantly, give a validation error.
How can i define this rule?
Thanks!
EDIT:
This is my NewsletterUser model:
class NewsletterUser extends Eloquent {
protected $table = 'newsletterusers';
protected $guarded = array();
public static $rules = array(
'email' => 'required | email | unique:newsletterusers'
);
public static $messages = array(
'email' => 'You already subscribed',
'empty' => 'Insert the mail, please'
);
}
And this is the subscription method in the Controller:
public function subscription()
{
$input = Input::all();
$validation = Validator::make($input, NewsletterUser::$rules, NewsletterUser::$messages);
if($validation->passes())
{
// subscription stuff
}
return Redirect::back()->withInput()->withErrors($validation)->with('message','Insert the mail, please');
}
Just define a custom error message:
$messages = array(
'email' => 'Address in mailbox given :attribute does not comply with RFC 2822, 3.6.2.',
);
$validator = Validator::make($input, $rules, $messages);
Related
I am working on a web application with multiple users, and I have some issue with sending emails.
Sending is working fine, no problems, but the issue is that every mail interaction between visitors and users is sent to admin email address stated in env file. I am using gmail for sending, Laravel 8.
For example, visitor A has sent a message to user B. User B received mail correctly, but also admin received message.
Thanks for help.
Here is my EmailsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class EmailsController extends Controller
{
public function send(Request $request)
{
function sentence_case($string) {
$sentences = explode(" ",$string);
$sentences = array_reverse($sentences);
$new_string = '';
foreach ($sentences as $key => $sentence) {
$new_string = ucfirst(mb_strtolower(trim($sentence)))." ".$new_string;
}
return $new_string;
}
$this->validate($request, [
'ime' => 'required', //visitor name
'email' => 'required', // visitor email address
'poruka' => 'required' //visitor message
]);
$ime = $request->get('ime');
$ime = sentence_case($ime);
$email = $request->get('email');
$poruka = $request->get('poruka');
Mail::send('emails.message', [
'name' => $ime,
'email' => $email,
'comment' => $poruka ],
function ($m) use ($email) {
$m->from($email);
$m->to('usersemail#yahoo.com', 'MyApp')
->subject('Website Contact Form');
});
/* usersemail#yahoo.com is registered user email address different from aplication admin/owner email addres stated in env file, but admin receives this message */
if (Mail::failures()) {
return response()->Fail('Sorry! Please try again latter');
}else{
return back()->with('success', 'Thanks for contacting me, I will get back to you soon!');
}
}
}
I am creating a simple contact us from in Laravel.
I set up .env with my Gmail account. I can send email from Laravel with my email address, no problem with this.
But, I want to send email from sender address who is sending the message form contact us form.
Here is my code in controller:
public function sendMessage(Request $request)
{
$this->validate($request,[
'email'=>'required|email',
'subject'=>'required',
'body'=>'required|max:150'
]);
$body = $request['body'];
Mail::send('emails.support',['body' => $body], function($message){
$email = Input::get('email');
$subject = Input::get('subject');
$message->sender($email);
$message->to('smartrahat#gmail.com','Mohammed');
$message->subject($subject);
});
return redirect('contactUs');
}
Though, I am getting email address form contact us form, the email always sent form my email account which I configured in .env
I would have done some things different. I guess $body is the email-text? You should put this in a array and add it as a parameter in Mail::send (or directly put $request->all() as a parameter.
Also, inside of the closure of Mail, i don`t thinks its very nice to put logic there (like $email=Input::get). It does not look right if you ask me.
Didn`t test this code, but this should work:
public function sendMessage(Request $request)
{
$this->validate($request,[
'email' => 'required|email',
'subject' => 'required',
'body' => 'required|max:150'
]);
$data = [
'email' => $request->input('email'),
'subject' => $request->input('subject'),
'body' => $request->input('body')
];
Mail::send('emails.support', $data, function($message) use ($data)
{
$message->from($data['email']);
$message->to('smartrahat#gmail.com','Mohammed');
$message->subject($data['subject']);
});
return redirect('contactUs');
}
Because you add $data as as 'use', you can access this data inside the closure. The $data send as a parameter, can be used in the email blade. In this example, you can access the data like this: $email , $subject , $data, in blade these will output the values.
But you can also do it like this:
Mail::send('emails.support', $request->all(), function($message) use ($data)
public function reset_password(Request $request)
{
$user=User::whereEmail($request->email)->first();
if(count($user) == 1)
{
$data = array('name'=>"Virat Gandhi");
$subject=$request->email;
mail::send(['emails'=>'reset_set'],$data,
function($message)use($subject)
{
$message->from('your#gmail.com','kumar');
$message->to($subject);
$message->subject($subject);
});
echo "Basic Email Sent. Check your inbox.";
}
}
foreach ($emails as $value1)
{
$to[]=implode(',',$value1['0']);
}
$subject=$request->input('sub');
$mailsend=Mail::send('email.subscribe',$mail_content,
function($message)use($to,$subject)
{
$message->from('ganarone#ganar.io','Ganar');
$message->to($to);
$message->subject($subject);
});
I have two emails that get sent out, and both are called in a similar fashion. However, there is one major difference: For one of them, the recipient of the CC gets the email, and for the other she does not. I have absolutely NO idea what might be causing this behavior, since they appear to be exactly identical otherwise.
This is the code used to create the email that DOES get the CC applied properly:
public function sendEmail($order = array(), $account = array(), $member = null) {
$email = new CakeEmail();
$email->template('order', 'default')
->helpers(array('Html'))
->viewVars(array('order' => $order, 'account' => $account, 'member' => $member))
->emailFormat('html')
->to($account['Account']['email'])
->cc('katherine#thecommunitycoop.org')
->from('general#thecommunitycoop.com')
->subject('Your order for '.$order['Sale']['sale_date'])
->send();
This is the code used to create to create the email that is NOT getting the CC:
public function pendingEmail($order = array(), $member = false, $success = true) {
$email = new CakeEmail();
$email->template('pending', 'default')
->helpers(array('Html'))
->viewVars(array('order' => $order, 'member'=>$member, 'success' => $success))
->emailFormat('html')
->to($order['Account']['email'])
->cc('katherine#thecommunitycoop.org')
->from('general#thecommunitycoop.com')
->subject('Your order for '.$order['Sale']['sale_date'])
->send();
As you can see, the cc field is identical. Both emails are getting sent to the 'to' field correctly; it is only the cc field that is not working properly on the second one. What could possibly be causing this behavior?
I've got a form that has a single text field (for company):
class Cas_Form_Company extends Zend_Form
{
public function init()
{
$this->addElement('hidden', 'id');
$this->addElement('text', 'name', array('label' => 'Name'));
$this->addElement('submit', 'submit', array('label' => 'Create'));
$name = $this->getElement('name');
$name->addValidator('stringLength', false, array(2,45));
$name->addValidator(new Cas_Model_Validate_CompanyUnique());
$this->setMethod('post');
$this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/Company/Submit');
}
public function SetFromExistingCompany(Cas_Model_Company $company)
{
$this->getElement('id')->setValue($company->GetId());
$this->getElement('name')->setValue($company->GetName());
$this->getElement('submit')->setLabel('Edit');
$this->addElement('submit', 'delete', array('label' => 'Delete', 'value' => 'delete'));
}
public function Commit()
{
if (!$this->valid())
{
throw new Exception('Company form is not valid.');
}
$data = $this->getValues();
if (empty($data['id']))
{
Cas_Model_Gateway_Company::FindOrCreateByName($data['name']);
}
else
{
$company = Cas_Model_Gateway_Company::FindById((int)$data['id']);
$company->SetName($data['name']);
Cas_Model_Gateway_Company::Commit($company);
}
}
}
I've also created a little validator which enforces that I want companies to have unique names:
class Cas_Model_Validate_CompanyUnique extends Zend_Validate_Abstract
{
protected $_messageTemplates = array(
'exists' => '\'%value%\' is already a company.'
);
/**
* #param string $value
* #return bool
*/
public function isValid($value)
{
$this->_setValue($value);
$company = Cas_Model_Gateway_Company::FindByName($value);
if ($company)
{
$this->_error('exists');
return false;
}
return true;
}
}
Now, this works just fine for creating new companies. The problem comes in when I want to allow editing of companies. This is because for an edit operation, while the company name needs to be unique, a form containing the name already pertaining to the given ID isn't an edit at all (and therefore is valid). That is, the form is valid if either the name doesn't already exist in the database, or the name given matches the name already assigned to that ID.
However, writing this as a validator seems to be problematic, because the validator only gets the value it's working on -- not the ID in question.
How does one write a validator for this sort of thing?
You can use the poorly documented second $context argument to isValid().
See http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.validators and scroll down to the note "Validation Context"
I think this link may help you.
Zend Form Edit and Zend_Validate_Db_NoRecordExists
You have to user Db no record exist but for edit you can specify the exclude attribute in validation.
I have the following code to generate an input field for user's email address
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')
->addFilters(array('StringTrim', 'StripTags'))
->addValidator('EmailAddress')
->addValidator(new Zend_Validate_Db_NoRecordExists(
array(
'adapter'=>Zend_Registry::get('user_db'),
'field'=>'email',
'table'=>'tbl_user'
)))
->setRequired(true)
->setDecorators(array(
array('Label', array('escape'=>false, 'placement'=>'append')),
array('ViewHelper'),
array('Errors'),
array('Description',array('escape'=>false,'tag'=>'div')),
array('HtmlTag', array('tag' => 'div')),
));
$this->addElement($email);
now the problem is if user enter invalid hostname for email, it generate 3 errors. lets say user enter 'admin#l' as email address, and the errors will be
* 'l' is no valid hostname for email address 'admin#l'
* 'l' does not match the expected structure for a DNS hostname
* 'l' appears to be a local network name but local network names are not allowed
I just want it to give only one custom error instead of all these. If I set error message "Invalid Email Address" by addErrorMessage method, it will again generate the same message against the db_validation.
Well, it's a late answer but I think is always useful.
Simply add true as second param of addValidator()
From Zend docs (http://framework.zend.com/apidoc/1.8/):
addValidator (line 67)
Adds a validator to the end of the chain
If $breakChainOnFailure is true, then if the validator fails, the next
validator in the chain, if one exists, will not be executed.
return: Provides a fluent interface
access: public
Here the signature:
Zend_Validate addValidator (Zend_Validate_Interface $validator, [boolean $breakChainOnFailure = false])
Zend_Validate_Interface $validator
boolean $breakChainOnFailure
So the code is:
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')
->addFilters(array('StringTrim', 'StripTags'))
->addValidator('EmailAddress', TRUE ) // added true here
->addValidator(new Zend_Validate_Db_NoRecordExists(
array(
'adapter'=>Zend_Registry::get('user_db'),
'field'=>'email',
'table'=>'tbl_user'
), TRUE )
);
You have to create an instance of the Zend_Validate_EmailAddress class and call the setMessages method and then override the messages that you like, to remove the ones that you mention it would be something like this:
$emailValidator->setMessages(array(
Zend_Validate_EmailAddress::INVALID_FORMAT => "Your error message",
Zend_Validate_Hostname::INVALID_HOSTNAME => "Your error message",
Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED => "Your error message"
));
I hope this help somebody :-)
$email->addErrorMessage("Please Enter Valid Email Address");
you can use custom validator. create a file Email.php inside folder Validate in your library folder at the root of project
class Validate_Email extends Zend_Validate_Abstract
{
const INVALID = 'Email is required';
protected $_messageTemplates = array(
self::INVALID => "Invalid Email Address",
self::ALREADYUSED => "Email is already registered"
);
public function isValid($value)
{
if(preg_match($email_regex, trim($value))){
$dataModel = new Application_Model_Data(); //check if the email exists
if(!$dataModel->email_exists($value)){
return true;
}
else{
$this->_error(self::ALREADYUSED);
return false;
}
}
else
{
$this->_error(self::INVALID);
return false;
}
}
}
and in you form.php file
$mailValidator = new Validate_Email();
$email->addValidator($mailValidator, true);
Don't know if it works or not but for me it worked in case of telephone. Courtesy of http://softwareobjects.net/technology/other/zend-framework-1-10-7-telephone-validator/
It seems to be missing quite a few lines...
probably should use this:
$mailValidator = new Zend_Validate_EmailAddress();
you can also do some other validations see here: http://framework.zend.com/manual/en/zend.validate.set.html
Using a custom validator is the only way I found to avoid this problem.
If what you want is:
Having only one error message if the email address is in a wrong format
If the format is good, then validate if the email address is already in the database
Then I suggest you to do something like this:
$where = array('users', 'email', array('field' => 'user_id',
'value' => $this->getAttrib('user_id')));
$email = new Zend_Form_Element_Text('email');
$email->setLabel('E-mail:')
->setRequired(true)
->setAttrib('required name', 'email') // html5
->setAttrib('maxlength', '50')
->addFilter('StripTags')
->addFilter('StringTrim')
->addFilter('StringToLower')
->addValidator('email', true)
->addValidator('stringLength', true, array(1, 50))
->addValidator('db_NoRecordExists', true, $where)
->addDecorators($this->_elementDecorators);
$this->addElement($email);
$this->getAttrib('user_id') represents the current user's id.
There are three validators here, all of them have their second parameter $breakOnFailureset to false, so if a validator fails, the other ones won't be called.
The first validator is email, which is my own custom validator:
class My_Validate_Email extends Zend_Validate_EmailAddress
{
public function getMessages()
{
return array('invalidEmail' => 'Your email address is not valid.');
}
}
You can add this validator in your library, in /application/library/My/Validate for example, and then add
$this->addElementPrefixPath('My_Validate', 'My/Validate', 'validator');
into your form. Of course, you need to replace "My" by the name of your library.
Now if an email is in the wrong format, it will always display 'Your email address is not valid.'. If your email is too long and doesn't fit into your database field (VARCHAR(100) for example), it's going to show your stringLength validator errors, and in the last case, if an entry already exists in the database, only this error will be shown.
Of course you can add more methods into your custom validator and overload setMessages, so that you can display your own messages whatever the form you are working on.
Hope it can help someone!