我正在开发一个Swift_Mailer用于发送电子邮件的 Symfony 4 应用程序。由于在我的情况下不可能使用 SMTP(不要问为什么......)我必须使用类似sendmail.
默认情况下,Swift Mailer 的配置在 Symfony 的.env文件中以 URL 表示法在名为MAILER_URL. 默认值为“ null://localhost”,它根本不发送邮件。
我所能找到的值必须是 Gmail 或 SMTP 的示例,如Symfony 文档以及 Composer 生成的示例 .env 中所述。
.env 的默认内容:
# …
###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###
# …
Run Code Online (Sandbox Code Playgroud)
我不知道什么,因此我的问题是:
我需要做什么才能sendmail使用它?
我已经尝试过类似的东西:
MAILER_URL=sendmail://localhost
Run Code Online (Sandbox Code Playgroud)
和
MAILER_URL=sendmail://my-domain.com …Run Code Online (Sandbox Code Playgroud) 我真的不知道这是关于SwiftMailer还是mailtrap.io,但问题是当我尝试建立到所述服务(mailtrap.io)的 SMTP 连接时,即使我不使用任何用户名或密码,我也从未收到任何错误。不使用任何用户名或密码时,我不应该收到任何身份验证错误吗?或者我这样做的方式不对?
这是我在将其存储到文件之前测试是否可以建立动态连接的方法.env。
/**
* E-mail configuration
*
* @param Request $request
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
public function postEmailConfiguration(Request $request)
{
# Try connecting to the SMTP
try {
$encryption = $request->input('encryption');
if ($encryption == 'null') {
$encryption = NULL;
}
$transport = new Swift_SmtpTransport($request->input('host'), $request->input('port'), $encryption);
$transport->setUsername($request->input('username'));
$transport->setPassword($request->input('password'));
$mailer = new Swift_Mailer($transport);
$mailer->getTransport()->start();
# Update .env file
$this->setEnvironmentValues([
'MAIL_HOST' => $request->input('host'),
'MAIL_PORT' => $request->input('port'),
'MAIL_USERNAME' => $request->input('username'),
'MAIL_PASSWORD' => $request->input('password'), …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 symfony 中的 swiftmailer 和 office365 服务器在注册后发送确认电子邮件。我已经尝试了我遇到的所有主机、端口和加密类型的组合。
目前我的 .env 文件包含这一行:
MAILER_URL=smtp://smtp.office365.com:587?encryption=ssl&auth_mode=login&username="myusername@mycompany.com"&password="mypassword"
Run Code Online (Sandbox Code Playgroud)
*注意:我使用“”作为我的用户名和密码,因为它们包含特殊字符,我在某处读到这可能会导致 MAILER_URL 出现问题。
我的 swiftmailer.yaml 文件包含以下内容:
swiftmailer:
url: '%env(MAILER_URL)%'
stream-options:
ssl:
allow_self_signed : true
verify_peer: false
Run Code Online (Sandbox Code Playgroud)
最后,我在控制器中使用此代码发送电子邮件:
$message = (new \Swift_Message('Referral tool registration'))
->setFrom('myusername@mycompany.com')
->setTo('test@gmail.com')
->setBody(
$this->renderView(
'email/notification/user_registered.html.twig',
['firstName' => $user->getFirstName(),
'lastName' => $user->getLastName()
]
),
'text/html'
);
$mailer->send($message);
Run Code Online (Sandbox Code Playgroud)
通过当前选择的主机、端口和加密,我得到: "Connection could not be established with host smtp.office365.com [ #0]"
更新:当我输入 telnet smtp.office365.com 587 时,我得到了一个有效的响应,所以我认为问题与网络无关,端口没有被阻止。
我正在使用Symfony 1.4邮件程序,我在其中构建了电子邮件所需的各种位,然后使用以下命令将其发送出去:
$this->getMailer()->composeAndSend($sender, $recipient, $subject, $body);
Run Code Online (Sandbox Code Playgroud)
在电子邮件正文中,我需要能够利用操作中生成的变量,所以现在我可能在我的操作中有这个:
$body = 'Your username is '.$username.' and this is the email body.';
Run Code Online (Sandbox Code Playgroud)
有没有人知道存储/组织各种电子邮件正文的优雅方式,而不是像这样直接编码我的行为?我将有许多电子邮件模板,并且还将使用多种语言.
我发现了一个旧的Askeet教程讨论这个,但它似乎有点过时与SwiftMailer的新symfony 1.4集成,而SwiftMailer文档本身对此并不十分清楚.
谢谢.
我直接用swift知道你可以创建一个Recipient_List并执行 - > addBcc().我如何在symfony 1.4的上下文中做同样的事情
$message = $this->getMailer()->compose();
$message->setSubject(sfConfig::get('app_email_welcome_subject'));
$message->setTo($user->getUsername());
$message->setFrom(sfConfig::get('app_email_from'));
Run Code Online (Sandbox Code Playgroud) 我已经在我的Symfony应用程序中创建了一个新命令.
在此命令中,我调用一个服务,该服务从FOS包发送邮件 作为邮件程序.
我的问题是电子邮件正文显示在控制台中以发送电子邮件.
我想说的是,该msg mailer方法工作正常进行的命令.
这是我的命令:
class ReminderCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->container = $this->getApplication()->getKernel()->getContainer();
# the mailer service works fine out the command
$mailer = $this->container->get('mailer');
$mailer->sendMsg(...);
$text = ' => mail sent';
$output->writeln($text);
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙
谢谢
山姆
我不能用Swift Mailer发送multipart/alternative(我没有找到任何引用,所以也许我不能使用这个函数),这是我的代码:
$file[1]=html_entity_decode($file[1]);
//Prepare plain/html body
foreach($rep as $find => $sost)
$file[1]=str_replace($find,$sost,$file[1]);
//Prepare plain/text body
$plain=strip_tags(str_replace(' ',' ',str_replace('<br/>',"\n",$file[1])));
$boundary=uniqid('n_=_p');
//Prepare mail body
$body = "--".$boundary."\r\n";
$body .= "Content-type: text/plain;\r\ncharset=utf-8\r\nContent-Transfer-Encoding: 7bit\r\n";
$body .= $plain;
$body .= "\r\n--".$boundary."\r\n";
$body .= "Content-type: text/html;\r\ncharset=utf-8\r\nContent-Transfer-Encoding: 7bit\r\n";
$body .= "<html><body>".$file[1]."</body></html>";
$body .= "r\n--".$boundary ."--";
//Send Mail
$message = Swift_Message::newInstance();
$message->setFrom(array($stmp[2]=>$stmp[1]));
$message->setReplyTo(array($stmp[2]=>$stmp[1]));
$message->setSubject($file[0]);
$message->setContentType("multipart/alternative");
$message->setBody($body);
$message->setTo($mail);
$message->setBoundary($boundary);
if($stmp[0]==0)
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -t');
else if($stmp[0]==1){
if($stmp[5]==0)
$transport = Swift_SmtpTransport::newInstance($stmp[2],$stmp[4]);
else
$transport = Swift_SmtpTransport::newInstance($stmp[2],$stmp[4],'ssl');
if($stmp[6]==1){
$transport->setUsername($stmp[7]);
$transport->setPassword($stmp[8]);
}
}
$mailer …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PHP Swiftmail(Laravel Package),但它一直在抛出这个愚蠢的异常:
致命错误:未捕获异常'Swift_TransportException',消息'预期响应代码220但得到代码"",消息"+ OK Microsoft Exchange POP3服务已准备就绪.[QwBPADEAUABSADAANwBDAEEAMAAxADMALgBuAGEAbQBwAHIAZAAwADcALgBwAHIAbwBkACAAbwB1AHQAbABvAG8AawAuAGMAbwBtAA ==]"
任何人来到这里并知道如何解决它?
问候
我想通过我的Gmail帐户发送电子邮件.
我的邮件配置:
[
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'my@gmail.com',
'password' => 'pass',
'port' => '587',
'encryption' => 'tls',
],
]
Run Code Online (Sandbox Code Playgroud)
我写了命令MailController:
<?php
namespace app\commands;
use yii\console\Controller;
use Yii;
/**
* Sanding mail
* Class MailController
* @package app\commands
*/
class MailController extends Controller
{
private $from = 'my@gmail.com';
private $to = 'to@gmail.com';
public function actionIndex($type = 'test', …Run Code Online (Sandbox Code Playgroud) 命令功能
$message = \Swift_Message::newInstance('test')
->setContentType("text/html")
->setFrom('x@x.com')
->setTo('x@gmail.com');
$message->setBody('test');
if ($this->getApplication()->getKernel()->getContainer()->get('mailer')->send($message))
{
return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)
当我在命令行执行命令时,我发送邮件是真的.
Paramters.yml
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: x@gmail.com
mailer_password: xxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
Config.yml
swiftmailer:
spool:
type: file
path: "%kernel.root_dir%/../swiftmailer"
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
Run Code Online (Sandbox Code Playgroud)
我曾在某处读过可以使用Symfony Profiler查看邮件是否已发送但我只是想在测试目的中将邮件物理地发送到我的地址.我只需要知道我做错事的地方.
我必须注意到我在gmail帐户中使用了允许的设备安全工具.
这可能是邮件未发送的原因吗?
swiftmailer ×10
email ×5
php ×5
symfony ×3
office365 ×2
symfony4 ×2
command-line ×1
laravel ×1
sendmail ×1
symfony-1.4 ×1
symfony1 ×1
yii2 ×1