简体   繁体   English

如何在我的 Typo3 分机内发送邮件

[英]How can i send Mails within my Typo3 extension

I get results from a jQuery Survey in JSON within my Typo3 Extension.我从我的 Typo3 扩展中的 JSON 中的 jQuery 调查中获得结果。 Now I want to send these results with the Typo3 Mail API to my Inbox.现在我想将这些结果与 Typo3 邮件 API 一起发送到我的收件箱。 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.您需要构建一个完全成熟的 TYPO3 扩展或直接使用symfony/mailer的独立应用程序。

Your script is missing $GLOBALS['TYPO3_CONF_VARS']['MAIL'] .您的脚本缺少$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...您无意中发现您正在按照 TYPO3 的约定和要求进行编程,但随后您惊讶于该框架的功能不起作用......

Create a middleware in your sitepackage, and register it correctly.在您的站点包中创建一个中间件,并正确注册它。 Check for a special route and let the rest run.检查特殊路由,让 rest 运行。 If handling, use the code there.如果处理,请使用那里的代码。

You need to run the mailing code in a (full) bootstrapped Typo3 context:您需要在(完整)自举 Typo3 上下文中运行邮件代码:

  • in a middleware在中间件中
  • extbase plugin registered with pageNum directly直接用 pageNum 注册的 extbase 插件

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.只是将文件放在扩展文件夹中并不意味着它是正确的 typo3 扩展名。

Maybe you want to start and have a proper extension setup first, with the corresponding readings:也许您想先开始并进行适当的扩展设置,并使用相应的读数:

  • ext_emconf.php / composer.json as a minimum至少 ext_emconf.php / composer.json
  • code files in 'Classes/' subfolders “Classes/”子文件夹中的代码文件

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.然后决定你想用哪种方式处理“ajax”调用并发送 email,有几种可能性(如上所述)......而不是以正确和正确的方式阅读和实施该解决方案。

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).直接引用/调用一个 php 文件并引导 composer 自动加载器不会也永远不会引导 Typo3 和所有需要的配置引导(从扩展、本地/附加配置、站点配置和和和收集配置)。

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..因此在您的扩展 composer.json 中注册为 psr-4 自动加载..

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.开始实施 peaces 以在该文件中模拟 Typo3 bootstpraping 并不好,而且会让人很头疼……拥有适当的站点包/扩展结构会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM