Im trying to send mail from laravel and when i add the dynamic from field i get this error:
"Expected response code 250 but got code "501", with message "501 A syntax error was encountered in command argument.."
this is the code:
$user = Input::get('user');
Mail::send('template.contact', $user , function($message) use ($user)
{
$email = $user['email'];
$message->from($email , 'name'); thats doesnt
//$message->from('us#example.com', 'Laravel'); that work
$message->to('test#gmail.com', 'contact us' )->subject($user['subject']);
});
and the user is coming from angular -
service:
this.sendConatctMail = function(data) {
return $http.post('send-contact-mail', {user: data});
}
and controller:
contactService.sendConatctMail($scope.user);
Here's one way you can solve this.
Let's assume the data on this.sendConatctMail = function(data) { is a object like this:
var data {
email: 'some#email.com',
// other field: values
}
Right before you post it, you should convert it into JSON string like this:
return $http.post('send-contact-mail', {user: JSON.stringify(data)});
Then on Laravel/PHP side, decode that back into an array and use it like this:
if (Input::has('user'))
{
// Decode json string
$user = #json_decode(Input::get('user'), true);
// Proceed if json decoding was success
if ($user)
{
// send email
Mail::send('template.contact', $user , function($message) use (&$user)
{
$message->from($user['email'], 'name')
->to('test#gmail.com', 'contact us')
->subject($user['subject']);
});
}
}
I don't know if this will help you, but whenever I send emails through Laravel, I need to alter the use(...) section a bit. You have:
$user = Input::get('user');
Mail::send('template.contact', $user , function($message) use ($user)
...
Try changing it to this and see what happens:
$user = Input::get('user');
Mail::send('template.contact', array('user' => $user), function($message) use (&$user) {
$message->from($user['email'], $user['name']);
$message->to('test#gmail.com', 'contact us')->subject($user['subject']);
}
2 changes I made:
array('user' => $user)
and
use(&$user)
I don't know if that will help you, but I have working emails on my application that look almost identical to yours, except for the &$variable instead of just $variable
Good luck!
The problem is with your $email = $user['email'];
try checking your $user['email']
if $message->from('us#example.com', 'Laravel'); this works
then
$email = $user['email'];
$message->from($email , 'name');
this must work...
I have tried the same without much trouble...
In my application I needed to pass the _token variable in all my ajax request any such problems??
Related
I want to make my mail more detailed when the user has sent a forgot password reset link to his/her email. This is the sample of the picture when receiving a reset password link.
I want to add some details here that the Hello should be Hello! (user name here)
Here is the code that I added in my SendsPasswordResetEmails.php
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email')
);
$applicant_name = Applicant::where('email', $request->email)->get()->value('name');
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
}
and it should pass the data to app\Notifications\ApplicantResetPasswordNotification.php
public function toMail($notifiable)
{
return (new MailMessage)
->from('vcc3dummy#gmail.com', 'CCV3')
->greeting('Hello! Applicant Name') // Applicant name pass here
->line('You are receiving this email because we received a password request for your account.')
->action('Click here to Reset Password', route('applicant.reset', $this->token))
->line('If you did not reset your password, no further action is required.');
}
Looking for help on how to pass the data or how to query it.
Would appreciate if someone could help me
Thanks in advance.
In your ApplicationResetPasswordNotification.php you can use the $notifiable variable as follows:
public function toMail($notifiable)
{
return (new MailMessage)
->from('vcc3dummy#gmail.com', 'CCV3')
->greeting('Hello!' . $notifiable->name) // Applicant name
...
}
Please mark as answer if that works for you!
This is the another way to send the mail in laravel -
Put that data you want to use/show in email template.
$data = [
'email' => $email,
'remember_token' => $remember_token,
'name' => $applicant_name
];
Mail::send('emails/forgotmail', $data, function ($message) use ($data) {
$message->from('youremail#gmail.com');
$message->to( $data['email'] )->subject('Forgot Password Link');
});
Where as 'email/forgotmail' is in 'resources/views/email/forgotmail.blade.php' that you have to create. So that here you can put your email template over here and make use of $data in it .
I try to make, send SMS using simple-sms Laravel package. Then user activated he get sms message with text: Activated!, but i dont getting sms by tag $phone, if i use phone like that: "+4425652125", everything works fine. My code looks like that:
public function activate($token, User $user)
{
$user = User::whereActivationToken($token)->firstOrFail();
$user->confirmEmail();
$phone = $user->phone;
flash()->success(trans('frontend.account.activated'));
SMS::send('Your SMS Message', null, function($sms) use ($phone) {
$sms->to($phone);
});
}
Any ideas?
I want to send email to multiple users. Here is my email sending code.
$email_id = User::select('email_id')->get();
Mail::send('test' , array('user' => $email_id) , function ($message) {
$message -> to('xyz#gmail.com') -> subject ('Welcome!!!');
});
I am getting array of email_id while printing $email_id and I pass $email_id to array. but it's not working.
Any help would be grateful.
Thank You.
Please check mention url you can use this
Laravel Mail::send() sending to multiple to or bcc addresses
https://codedump.io/share/7tL5qbeUwnKT/1/laravel-mailsend-sending-to-multiple-to-or-bcc-addresses
Try this:
$email_id = User::select('email_id')->get()->toArray();
Mail::send('test' , array('user' => $email_id) , function ($message) {
$message -> to('xyz#gmail.com') -> subject ('Welcome!!!');
});
I am stuck on the point where i want to send an email using ajax. Find code below.
$user = \Auth::user();
Mail::send('emails.reminder', ['user' => $user], function ($message) use ($user) {
$message->from('xyz#gmail.com', 'From');
$message->to($request['to'], $name = null);
// $message->cc($address, $name = null);
// $message->bcc($address, $name = null);
$message->replyTo('xyz#gmail.com', 'Sender');
$message->subject($request['subject']);
// $message->priority($level);
if(isset($request['attachment'])){
$message->attach($request['attachment'], $options = []);
}
// Attach a file from a raw $data string...
$message->attachData($request['message'], $name = 'Dummy name', $options = []);
// Get the underlying SwiftMailer message instance...
$message->getSwiftMessage();
});
return \Response::json(['response' => 200]);
Now i want to send this email using ajax and upon request complete i want to display message on same page that your message sent successfully.
i found the solution for this, but this is to send plain message directly using Mail::raw() function which is working perfectly. But i am not able to attach files here or some html codes.
Any suggestion in this regard.
I have the following code in my controller trying to get it to work before adding validation etc
This code is from here however I will be adapting it anyway
$email = $_POST['Newsletter[email]'];
$session->save_email($email);
// Subscribe User to List
$api_key = new sammaye\mailchimp\Mailchimp(['apikey' => 'xxxxxxxxxxx']);
$list_id = "xxxxxxxxxxx";
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );
However I get the following error (Does this mean my most data is in an array newbie)
Undefined index: Newsletter[email]
Is this something I need to set within my yii2 form so that instead of the name field being Newsletter[email] its just email?
You can do this as follows:
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$email = $data['NewsLetter']['email'];
}
As was already stated you can either use $_POST or the request-object.
That object encompasses everything that enters your application on startup. So yes, \Yii::$app->request->post() gives you all incoming POST-data and \Yii::$app->request->post('name') will give you a single one. Same with the get function.
However given your code that is not how you should be using Yii. The name of your variables suggests that the post is done using a model, so you might want to use that again, makes it a lot easier on validation.
If you don't have the model, it can look like so:
class Newsletter extends \yii\base\Model
{
public $email;
public function rules()
{
return array(
array('email', 'email', 'skipOnEmpty' => false)
);
}
}
The actual code in your controller could be more amongst the lines:
$request = \Yii::$app->request;
if ($request->isPost) {
$newsletter = new Newsletter;
if ($newsletter->load($request->post()) && $newsletter->validate()) {
// do your thing
}
}
It means, it is inside an array.
Try to do the following instead :
// Using isset will make sure, you don't trigger a Notice (when the variable does not exist)
$email = isset($_POST['Newsletter']['email']) ? $_POST['Newsletter']['email'] : null;
// Make sure you are receiving an email address
if ($email && filter_var($email, FILTER_VALIDATE_EMAIL))
{
$session->save_email($email);
// Subscribe User to List
$api_key = new sammaye\mailchimp\Mailchimp(['apikey' => 'xxxxxxxxxxx']);
$list_id = "xxxxxxxxxxx";
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );
}
// Display an error message ?
else
{
// #todo
}
If the HTML form field is as below
<input name="Newsletter[email]" type="text">
then the code with in controller should be
$data = Yii::$app->request->post();
$email= $data['Newsletter']['email'];
The inline solution is:
if(is_null($email = Yii::$app->request->post('Newsletter')['email']))
throw new BadRequestHttpException('newsletter email must set');
if so if email isn't set it throws Bad Request and notice that $_POST isn't good solution when you are using a PHP framework and it's security isn't provided by framework but Yii::$app->request->post is secured by Yii.
You should simply try $_POST['Newsletter']['email'].