简体   繁体   English

我可以在本地操作系统下使用 mailgun 沙盒域发送电子邮件吗?

[英]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 开始的简短描述:

  • Create an account at mailgun including a credit card在 mailgun 创建一个帐户,包括信用卡
    -> If you don't add a credit card, you will not be able to use MG -> 如果不添加信用卡,将无法使用MG
    -> This don't means, that you have to pay unless you don't reach the limit -> 这并不意味着你必须支付,除非你没有达到限额
    of 5'000 emails / month 5'000 封电子邮件/月
  • add your (existing) domain in MG (only possible, if you have added a CC)在 MG 中添加您的(现有)域(仅可能,如果您添加了 CC)
    under "Sending" - button "Add new Domain" (Button only visible if a CC is added)在“发送”下 - 按钮“添加新域”(按钮仅在添加 CC 时可见)
  • follow exactly the steps that are showed at the right top corner完全按照右上角显示的步骤操作
    in the MG portal.在MG门户中。 Title "Getting started"标题“入门”
    You have to reach 100% before you will be able to send your first emails您必须达到100%才能发送第一封电子邮件
    Note:笔记:
    This also includes "illogical" steps - eg send an email to mailgun@YourDomain这还包括“不合逻辑”的步骤 - 例如发送电子邮件至 mailgun@YourDomain
    although, a mailbox "mailgun" does not exist虽然,邮箱“mailgun”不存在
    You further have to send an email to yourself您还必须向自己发送电子邮件
    These steps seems to be needed for initial verification at MG MG 的初始验证似乎需要这些步骤
  • add exactly the DNS entries from the MG portal (spf and mta) to your domain将 MG 门户(spf 和 mta)中的 DNS 条目准确添加到您的域中
    To do this you need access to the portal of your domain provider为此,您需要访问域提供商的门户
  • in MG - under DNS Records - press button "Verify DNS settings"在 MG - 在 DNS 记录下 - 按下按钮“验证 DNS 设置”
    the DNS settings have to be verified (a green check is showed), before you can send emails必须先验证DNS 设置(显示绿色对勾),然后才能发送电子邮件
  • If anything is OK, you will receive an eMail from MG:如果一切正常,您将收到来自 MG 的电子邮件:
    Good news!好消息! The DNS settings you added for YourDomain have been verifyed.您为 YourDomain 添加的 DNS 设置已经过验证。
  • If you have received this email, you should be able to send emails over MG如果您收到此电子邮件,您应该可以通过 MG 发送电子邮件

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:对我来说,现在一切似乎都正常工作:

  • we can sed out html mails over MG我们可以通过 MG 发送 html 邮件
  • we can capture the states to the sent emails over the MG API我们可以通过 MG API 捕获发送电子邮件的状态
  • we still can send (manually) emails over our own mail server我们仍然可以通过我们自己的邮件服务器发送(手动)电子邮件
    And important:重要的是:
  • if a recipient answers to an email, the answer is delivered to our mailbox on our server如果收件人回答到电子邮件,答案发送到我们的邮箱我们的服务器上

Hope this helps somebody...希望这可以帮助某人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mailgun:如何从我的本地机器(Laravel)发送电子邮件 - Mailgun: How can send email from my local machine (Laravel) Laravel不会将我的域传递给MailGun驱动程序,因此我无法发送邮件 - Laravel won't pass my domain to the MailGun driver so I can't send mail 只能使用Laravel 5和Mailgun向自己发送电子邮件 - Can only send email to myself using Laravel 5 and Mailgun 使用 Mailgun 帐户发送电子邮件我得到的域未经验证且需要 DNS 配置错误 - Sending email with Mailgun account I got the domain is unverified and requires DNS configuration error 如何使用 Mailgun 在 Laravel 中发送 Email? - How to send Email in Laravel using Mailgun? 如何使用 Mailgun 和 Laravel 发送电子邮件? - How to send Email using Mailgun and Laravel? 在 laravel 5.2 上使用 mailgun - 无法验证我的沙箱帐户 - Using mailgun on laravel 5.2 - failed to authenticate my sandbox account 我已经在我的应用程序 .env 中设置了我的域和密码,但是我一直无法解析 mailgun 和 laravel 中的错误 - I have set up my domain and secret in my app .env, but i keep getting unable to parse error in mailgun and laravel Laravel 和 mailgun 设置正确,但不会发送电子邮件 - Laravel and mailgun set up correctly, but wont send email 如何发送带有 Laravel 的模板 Mailgun email? - How do you send a templated Mailgun email with Laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM