Cakephp email official example not working? - php

I am new to Cakephp (and I have asked a few question on Cakephp here recently) and I am learning by trying out the example on the official site. The following is the link.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
I added the to my Posts controller which I created before following the official example code.
public function emailTesting(){
$Email = new CakeEmail();
$Email->config('default');
$Email->from(array('me#example.com' => 'My Site'));
$Email->to('myWorkingEmail#gmail.com');
$Email->subject('Testing Subject');
$Email->send('Testing email content');
}
According to the configuration section,
It is not required to create app/Config/email.php, CakeEmail can be used without it and use respective methods to set all configurations separately or load an array of configs.
So, I think the above code should work even if I don't create app/Config/email.php. Then,when I try to open the page
http://localhost/cakephpTesting/emailTesting/
It gives a blank page with no error message (the debug is already 2, which should shows all kind of error message if there is any error) but I didn't receive any email. I also tried different email service provider like Yahoo and my school email. All of them fails, so I think it should be the problem of my code?
So, anybody got any idea on what have I missed?

As mentioned in your question, it is not required to create a app/Config/email.php, IF you use respective methods to set all configurations separately or load an array of configs.
You are using Email::config() method to load a $default config array which is supposed to be defined somewhere. As good practise, it can be defined in the app/Config/email.php or define it in your controller's action (don't recommend the latter). CakeEmail needs to know the transport type, host, port, etc.
Read the section after the line that you have mentioned in your question which tells you how to either use the constructor of CakeEmail or use the $config array.

Related

How can i send Mails within my Typo3 extension

I get results from a jQuery Survey in JSON within my Typo3 Extension. Now I want to send these results with the Typo3 Mail API to my Inbox. According to the documentations following shoud do the work
$mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
$mail->from(new \Symfony\Component\Mime\Address('john.doe#example.org', 'John Doe'));
$mail->to(
new \Symfony\Component\Mime\Address('receiver#example.com', 'Max Mustermann'),
new \Symfony\Component\Mime\Address('other#example.net')
);
$mail->subject('Your subject');
$mail->text('Here is the message itself');
$mail->html('<p>Here is the message itself</p>');
$mail->attachFromPath('/path/to/my-document.pdf');
$mail->send();
so my code looks like this:
namespace TYPO3\CMS\Core\Mail;
if (!file_exists( dirname(__DIR__, 2).'/vendor/autoload.php' )) {
throw new \RuntimeException(
'Could not find vendor/autoload.php, make sure you ran composer.'
);
} else {
require dirname(__DIR__, 2).'/vendor/autoload.php';
}
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MailUtility;
$mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
$mail->from(new \Symfony\Component\Mime\Address('john.doe#example.org', 'John Doe'));
$mail->to(
new \Symfony\Component\Mime\Address('receiver#example.com', 'Max Mustermann'),
new \Symfony\Component\Mime\Address('other#example.net')
);
$mail->subject('Your subject');
$mail->text('Here is the message itself');
$mail->html('<p>Here is the message itself</p>');
$mail->attachFromPath('/path/to/my-document.pdf');
$mail->send();
I just getting errors. Does anyone know how i can achieve this?
Following error i get
Notice: Undefined index: TYPO3_CONF_VARS in /var/www/html/local_packages/ext_sitepackage/public/typo3/sysext/core/Classes/Mail/Mailer.php on line 170
Apparently this is the same question as you already posted here.
This will never work like this. You need to either build a fully fledged TYPO3 extension or a standalone application using symfony/mailer directly.
Your script is missing $GLOBALS['TYPO3_CONF_VARS']['MAIL'].
You stumble over the fact that you're programming past the conventions and requirements of TYPO3, but then you're surprised that the framework's functions don't work...
Create a middleware in your sitepackage, and register it correctly. Check for a special route and let the rest run. If handling, use the code there.
You need to run the mailing code in a (full) bootstrapped Typo3 context:
in a middleware
extbase plugin registered with pageNum directly
Your pasted code looks weired in a lot of ways. Just putting a file in a extension folder do not mean that is correctly a typo3 extension.
Maybe you want to start and have a proper extension setup first, with the corresponding readings:
ext_emconf.php / composer.json as a minimum
code files in 'Classes/' subfolders
then decide with which way you want to handle the "ajax" call and send the email, there are several possibilities (as already mentioned above) .. than read and implement that solution the proper and correct way.
Just having a php file directly referenced/called and just bootstpping the composer autoloader do not and never will be bootstrapping Typo3 with all needed configuration bootstrapping (gathering confings from extensions, Local/AdditionalConfiguration, siteconfig and and and).
And generally a package/extension should have a namespace something like following (as a convention):
<extensionname.....
an thus registered as psr-4 autoloading in your extension composer.json ..
Starting to implement the peaces to simulate a Typo3 bootstpraping in that file is not good and will make a lot of headache ... it's easier to have a proper sitepacke/extension structure.

