I would use a transactional email service for sending these types of emails. You can set them up with a domain like mail.domain.tld and then send them from the same email you have above. Gmail isn't meant for this and it will clog up your sent folder with these emails being sent.
Email works on Local Environment, but not Production Environment
Here are my environment variables related to email:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=********
MAIL_PASSWORD=********
MAIL_ENCRYPTION=tls
Both my Local and Production environment share these settings (Same Username and Password too).
And here's my AppMailer class that ends the emails:
<?php
namespace App\Mailers;
use Illuminate\Contracts\Mail\Mailer;
use App\Models\User;
class AppMailer
{
protected $mailer;
protected $from;
protected $to;
protected $view;
protected $data = [];
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* Delivers the View as an Email to the specified Receiver, given
* a Sender and their Name.
*
* @param View $view The View for the Email.
* @param array $data The Data for the View.
* @param string $to The Email Address to send the Email to.
* @param string $from The Email Address that sent the Email.
* @param string $name The Name of the Person that sent the Email.
*
* @return void
*/
public function deliver($view, $data, $to, $from = null, $name = 'No-Reply')
{
// Resolve From Address
if($from == null)
$from = env('MAIL_USERNAME');
$this->mailer->send($view, $data, function($message) use ($from, $to, $name)
{
$message->from($from, $name)
->to($to);
});
}
/**
* Sends an Account Validation Link to the specified User.
*
* @param User $user The specified User.
*
* @return void;
*/
public function sendValidationRequest(User $user)
{
$this->deliver('emails.validate', compact('user'), $user->email);
}
/**
* Sends a Password Reset to the specified User.
*
* @param User $user The specified User.
*
* @return void;
*/
public function sendPasswordResetRequest(User $user)
{
$this->deliver('emails.password_reset', compact('user'), $user->email);
}
}
When I register a User on my site, I expect said User to receive an email from me, prompting them to validate their account.
When I register locally, it works just fine. I receive the email in the inbox of the registered user's email account, and it's from the email listed in the Environment variables.
However, when I register on the production site (Which is hosted on GoDaddy), I receive the following error:
Swift_TransportException in AuthHandler.php line 181:
Failed to authenticate on SMTP server with username "cdp.admin-noreply@bible.org" using 2 possible authenticators
This is a followup from this thread, where I initially couldn't get emails to work on either environment.
Some things to note about the account being used as the sender:
- The Email resolves as a Gmail account
- 2-Step Verification is not enabled
- Allow Less Secure Apps is enabled
- The Account is managed through Google Apps to allow a separate domain (Like me@mydomain.com, where I log into Google using me@mydomain)
As far as I can tell, GoDaddy (the host provider for the Production Site) does not interfere with this. If the port or address didn't work because GoDaddy was blocking it, I would receive a different error (to my knowledge).
So now the question is: Why isn't this working on the Production site?
Please or to participate in this conversation.