how to edit email body before sending using Laravel - php

I have generated email function, that sends email to multiple people using laravel.
Now I want to generate an edit window so that i can write the body of email,like in Gmail, if we are sending mail, we first edit body and hit send mail.
So, if anyone know how can I implement this, leave a comment.

It should be as simple as
Mail::send([], array('yourValue' => $yourValue), function($message) use ($yourValue) {
$MailBody = 'Your Custom Body';
$message->setBody($MailBody, 'text/html');
$message->to('yourtoaddress#yourdomain.com');
$message->subject('Your Custom Subject');
});

Though I am fairly new to Laravel myself, I could try to help you out with this. Firstly, set up your routes in the Routes.php file. For e.g.
Route::get('myapp/sendEmail', 'EmailController#returnComposeEmail');
Route::post('myapp/sendEmail', 'EmailController#sendEmail');
The first route when visited should return a view to the user where he can compose his email. This basically is a form which will be submitted by the POST method when the user clicks the 'Send' button. The second route is for that method which would collect the submitted data and use it appropriately then to send the email.
If you go by the routes I have provided, you should have a controller file named EmailController.php with the following methods:
public function returnComposeEmail()
{
return view('pages.ComposeEmail');
}
public function sendEmail(Request $input)
{
$input = $input->all();
$dataArray = array();
$dataArray['emailBody'] = $input['emailBody'];
$to = $input['to'];
$subject = $input['subject'];
Mail::send('email.body', ['dataArray' => $dataArray], function ($instance) use ($to, $subject)
{
$instance->from(env('MAIL_USERNAME'), 'Your Name Here');
$instance->to($to, 'Recipient Name');
$instance->subject($subject);
$instance->replyTo(env('MAIL_REPLY_TO', 'some#email.id'), 'Desired Name');
});
}
You may use use the $dataArray in the email/body.blade.php file as is or as per your requirement.
Do let me know if I could be of help. :-)

Controller:
public function showForm(Request $request )
{
//Get Content From The Form
$name = $request->input('name');
$email = Input::get('agree');
$message = $request->input('message');
//Make a Data Array
$data = array(
'name' => $name,
'email' => $email,
'message' => $message
);
//Convert the view into a string
$emailView = View::make('contactemail')->with('data', $data);
$contents = (string) $emailView;
//Store the content on a file with .blad.php extension in the view/email folder
$myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
fwrite($myfile, $contents);
fclose($myfile);
//Use the create file as view for Mail function and send the email
Mail::send('emails.email', $data, function($message) use ($data) {
$message->to( $data['email'], 'Engage')->from('stifan#xyz.com')->subject('A Very Warm Welcome');
});
// return view();
}
Routes:
Route::post('contactform', 'ClientsController#showForm');
Route::get('/', 'ClientsController#profile');
The view contactemail has the data to be sent, and the view email we are sending through mail function. When the user puts data in the form, that data will get saved in email.blade.php because of these lines of code:
//Convert the view into a string
$emailView = View::make('contactemail')->with('data', $data);
$contents = (string) $emailView;
//Store the content on a file with .blad.php extension in the view/email folder
$myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
fwrite($myfile, $contents);
fclose($myfile);

Related

sending multiple emails using queue - laravel 7

I need to send bulk emails using Laravel queue and jobs. If I understood, my method this way should dispatch 1 job where all the emails are fetched and send it one by one going through the foreach loop, right? Somehow, only one email got send. And when I check the message, it appears the recipient message is in this format - "test2#gmail.com" <test1#gmail.com>. Only test1 email account received the email. I am not sure what causing it. Thank you for your help.
Controller
$body = $request->body;
$titleName = $request->subject;
$job = (new \App\Jobs\SendQueueEmail($body, $titleName))
->delay(now()->addSeconds(2));
dispatch($job);
Job
public function handle(Request $request)
{
$emailsAlumni = ['test1#gmail.com', 'test2#gmail.com'];
$date = Carbon::now()->format('d M Y');
$data = [
"body" => $this->body,
"date" => $date
];
foreach ($emailsAlumni as $email) {
Mail::send('main.admin.email.general', $data, function ($message) use ($email) {
$message->to($email);
$message->subject('title');
});
}
}
You don't need to loop whole Mail instance you can just try it as
Mail::send('main.admin.email.general', $data, function ($message) use ($emailsAlumni) {
$message->to($emailsAlumni);
$message->subject('title');
});

