Laravel Mailgun : Emails accepted but not delivered for addresses with the same domain - laravel

I have a domain https://magicamaids.com and a sub domain related to it is mailing.magicamaids.com. I configured mailgun with sub domain DNS. All TXT, MX, CNAME showing verified.
But I can't send email to anyuser#magicamaids.com from no-reply#magicamaids.com. But I can send email to any other gmail or outlook mail account from no-reply#magicamaids.com.
Note: Basically i have a contact-us form submission page. and i need to send email to sales#magicamaids.com. Here is my code
Mail::to(['sales#magicamaids.com'])->send(new ContactUs($data));
My .env file is
MAIL_FROM_ADDRESS=no-reply#magicamaids.com
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=mailing.magicamaids.com
MAILGUN_SECRET=4a6ec.....
MAILGUN_ENDPOINT=api.eu.mailgun.net

Related

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
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 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 :
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.
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.
In Account Security->API security->API keys of my mailgun account I have parameters 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 ?
UPDATED BLOCK 2:
I try to fill parameters from live domain, but failed.
I search paramters here : 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
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-
If still not working then change mailer to 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:
Create an account at mailgun including a credit card
-> If you don't add a credit card, you will not be able to use MG
-> This don't means, that you have to pay unless you don't reach the limit
of 5'000 emails / month
add your (existing) domain in MG (only possible, if you have added a CC)
under "Sending" - button "Add new Domain" (Button only visible if a CC is added)
follow exactly the steps that are showed at the right top corner
in the MG portal. Title "Getting started"
You have to reach 100% before you will be able to send your first emails
Note:
This also includes "illogical" steps - e.g. send an email to mailgun#YourDomain
although, a mailbox "mailgun" does not exist
You further have to send an email to yourself
These steps seems to be needed for initial verification at MG
add exactly the DNS entries from the MG portal (spf and mta) to your domain
To do this you need access to the portal of your domain provider
in MG - under DNS Records - press button "Verify DNS settings"
the DNS settings have to be verified (a green check is showed), before you can send emails
If anything is O.K., you will receive an eMail from MG:
Good news! The DNS settings you added for YourDomain have been verifyed.
If you have received this email, you should be able to send emails over MG
Further notes to development (I work with Visual Studio 2017, Windows Forms and vb.net):
I had to set TLS 1.2 to be able to connect:
vb.net code:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
I further have added the NuGet package: RestSharp (as the code examples in MG are based on RestSharp).
Code example for vb.net (after adding the NuGet to 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.
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).
As I'm not from thee USA, I have to use the url: https://api.eu.mailgun.net/v3/".
If you are from the USA, you have to use another url (see MG portal).
Notes to the error handling in MG (Log in 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).
To the not existing mailbox, the state OK was logged in MG (what is wrong).
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.
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.
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.
Then, I have changed the domain parameter to 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.
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).
For me, everything now seems to work as it should:
we can sed out html mails over MG
we can capture the states to the sent emails over the 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...

Send mail with Laravel Lumen

I've a domain and an email address dedicated. I've a SMTP server provided by my domain. I'm asking myself if it's possible to send mail directly with this SMTP server without passing through API like MailGun (because, it's not free!), and how can I do that ?
Thanks !

Laravel send email verification to a registered user

I am new to Laravel. I have easily implemented Laravel email verification for the registered based on Laravel https://laravel.com/docs/5.8/verification
its working fine for testing purpose in local server. now I want to upload to server. now it's using default Mailtrap to receive an email of all the users.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=****
MAIL_PASSWORD=****
MAIL_ENCRYPTION=tls
//user.php
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
My requirement is after registering, the email should send to the registered emailId, not to the mailtrap account. I have researched a lot on this in google but all the solution is only for sending emails to mailtrap or others but instead, I want to send to the users' email ID with the verification link.
I am assuming you have successfully implemented the activation email.
For your application to send email to the registered user email address you need to provide your email server SMTP credentials. For live application, you can not use mail trap credentials. If you use mailtrap credentials you will only get emails to your mail trap account.
If you do not have live smtp server credential, you can create a Gmail account and user Gmail SMTP credentials.
And one more thing, if you are using Gmail, change settings for Less secure app from your google account.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=****
MAIL_PASSWORD=****
MAIL_ENCRYPTION=tls
this is .env configuration you have change to your own mail server. so that laravel will send email using these settings to the registered users.

Laravel mail on shared hosting

I've a form contact on my website and I want the form to be sent via email when submitted.
I've tried it in xampp and it's working fine.
On shared hosting, it does not work.
I'm using my own custom domain smtp.
Someone can explain me why it's not working ?
Your shared hosting probably blocks outgoing SMTP connections. Many hosting providers do that to prevent spam. You can try using a HTTP/WEB API instead of SMTP to send email.
Some email APIs like Flute Mail allow you to set up an HTTP API connection which can immediately forward the request through your "custom domain smtp" server. So you can keep using your custom domain email server, but get an API for it.
Otherwise you'll have to set up a Web API yourself with an open source tool like Postal.
In your laravel .env file if add following parameters mentioned below and create a e-mail on your shared hosting and add username, password and host of your hosting.
MAIL_DRIVER=smtp
MAIL_HOST=shared_host_name
MAIL_PORT=587
MAIL_USERNAME=mail#sharedhost.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
I suppose then it would work fine.

MailGun Laravel - Cant send to gmail

I have mailgun setup and working with my custom domain name, as in, I can send test emails to me#mydomain.com but when I try to send to gmail I get the following error.
ClientException in RequestException.php line 107:
Client error: `POST https://api.mailgun.net/v3/mydomain.com/messages.mime` resulted in a `400 BAD REQUEST` response:
{
"message": "Please activate your Mailgun account. Check your inbox or log in to your control panel to resend the act (truncated...)
My mailgun account is setup to work with my domain name correctly and my custom email address doesn't match my site domain name and mail gets delivered to it from mailgun no problems...
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=mydomain.com
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#mydomain.com
MAIL_PASSWORD=ljhasdlkfhklahsdfklhklasdhflkhasdlkfhkhasdkflh
MAILGUN_SECRET=key-asdflkhjaklsdfkljaslkdfjlkjasdfkj
MAIL_FROM=postmaster#mydomain.com
MAIL_ENCRYPTION=tls
Its weird that when trying to send to a gmail address it gives tells me I need to activate my mailgun account but when sending to a custom domain name address its works perfectly, anyone have any ideas.. Here is the function I am using to send the emails
Mail::send('emails.recontact', ['title' => $title, 'content' => $content], function ($message) use ($request){
$message->from( 'me#mysite.ie', $request->input('name') );
$message->to('myname#gmail.com');
$message->subject("Website Enquiry");
});
You may need to activate you account.
Please login to your mailgun account and make sure there is no a yellow message on the top of the screen that said:
"Please activate your account to start sending emails. We sent an activation email to {your_email}. Resend activation. Update email address."
This solved my problem :)
I am working on mailgun but i faced different problem when recipient reply mail stores instead of delivered

Resources