我想在我的Symfony2应用程序中发送电子邮件时命名发件人
Administrator <no-reply@app.com>
Run Code Online (Sandbox Code Playgroud)
我尝试过parameters.ini:
mailer_from = {"no-reply@app.com : Administrator"}
Run Code Online (Sandbox Code Playgroud)
似乎可以这样做,因为在SimpleMessage.php中我们有:
/**
* Set the sender of this message.
* This does not override the From field, but it has a higher significance.
* @param string $sender
* @param string $name optional
* @return Swift_Mime_SimpleMessage
*/
public function setSender($address, $name = null)
{
if (!is_array($address) && isset($name))
{
$address = array($address => $name);
}
if (!$this->_setHeaderFieldModel('Sender', (array) $address))
{
$this->getHeaders()->addMailboxHeader('Sender', (array) $address);
}
return $this;
}
Run Code Online (Sandbox Code Playgroud)
我在parameters.ini失败中尝试过的所有事情.你知道我做错了吗?
默认情况下,我使用假脱机邮件解决方案在我的网页中发送简报.但我还需要立即发送电子邮件.所以我使用了这个解决方案
如果我用Spool发送简报,一切都很好.但是当我使用时
$mailer = $this->get('instant_mailer');
Run Code Online (Sandbox Code Playgroud)
我在开头收到一些带有一些文字的电子邮件:
HTTP/1.0 200 OK Cache-Control:no-cache Content-Type:text/html; charset = UTF-8日期:星期五,2012年9月7日16:19:06 GMT
如何删除?
我正在使用SYmfony 1.4和swift邮件程序通过Sendgrid发送大量电子邮件.
我在某些电子邮件地址上收到RFC合规性错误.
一种解决方案是删除抛出错误的条件,它确实有效,但它涉及更改核心.你如何在站点文件而不是symfony核心中扩展MailboxHeader.php.像这样的东西,但不是因为它不起作用:
class overrideRFCError extends Swift_Mime_Headers_AbstractHeader
{
private function _assertValidAddress($address)
{
if (!preg_match('/^' . $this->getGrammar('addr-spec') . '$/D',
$address))
{
// throw new Swift_RfcComplianceException(
// 'Address in mailbox given [' . $address .
// '] does not comply with RFC 2822, 3.6.2.'
// );
}
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎有点沉重.有没有办法验证电子邮件的RFC合规性.如果是这样,那么我可以从阵列中取消它.
更新07/17/13
我能够对每个地址进行彻底的清理,使其符合RFC标准,从而解决了这个问题.但是,我想知道SwiftMailer中是否存在执行此检查的函数,而不是编写自定义函数.
更新07/18/13
这就是我的工作.我试图尽可能地清理条目.
在名为的变量中加载一组地址 $emailList
在行动中:
$cleanList = sendGridEmailer::validateEmails($emailList);
Run Code Online (Sandbox Code Playgroud)
在sendGridEmailer类中:
// loop and validate each email address
public static function validateEmails($emailList) {
foreach($emailList as $address => $name) { …Run Code Online (Sandbox Code Playgroud) 我想在我的邮件模板中使用图像 - 它们应该与邮件一起发送.如何将它们嵌入树枝模板(特殊路径或设置以解决它们?)?我是将它们作为附件附加还是有一些特殊设置?
从html&css发送电子邮件的最佳做法是什么?我的项目中有很多邮件,需要解决方案,这可以让我不再一次又一次地写下所有代码:
$msg = \Swift_Message::newInstance()
->setSubject('Test')
->setFrom('noreply@example.com')
->setTo('user@example.com')
->setBody($this->renderView('MyBundle:Default:email1.text.twig'));
$this->get('mailer')->send($msg);
Run Code Online (Sandbox Code Playgroud) 默认情况下,在发送电子邮件时,下面的代码工作正常但是我想在发送电子邮件之前覆盖SwiftMailer传输选项(主机,用户和密码),选项将来自数据库.
谁知道如何实现这一目标?
注意:我通过这篇文章,但是听众没有做任何事情,OP的解决方案有一个$transport->getExtensionHandlers();方法,当我转储它时,它没有出现在我的$ transport对象中.
作曲家入门
"symfony/swiftmailer-bundle": "2.1.*"
Run Code Online (Sandbox Code Playgroud)
/booking/app/config/parameters.yml
parameters:
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: vizzzzz
mailer_password: 123123
Run Code Online (Sandbox Code Playgroud)
/booking/app/config/config.yml
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
encryption: tls
Run Code Online (Sandbox Code Playgroud)
调节器
$hostDb = $dbObject->getHost();
$userDb = $dbObject->getUser();
$passwordDb = $dbObject->getPassword();
// then I should override setting here so that the data coming from parameters.yml is overriden
$order = // database object
$message = \Swift_Message::newInstance()
->setSubject($order->getSubject())
->setFrom($order->getFrom())
->setTo($order->getTo())
->setBody($content)
;
$this->mailer->send($message);
Run Code Online (Sandbox Code Playgroud) 当我使用telnet发送电子邮件时,smtp服务器会在发送电子邮件250 2.0.0 Ok: queued as 7A821440123E时回复我.所以我需要获取ID 7A821440123E来跟踪邮件日志中的电子邮件.使用Swiftmailer获取此功能是否可行?
在我的测试中,我调用了一些发送电子邮件的命令.我可以使用以下命令显示发送的电子邮件数量:
$output->writeln(
$spool->flushQueue(
$this->getContainer()->get('swiftmailer.transport.real')
)
);
Run Code Online (Sandbox Code Playgroud)
Symfony2文档解释了如何在Web测试期间使用探查器获取电子邮件内容(此处也在Stack Overflow中进行了解释),但我不知道在没有Web请求时如何执行相同的操作.
我使用了这些链接中提供的代码:
<?php
namespace ACME\MyBundle\Tests\Command;
use Liip\FunctionalTestBundle\Test\WebTestCase;
class EmailTest extends WebTestCase
{
public function tesEmailCommand()
{
// load data fixtures
// http://symfony.com/doc/current/cookbook/email/testing.html
$client = static::createClient();
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$client->enableProfiler();
/** @var \Symfony\Bundle\FrameworkBundle\Console\Application $application */
// inherit from the parent class
$application = clone $this->application;
$application->add(new EmailCommand());
$command = $application->find('acme:emails');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => 'acme:emails' …Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 4.2,它使用swiftmailer lib来发送电子邮件.我正在尝试使用Sendgrid SMTP服务,但我在登台服务器上收到超时错误.
在我的本地开发中一切正常.
mail.php
return array(
'driver' => 'smtp',
'host' => 'smtp.sendgrid.net',
'port' => 587,
'encryption' => 'tls',
'username' => 'username'
'password' => ...
Run Code Online (Sandbox Code Playgroud)
我已切换到端口465和'ssl',在本地工作,但在服务器上没有运气.服务器上启用了OpenSSL.
有趣的是Mandrill smtp(它使用不同的laravel驱动程序),本地服务器smtp在服务器上正常工作.
港口是开放的.我可以从服务器telnet到sendgrid.我还将IPv4的主机名切换为仅用于检查,因为人们使用smtp.gmail.com IPv4与IPv6的所有麻烦.没有什么区别.
具体的错误位置是这样的:
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->_stream) {
throw new Swift_TransportException(
'Connection could not be established with host '.$this->_params['host'].
' ['.$errstr.' #'.$errno.']');
Run Code Online (Sandbox Code Playgroud)
所以我的问题是我应该检查一下为什么swiftmailer和stream_socket_client php函数在本地工作但不在服务器上工作.本地我在mac上,服务器正在运行centOS.
此代码之前工作正常,laravel自上周以来突然停止发送邮件.我尝试使用swiftmailer分别使用相同的凭据发送邮件,这也正常.我.env在laravel-中有关于邮件功能的文件的以下凭证 -
MAIL_DRIVER=mandrill
MAIL_HOST=smtp.mandrillapp.com
MAIL_PORT=465
MAIL_USERNAME=*******
MAIL_PASSWORD=*******
MANDRILL_SECRET=*********
MAIL_ENCRYPTION=ssl
Run Code Online (Sandbox Code Playgroud)
我配置/config/services.php和/config/mail.php文件到.
没有错误
Mail::failures().可能是造成这种问题的可能原因.
我可以使用swiftmailer发送带有模板的邮件,但是有必要找出问题来解决这些问题,以后再也没有时间.
swiftmailer ×10
symfony ×6
php ×5
email ×3
html-email ×1
laravel-5 ×1
mandrill ×1
rfc ×1
sendgrid ×1
spool ×1
symfony-1.4 ×1
testing ×1
twig ×1