[英]Can I send email with mailgun sandbox domain under my local OS?
I registered myself at mailgun with domain I use for my laravel apps我在 mailgun 注册了自己的域名,用于我的 Laravel 应用程序
Also I see that with my domain there is sandbox domain, which looks like :我也看到我的域有沙盒域,它看起来像:
sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
and under SMTP block I see text: Grab your SMTP credentials:在 SMTP 块下,我看到文本:Grab your SMTP credentials:
SMTP hostname: smtp.mailgun.org
Port: 587 (recommended)
Username: postmaster@sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
Default password: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX
Working on my local OS, apache 2 under ubuntu 18 I try to send email in my local having in .env :在我的本地操作系统上工作,ubuntu 18 下的 apache 2 我尝试在我的本地发送电子邮件,其中包含 .env :
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
MAIL_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='from_email@site.com'
MAIL_FROM_NAME='FROM NAME'
MAILGUN_SECRET='XXXXXX-XXXXXXX'
MAILGUN_DOMAIN='sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org'
As result I sent email without errors, But I did not receive emails for 2 my users which are in Authorized Recipients of mailgun.结果我发送了没有错误的电子邮件,但是我没有收到 2 个属于 mailgun 授权收件人的用户的电子邮件。
What for this sandbox domain, can I use it for local OS and can I send emails under my local OS ?这个沙盒域有什么用,我可以将它用于本地操作系统吗?我可以在本地操作系统下发送电子邮件吗?
UPDATED : In my local .env I set mailtrap.io params and it works ok.更新:在我的本地 .env 中,我设置了 mailtrap.io 参数并且它工作正常。
In Account Security->API security->API keys of my mailgun account I have parameters L在 Account Security->API security-> 我的 mailgun 帐户的 API 密钥中,我有参数 L
Private API key
Public validation key
HTTP webhook signing key
Setting app on remote dev server在远程开发服务器上设置应用程序
MAIL_MAILER=smtp
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=XXX
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='from_email@site.com'
MAIL_FROM_NAME='FROM NAME'
If MAIL_USERNAME and MAIL_PASSWORD must be filled with values from Account Security->API security->API keys also if MAIL_FROM_ADDRESS must one of Authorized Recipients I created in the settings before ?如果 MAIL_USERNAME 和 MAIL_PASSWORD 必须填充来自帐户安全-> API 安全-> API 密钥的值,如果 MAIL_FROM_ADDRESS 必须是我之前在设置中创建的授权收件人之一?
UPDATED BLOCK 2: I try to fill parameters from live domain, but failed.更新块 2:我尝试从实时域中填充参数,但失败了。 I search paramters here : https://prnt.sc/18ouojv ?
我在这里搜索参数: https : //prnt.sc/18ouojv ?
Thanks in advance!提前致谢!
As Sandbox domains are restricted to authorized recipients only.So make sure to verify recipients emails in由于沙盒域仅限于授权收件人。因此请确保验证收件人电子邮件
https://app.mailgun.com/app/sending/domains https://app.mailgun.com/app/sending/domains
If recipients has not received email to inbox then check in spam folder.
如果收件人没有收到收件箱的电子邮件,请检查垃圾邮件文件夹。
Ref: https://help.mailgun.com/hc/en-us/articles/360011702394-Why-Do-My-Emails-Go-to-Spam-参考: https : //help.mailgun.com/hc/en-us/articles/360011702394-Why-Do-My-Emails-Go-to-Spam-
If still not working then change mailer to smtp如果仍然无法正常工作,则将邮件程序更改为 smtp
MAIL_MAILER=smtp
Then run然后运行
php artisan config:clear
Env环境
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox**************.mailgun.org
MAIL_PASSWORD=************************
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=youraccountemailaddress
MAIL_FROM_NAME="${APP_NAME}"
I would not use the sandbox domain, as it has problems, does not work like a real domain and there is no reason to not start with the real domain for free .我不会使用沙盒域,因为它有问题,不像真正的域那样工作,而且没有理由不免费使用真正的域。
Short description to start with MailGun with your own domain:使用您自己的域从 MailGun 开始的简短描述:
Further notes to development (I work with Visual Studio 2017, Windows Forms and vb.net):开发的进一步说明(我使用 Visual Studio 2017、Windows Forms 和 vb.net):
I had to set TLS 1.2 to be able to connect:我必须设置TLS 1.2才能连接:
vb.net code: vb.net 代码:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
I further have added the NuGet package: RestSharp (as the code examples in MG are based on RestSharp).我进一步添加了 NuGet 包:RestSharp (因为 MG 中的代码示例基于 RestSharp)。
Code example for vb.net (after adding the NuGet to RestSharp): vb.net 的代码示例(在将 NuGet 添加到 RestSharp 之后):
Imports System.Net
Imports System.Net.Mail
Imports RestSharp
Imports RestSharp.Authenticators
...
Dim client As RestClient = New RestClient()
client.BaseUrl = New Uri("https://api.eu.mailgun.net/v3/")
Dim cAPIKey As String = "YourKey from MG"
client.Authenticator = New HttpBasicAuthenticator("api", cAPIKey)
Dim request As RestRequest = New RestRequest()
request.AddParameter("domain", "YourDomain", ParameterType.UrlSegment)
request.Resource = "{domain}/messages"
request.AddParameter("from", "Your Description <Mailbox@YourDomain>")
' add mail address(es):
request.AddParameter("to", "mail-address1")
' further, if needed (Note: I send a single email to each recipient):
request.AddParameter("to", "mail-address2")
' add bcc mail address (if needed):
request.AddParameter("bcc", "bcc-mail-address")
' fill the mail:
request.AddParameter("subject", cMailBetreff)
request.AddParameter("html", cHTMLMailBody)
request.Method = Method.POST
Dim status As IRestResponse
status = client.Execute(request)
' state handling:
If status.StatusCode.ToString = "OK" Then
' O.K handling
Else
' Error handling
End If
Notes to the code above:上面代码的注释:
I send a complex html, that I have stored in a file that is loaded in the variable cHTMLMailBody.我发送了一个复杂的 html,我将它存储在一个加载到变量 cHTMLMailBody 中的文件中。
After the load, some variables in the file are change to customer values stored on a SQL server (cMailBetreff also contains a customer specific generated text).加载后,文件中的一些变量将更改为存储在 SQL 服务器上的客户值(cMailBetreff 还包含客户特定的生成文本)。
As I'm not from thee USA, I have to use the url: https://api.eu.mailgun.net/v3/" .由于我不是来自美国,我必须使用以下网址: https : //api.eu.mailgun.net/v3/" 。
If you are from the USA, you have to use another url (see MG portal).如果您来自美国,则必须使用另一个 url(请参阅 MG 门户)。
Notes to the error handling in MG (Log in MG): MG中的错误处理注意事项(登录MG):
I have done some tests with the code above (that works).我已经用上面的代码做了一些测试(有效)。 As I also want to know (and store), if the sent email has reached the recipient, I have done some tests and sent emails to a not existing domain and an existing domain, but not existing mailbox.
正如我也想知道(和存储),如果发送的电子邮件已到达收件人,我已经做了一些测试并将电子邮件发送到不存在的域和存在的域,但不存在的邮箱。
To the not existing domain, an error was logged in MG (what is correct).对于不存在的域,MG 中记录了错误(正确的是什么)。 To the not existing mailbox, the state OK was logged in MG (what is wrong ).
对于不存在的邮箱,状态 OK 已登录 MG(出了什么问题)。 The reason to the wrong state (OK) to the not existing mailbox was, that MG don't receive non delivery mails, if there is no MX record to the domain that targets the MG server.
不存在邮箱的错误状态 (OK) 的原因是,如果针对 MG 服务器的域没有 MX 记录,则 MG 不会收到未投递邮件。 As we (manually) also send out emails to the domain from our own mail server, I don't wanted to change the mx record to our domain in general to the MG server.
由于我们(手动)也从我们自己的邮件服务器向域发送电子邮件,因此我不想将我们域的 mx 记录一般更改为 MG 服务器。
Therefore I have added a sub domain to our domain (in MG and also at our DNS provider) and then added a MX record to the new sub domain that points to the MG server.因此,我增加了一个二级域名,以我们的域(MG,并在我们的DNS提供商),然后添加一个MX记录到新的子域指向MG服务器。
Then, I have changed the domain parameter to SubDomain.Domain:然后,我将域参数更改为 SubDomain.Domain:
request.AddParameter("domain", "SD.Domain", ParameterType.UrlSegment)
The problem then was, that a not nice "on behalf of" was generated automatically in the header.然后的问题是,在标题中自动生成了一个不好的“代表” 。 I was able to solve the issue by adding a further parameter:
我能够通过添加另一个参数来解决这个问题:
request.AddParameter("h:sender", "Your Description <Mailbox@YourDomain>")
(same value as in from parameter, whereby the h: is needed in addition) After doing that, MG also receives non delivery emails and add also an error to not existing mailboxes. (与 from 参数中的值相同,其中还需要 h :) 这样做之后,MG 还会收到未送达的电子邮件,并向不存在的邮箱添加错误。
So.. if you need to get/store the states to the sent emails and don't want to change your mx record generally to MG, you should add a sub domain (if not, you should not need a sub-domain).所以..如果您需要获取/存储已发送电子邮件的状态并且不想将您的 mx 记录一般更改为 MG,您应该添加一个子域(如果不是,则不需要子域)。
For me, everything now seems to work as it should:对我来说,现在一切似乎都正常工作:
Hope this helps somebody...希望这可以帮助某人...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.