How to make custom email verification (Laravel 5.8)? [duplicate]

This question already has answers here:
How to customize the email verification email from Laravel 5.7?
(9 answers)
Closed 2 years ago.
I can make email verification using default template from Laravel 5.8.
Now, I want to customize the email.
Can you help me?
I tried to follow instructions from this: https://medium.com/#lordgape/custom-verification-email-notification-laravel-5-8-573ba3183ead. There is no error, but no email sent.
When I switch back to default verification, the default email is sent.
Edit:
The steps I tried so far:
Create a mailable for email verification.
Create views for new email template
override toMailUsing() using the following code in AppServiceProvider.php:
VerifyEmail::toMailUsing(function ($notifiable){
$verifyUrl = URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
['id' => $notifiable->getKey()]
);
// dd($notifiable);
return new EmailVerification($verifyUrl, $notifiable);
});
edit mailable, add two variable: $verifyUrl and $user.
edit __construct function:
public function __construct($url, User $user)
{
$this->user = $user;
$this->verifyUrl = $url;
}
edit build() function in mailable, add return $this->view('emails.verifyUser'); (views of custom template).
No error, "please verify your email" page is shown like usual. But no email sent.
If you want to only customize the look and the layout of the sent email and not the contents (text) of the mail you have to publish the view files of the notification and mail components.
To do so you can type:
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
These command will copy the Laravel mail message templates from the vendor folder to resources/views/vendor/notifications and resources/views/vendor/mail.
In the former path you have the email.blade.php which is basically the the MailMessage layout. (In fact you can see all the available slots you can customize with MailMessage).
In the latter path you can find the markdown and html components (that are also used in the MailMessage layout I mentioned earlier). You can take a look at the various files and modify them.
Note: the changes you makes to these templates would apply to any email message that you send, also if you create a new notification and mailable, it would still use these modified templates.
You can use this method to do global changes to the look of an email and, for example customize the header to include the logo of your company, etc.
If you just have to edit the content (texts) of an email message, you should still have to create your custom notification/mailable.
Quick tip: you can preview emails in the browser to make adjustments to the layout and texts quickly, as you don't have to fire off the notification all the time: https://medium.com/#jaouad_45834/preview-your-emails-notifications-in-browser-laravel-9058d8c856c4

Using Respect/Validation in another language

I want to use the Respect/Validation library in PHP. I know how to use it but currently I'm using it in a project in German language and of course, I also want the error messages in German.
For language translation, there is a section in the documentation but I don't really get it and I did not found any answer yet.
They're talking about a translator that should handle the translation of the messages. As a second parameter they're giving "gettext" but I don't know what this should be and how this should handle the translation.
Can anybody explain me how this works?
Respect/Validation won't do the translation for you, you should use a different project, library or function to do that. It won't leave you empty handed though, as the documentation states.
First, you should try to understand how translation libraries work (such as gettext()) and then read PHP documentation on Callables. Then it is a matter of choosing a library, creating the translations and calling setParam('translator', 'callable') method on the exception instance.
A quick introduction to your problem:
Translations are done based on a source: it can be a file, a database or something else, depending on which library you use.
Respect/Validation exception messages use the same pattern: {{name}} is invalid.. Where {{name}} will be replaced by the input given or the name if setName() was called on that rule.
You can see all messages you need to translate under the Respect\Validation\Exceptions namespace.
Usually, every library provide a single function/method to translate a given string. This is the method/function you want to set on the $exception->setParam() call.
If you ever translate all exception messages, we would love to make them available to everyone else.
PS: You could also make this question on the repository page, more people would help and we could also improve the way translations are handled by the library in the future.
I just changed all Exception defaultTemplates property, it works for me.
class Localization
{
public function init()
{
$this->validation();
}
public function validation()
{
$prefix = '\\Respect\\Validation\\Exceptions\\';
$localize = [
'EmailException' => 'local message',
'NotEmptyException' => 'local message'
];
foreach($localize as $class => $message) {
($prefix.$class)::$defaultTemplates[
ValidationException::MODE_DEFAULT][ValidationException::STANDARD] = $message;
}
}
}
$localization = new Localization();
$localization->init();

using sfMailer and Dispatcher inside a task

I created a task in symfony 1.4, and I want to create a mailer based on the app.yml config.
this is my PHP code :
$config = sfConfig::get('app_mailers_mailer_a');
$this->_mymailer = new sfMailer($this->dispatcher, $config['param']);
when I execute the task, and vardump the $config variable, I get my config information (host, port, etc..).
The problem is in the second line, It stops their, without any error messages, so I think that the way I use the dispatcher (or $config['param']) is incorrect.
Thanks
I found the solution, the problem was not $config or $this->dispatcher, but the problem was that I forgot to initialize the sfMailer, so I added those lines before;
Swift::registerAutoload();
sfMailer::initialize();
Cheers ;)

CodeIgniter: Can't Get My New Controller/View To Show

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

Categories