Send link with variable using Zend_Mail

I am triyng to send a email with a link when the user complete registration.
The link should have a variable $id with the id of the user.
I tried different things but my link always appear as
http://localhost/users-data/activate/.php?id=>
I am using Zend_Mail.
What I am trying to do, is for example: send to user id =1 a link http://localhost/users-data/activate1. For then I can take the last number of url, which should correspond to id, and set the status to this user in my activate script.
Could you show me what I doing wrong?
This is my registerAction
public function registerAction()
{
// action body
$request = $this->getRequest();
$form = new Application_Form_UsersData();
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
$comment = new Application_Model_UsersData($form->getValues());
$mapper = new Application_Model_UsersDataMapper();
$mapper->save($comment);
// send email
$id = $comment -> getId();
$formValues = $this->_request->getParams();
$mail = new Application_Model_Mail();
$mail->sendActivationEmail($formValues['email'], $id,$formValues['name']);
$this->_redirect('/users-data/regsuccess');
}
}
$this->view->form = $form;
}
This is my Application_Model_Mail
class Application_Model_Mail
{
public function sendActivationEmail($email, $id,$name)
{
require_once('Zend/Mail/Transport/Smtp.php');
require_once 'Zend/Mail.php';
$config = array('auth' => 'login',
'username' => '*******#gmail.com',
'password' => '******',
'port' => '587',
'ssl' => 'tls');
$tr = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$mail->setBodyText('Please click the following link to activate your account '
. '<a http://localhost/users-data/activate/.php?id='.$id.'>'.$id.'</a>')
->setFrom('admin#yourwebsite.com', 'Website Name Admin')
->addTo($email, $name)
->setSubject('Registration Success at Website Name')
->send($tr);
}
}
You go from $request to $_request. The latter is not defined anywhere, so its values are null.
Try this
$mail->setBodyHtml("Please click the following link to activate your".
"account<a href='http://localhost/users-data/activate.php?id=$id'>$id</a>")
The HTML link you create is incorrect:
'<a http://localhost/users-data/activate/.php?id='.$id.'>'
In HTML, a valid link is (note the href attribute):
...
So your script should construct the link like this:
'<a href="http://localhost/users-data/activate/.php?id='.$id.'">'
Also, I suppose http://localhost/users-data/activate/.php is not what you want: your script has probably a name before the php extension.

laravel 5 send email with html elements and attachment using ajax

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.

Laravel mail cannot send from field

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??

Laravel Not sending Email

am having some sort of an issue with sending mails from my website with Laravel 4, I followed the on-line documentation but haven't successfully sent a mail with Laravel my code is as show below.
Mail::send('emails.contactmail', $data, function($message)
{
$name = Input::get('name');
$email = Input::get('email');
$message->from($email, $name);
$message->to('info#mysite.org', 'Info at My Site')
->subject('Website contact form');
});
I use following code for sending temporary password in case of forgot password
public function postForgotPassword(ForgotPasswordRequest $request){
$email=$request->email;
$objUser=DB::table('users')
->where('email','=',$email)
->select('email','id','first_name','last_name','user_group_id')
->first();
$string = str_random(15);
$pass=\Hash::make($string);
$objUser2=User::find($objUser->id);
$CURRENT_TIMESTAMP=new DateTime();
$objUser2->temporary_pass=$pass;
$objUser2->pass_status=1;
$objUser2->updated_at=$CURRENT_TIMESTAMP;
$objUser2->save();
$data=array("email"=>$email,"pass"=>$string,"first_name"=>$objUser->first_name,"last_name"=>$objUser->last_name);
$email=array("email"=>$email);
Mail::send('emails.forgot_password',$data, function($message) use($email) {
$message->to($email['email'],'MyProject')->subject('Password Recovery ');
});
return Redirect::to('auth/login')->with('flash_message','Check your email account for temporary password');
}
And I write email template in forgot_password.blade.php which is located in view.emails folder.

Categories