我发送电子邮件与Symfony 2.1和Swiftmailer后,我试图删除附件文件,但如果我在返回响应对象(重定向)之前删除该文件,电子邮件不会发送.
我想这是因为symfony在回复中发送电子邮件,因此当电子邮件发送时,附件已经被删除.
例如:
<?php
// DefaultCotroller.php
$message = \Swift_Message::newInstance($subject)
->setFrom('no-reply@dasi.es')
->setTo($emails_to)
->setBody($body, 'text/html')
->attach(\Swift_Attachment::fromPath('backup.rar'));
$this->get('mailer')->send();
unlink('backup.rar'); // This remove the file but doesn't send the email!
return $this->redirect($this->generateUrl('homepage'));
Run Code Online (Sandbox Code Playgroud)
一个选项是创建一个crontab来清理文件,但我不想使用它.
谢谢!
我做了一个简单的联系表,并使用swiftmailer(with symfony2)发送和发送电子邮件。
使用Gmail帐户时一切正常,但在生产环境中,它必须是网站域中的noreply地址。因此,我更改了参数以对应此电子邮件帐户。
看来它正在运行send方法,返回true,但未收到任何消息,我检查了一个垃圾邮件文件夹,但它也未到达那里。设置在雷鸟中工作时100%正确。
不知道在哪里寻找问题。有什么建议我应该检查什么?
我曾尝试使用相同的帐户通过PHPMailer发送电子邮件,但它确实起作用,因此看来问题仅在于swiftmailer。我将在项目的其他几个地方发送电子邮件,并且希望现在避免更改它,而改正它。
我正在使用symfony2命令作为cron作业向群发成员发送批量电子邮件.
实际代码:
$output->writeln('Before: '.(memory_get_usage() / 1024 / 1024));
$mailer->send($m);
$output->writeln('After: '.(memory_get_usage() / 1024 / 1024));
Run Code Online (Sandbox Code Playgroud)
我的结果:
Before: 182.38 MB
After: 183.73 MB
Run Code Online (Sandbox Code Playgroud)
每次我发送电子邮件时,swiftmailer都会消耗额外的1 + MB内存.这似乎不对,但每次发送新消息时内存都会增加.我在这里做错了吗?
我正在尝试使用Swiftmailer从Gmail发送电子邮件,并打印此错误:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "username@gmail.com" using 2 possible authenticators' in /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 Stack trace: #0 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() #2 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/websitename/public_html/cv/profile2.php(69): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184
Run Code Online (Sandbox Code Playgroud)
我的PHP代码:
require_once 'Swift-5.1.0/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('username@gmail.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('username@gmail.com' => 'Ahmad Shadeed'))
->setTo(array('receiver@gmail.com' => 'YOU'))
->setBody('This is the text of the mail send by Swift using …Run Code Online (Sandbox Code Playgroud) 我想将我的电子邮件代码从我的控制器移动到服务中.
到目前为止我做了以下事情:
在services.yml中创建了条目
在acme/demobundle/services/EmailManager.php中创建了一个EmailManager.php文件
可以使用一些帮助来了解需要进入EmailManager.php的内容以及如何在控制器中调用它?
services.yml
services:
email_manager:
class: Acme\DemoBundle\Services\EmailManager
arguments: [@request_stack, @mailer]
scope: request
Run Code Online (Sandbox Code Playgroud)
EmailManager.php
<?php
// src/Acme/DemoBundle/Services/EmailManager.php
namespace Acme\DemoBundle\Services;
class EmailManager
{
private $mailer;
private $request;
public function __construct(RequestStack $requestStack, $mailer)
{
$this->request = $requestStack->getCurrentRequest();
$this->mailer = $mailer;
}
What needs to go here? Do I just copy/paste the code from the contactAction below into here?
}
Run Code Online (Sandbox Code Playgroud)
带有contactAction的控制器代码,我想从控制器移出到EmailManager服务:
/**
* @Route("/", name="contact")
* @Template("AcmeDemoBundle:Default:index.html.twig")
*/
public function contactAction(Request $request)
{
$form = $this->createForm(new ContactType());
if ($request->isMethod('POST')) {
$form->submit($request); …Run Code Online (Sandbox Code Playgroud) 我已经成功将我的symfony网络应用程序配置为使用SMTP发送电子邮件。但是我所有发送的电子邮件都被放入spool目录中。
仅在发送中出现错误时才会发生这种情况。那是对的吗?
但是,如果我执行命令swiftmailer:spool:send --env=prod,则我的所有电子邮件都可以正确发送。
为什么我的服务器没有立即发送电子邮件?那是因为我修复了错误吗?有没有什么办法解决这一问题?
swiftmailer:
spool:
type: file
path: %kernel.root_dir%/spool
Run Code Online (Sandbox Code Playgroud) 使用Yii mailer该类时,是否可以在邮件布局中访问参数?我可以$model从视图访问,但不能从布局访问。
<?php
$params = array(
"model" => $model
);
$message = Yii::$app->mailer->compose([
'html' => $view.'Html',
'text' => $view,
], $params)
->setFrom("me@placeholder.com")
->setTo($recipient)
->setSubject($subject);
?>
Run Code Online (Sandbox Code Playgroud)
我知道对于标准的Web视图,您将设置yii\web\View::$params为访问布局中的变量,但这似乎不适用于邮件发件人。
有任何想法吗?
我正在尝试从CentOS服务器连接到mailtrap.io以发送电子邮件。发生以下错误:
“无法与主机邮件trap.io建立连接[权限被拒绝#13]”
有什么想法如何解决吗?
这就是我如何生成pdf页面
public function actionPdf(){
Yii::$app->response->format = 'pdf';
$this->layout = '//print';
return $this->render('myview', []);
}
Run Code Online (Sandbox Code Playgroud)
这就是我发送电子邮件的方式
$send = Yii::$app->mailer->compose('mytemplate',[])
->setFrom(Yii::$app->params['adminEmail'])
->setTo($email)
->setSubject($subject)
->send();
Run Code Online (Sandbox Code Playgroud)
如何将pdf生成为文件并将其附加到我的电子邮件中?
在我的Laravel设置中,邮件始终有效,直到我将框架升级到5.5(从5.4开始).现在它总是在以下情况下失败:
请注意以下事项:
mail()如果直接调用php的功能mail它仍然在这里sendmail(你可以vardump $命令,它说/usr/sbin/sendmail -bs) 这是.env邮件:
MAIL_DRIVER=mail
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Run Code Online (Sandbox Code Playgroud)
那么有谁知道如何解决这个问题?它只出演了laravel 5.5
非常感谢
swiftmailer ×10
php ×5
email ×4
symfony ×4
yii2 ×2
laravel ×1
laravel-5.5 ×1
pdf ×1
permissions ×1
php-7 ×1
response ×1
service ×1
ubuntu-14.04 ×1