Send link with variable using Zend_Mail - php

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.

Related

How to add Account name to a contact in Dynamics 365

I am using Alexa-php-toolkit for Dynamics 365 https://github.com/AlexaCRM/php-crm-toolkit, using this I can successfully create new contact but I can't add an account name with the contact, when I try I get this error:
Notice: Property accountid of the contact entity cannot be set in ../vendor/alexacrm/php-crm-toolkit/src/Entity.php on line 263.
Here is my script.
<?php
//URL: https://github.com/AlexaCRM/php-crm-toolkit
/**
* Use init.php if you didn't install the package via Composer
*/
use AlexaCRM\CRMToolkit\Client as OrganizationService;
use AlexaCRM\CRMToolkit\Settings;
require_once '../vendor/autoload.php';
require_once '../vendor/alexacrm/php-crm-toolkit/init.php';
require_once 'config.php';
require_once 'includes/db.php';
$db = new DB();
$options = getAuth();
$serviceSettings = new Settings( $options );
$service = new OrganizationService( $serviceSettings );
$accountId = 'a2536507-018d-e711-8115-c4346bac0a5f';
// create a new contact
$contact = $service->entity( 'contact' );
$contact->accountid = $accountId;
$contact->firstname = 'Test';
$contact->lastname = 'Contact12';
$contact->jobtitle = 'Business Analyst';
$contact->mobilephone = '1002345679';
$contact->fax = '9902345679';
$contact->emailaddress1 = 'john.doe1#example.com';
$contact->address1_line1 = '119 Cambridge';
$contact->address1_line2 = 'Apt 22';
$contact->address1_city = 'Houston';
$contact->address1_stateorprovince = 'TX';
$contact->address1_postalcode = '77009';
$contact->address1_country = 'US';
$contactId = $contact->create();
echo $contactId;
?>
There is this line of your code in a question:
$contact->accountid = $accountId;
First, the parent account on a contact is saved in the parentcustomerid field that is a special lookup field that can store link to both account or contact entity.
The fields accountid and parentcontactid help to handle this in background, but are not generaly available. You need to work with parentcustomerid field.
Second, another problem when working with lookups (foreign keys) is you need to pass entity type (table name).
The correct code might look like this:
$accountRef = $client->entity( 'account' );
$accountRef->ID = $accountId;
$contact->parentcustomerid = $accountRef;
or
$contact->parentcustomerid = new EntityReference( 'account', $accountId );
Those examples are taken from the issue list, adjusted, but not tested. I hope it is working example, not functionality request.

FosUserBundle: password reset request link is sent instead of resetting link

I am trying to send email with password reset link. This is my method:
public function sendPasswordResettingEmail(User $user){
$url = $this->router->generate('fos_user_resetting_reset',
array('token' => $user->getConfirmationToken()), UrlGenerator::ABSOLUTE_URL);
$html = $this->templating->render('CoreBundle:Email:password_reset.email.twig', array(
'user' => $user,
'confirmationUrl' => $url
));
$message = \Swift_Message::newInstance()
->setContentType('text/html')
->setSubject('Password reset')
->setFrom(array(self::EMAIL_FROM => 'App'))
->setTo($user->getEmail())
->setBody($html);
$this->service->get('mailer')->send($message);
}
But I get email with
http://127.0.0.1:8000/resetting/request
link, which takes me to a form asking for email or username. I need to submit my email address again to get resetting
http://127.0.0.1:8000/resetting/reset
link.
Is it possible to get reset link and skip request step? because I am already providing user parameter to method.

how to edit email body before sending using Laravel

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);

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.

yii2 how can I access post value

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'].

Categories