今天我试图将Symfony应用程序连接到作为Microsoft Exchange服务器运行的本地邮件服务器。
首先,在切换到Symfony之前,我的web项目是用Asp开发的。. NET,由于一些个人原因,我退回了Symfony下的项目。
在c#中,这段代码可以正常工作:
public static void MailSender(string server, string fromDisplayName, string fromMailAddress, List<List<KeyValuePair<string,string>>> tos, string subject, string body, List<string> attachments = null, List<List<KeyValuePair<string,string>>> ccs = null, List<List<KeyValuePair<string,string>>> bccs = null)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(fromMailAddress, fromDisplayName);
foreach(List<KeyValuePair<string, string>> to in tos){
mailMessage.To.Add(new MailAddress(to[1].Value.ToString(), to[0].Value.ToString()));
}
if (ccs != null){
foreach(List<KeyValuePair<string, string>> cc in ccs){
mailMessage.CC.Add(new MailAddress(cc[1].Value.ToString(), cc[0].Value.ToString()));
}
}
if (bccs != null){
foreach(List<KeyValuePair<string, string>> bcc in bccs){
mailMessage.CC.Add(new MailAddress(bcc[1].Value.ToString(), bcc[0].Value.ToString()));
}
}
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
if (attachments != null)
{
foreach (string attachment in attachments)
mailMessage.Attachments.Add(new Attachment(attachment));
}
using (SmtpClient smtpClient = new SmtpClient(server))
{
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(mailMessage);
}
}
}
服务器url格式为:myserver.domain.tld
。它还使用了Windows集成身份验证。
如何配置PHP或Symfony(或两者)与本地Exchange邮件服务器一起工作?
我尝试在MAILER_DSN
变量中使用这种类型的url:smtp://user:pass@myserver.domain.tld
(用户作为电子邮件地址,用'%40'代替'@'字符。
谢谢。
很抱歉这个问题,因为这是我的错误,我把MAILER_DSN=smtp://server.tld:25
另外,我在config/packages/messenger.yaml
中注释了一行SymfonyComponentMailerMessengerSendEmailMessage: async
第二个选项解决了我的问题。
如果你有其他选择,我知道!
呢?。