How to send email from Laravel to Real email address - laravel

I have a laravel app with a contact form. In local I was using mailtrap.io to test for emails being sent from the contact form and the emails were going through. I deployed my app and now when I submit a contact form, the user information gets stored in my database but the page re-directs and throws a 500 error. How do I send emails in a production enviornment? In local dev the page redirected back to my home and gave a flash message perfectly.
.env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=mailtrap-username
MAIL_PASSWORD=mailtrap-pw
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=myemail#gmail.com
MAIL_FROM_NAME="My Name Here"

Please follow these steps to send Email.
.Env
MAIL_DRIVER = smtp
MAIL_HOST = smtp.gmail.com
MAIL_PORT = 587
MAIL_USERNAME = your-gmail-username
MAIL_PASSWORD = your-application-specific-password
MAIL_ENCRYPTION = tls
Send through Mail class with raw function.
\Mail::raw('Hi, welcome user!', function ($message) {
$message->to(..)
->subject(..)
->setBody('<h1>Hi, welcome user!</h1>', 'text/html');
});
Second way is send function:-
\Mail::send([], [], function ($message) {
$message->to('my#email.com')
->subject('my subject')
->setBody('Hi, welcome user!');
});
Hope It will work for you. Thanks!

Related

Laravel dynamic email not sending by Mailable Class

In my case, In the Mailable Class I store the email in the variable hardcode form LIKE
$email = 'abc#example.com';
then, its work fine. but when we pass the email dynamically from the Controller LIKE
$this->email;
this shows the error are given below :
Expected response code 250 but got code "550", with message "550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements "
$subject = 'Account Created Successfully';
$name = 'Saakin Qatar';
return $this->view('emails.AgencyRegisterMail')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with('inputs', $this->inputs);
It works but when I pass the address from the controller and store it like below:
$subject = 'Account Created Successfully';
$name = 'Saakin Qatar';
$name = 'Saakin Qatar';
return $this->view('emails.AgencyRegisterMail')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with('inputs', $this->inputs);
then it returns the error
Twilio SendGrid developer evangelist here.
The error message here said:
Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements
On the page it recommends you visit it describes that to send emails you need to send them from either an email address that has been verified or an email address from an authenticated domain.
My guess is that you are building a contact form of sorts and trying to send the mail from an email address that a user enters into the form. If that is the case, you cannot use the user provided email address as the from parameter for sending the email, you have to use an email address that is verified or authenticated. But you can set the user's email address as the replyTo so that when you reply from your email client you write back to the user.

Send emails in laravel from a web server

I have a Laravel project that I need to send emails to, and it works on localhost, but when I upload it to a web server it doesn't work. Where's my mistake?
Controller:
class merluzaController extends Controller
{
public function mail(request $request){
$datos=[
"nombre"=>$request->nombre,
"apellido"=>$request->apellido,
"correo"=>$request->correoE,
"numeroT"=>$request->numeroT,
"mensaje"=>$request->mensaje,
];
Mail::send('emails.mails', $datos, function ($message) {
$message->to("ltomicb#gmail.com", "Lucas")->subject('Página de MerluzaDePincho');
});
Session::flash('mensaje_enviado','Mensaje enviado correctamente.');
return redirect('/contacto');
}
}
ENV:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandbox8f7345b6f0f14762871d8489a6a672c9.mailgun.org
MAIL_PASSWORD=****************
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=merluzadepincho#gmail.com
MAIL_FROM_NAME="Merluza de Pincho"
if the problem is "Class 'Illuminate\Support\facades\Mail' not found then you have to import it
you can try to use
use Illuminate\Support\Facades\Mail;
or you can try
\Mail::send('emails.mails', $datos, function ($message) {
$message->to("ltomicb#gmail.com", "Lucas")->subject('Página de MerluzaDePincho');
});
instead of
Mail::send('emails.mails', $datos, function ($message) {
$message->to("ltomicb#gmail.com", "Lucas")->subject('Página de MerluzaDePincho');
});
as \Mail::send() doesn't need to be imported
you can check this article you can find here some good answers
if nothing of this works then you need to verify that guzzle package is configured correctly and its version is 6 higher and look into your server php modules and compare them to your dev modules php -m should list them all
finally reach out to your hosting company and explain the issue for them as some companies block this kind of callbacks for security reasons or to force the user to purchase a premium account or something like that
hope this helps.

AWS SES setup - unable to send test mail

I configured AWS SES as per this document.
Unable to send mail from a SES 'verified' email id (info#medicovibes.com)
If I run this code from an AWS EC2 instance, I get a blank page or 500 error. no error message pertaining to the problem can be seen.
<?php
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
// Instantiate a new PHPMailer
$mail = new PHPMailer;
// Tell PHPMailer to use SMTP
$mail->isSMTP();
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$mail->setFrom('info#medicovibes.com', 'rimo ');
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
// Also note that you can include several addAddress() lines to send
// email to multiple recipients.
$mail->addAddress('support#dotweb.in', 'Vikas');
// Replace smtp_username with your Amazon SES SMTP user name.
$mail->Username = 'concealed';
// Replace smtp_password with your Amazon SES SMTP password.
$mail->Password = 'concealed';
// Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
//$mail->addCustomHeader('X-SES-CONFIGURATION-SET', 'ConfigSet');
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
// The subject line of the email
$mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)';
// The HTML-formatted body of the email
$mail->Body = '<h1>Email Test</h1>
<p>This email was sent through the
Amazon SES SMTP
interface using the <a href="https://github.com/PHPMailer/PHPMailer">
PHPMailer</a> class.</p>';
// Tells PHPMailer to use SMTP authentication
$mail->SMTPAuth = true;
// Enable TLS encryption over port 587
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Tells PHPMailer to send HTML-formatted email
$mail->isHTML(true);
// The alternative email body; this is only displayed when a recipient
// opens the email in a non-HTML email client. The \r\n represents a
// line break.
$mail->AltBody = "Email Test\r\nThis email was sent through the
Amazon SES SMTP interface using the PHPMailer class.";
if(!$mail->send()) {
echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
} else {
echo "Email sent!" , PHP_EOL;
}
?>
You can try to enable debug in PHP mailer and execute it from CLI.
php filename.php
https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging
You should be able to see where is the problem.

Laravel Mail the from and replyTo are ignored in the received email

I have problem with the Laravel's Mail. I have nothing wrong with the source code to send an email. The email was sent but the email address of sender is mine instead of sender. It is the one in the config file. The replyTo was ignored also.
My code is as following:
Mail::send('appointments.emails.new_appointment', ['data' => $data], function($message) use ($sender)
{
$message->from($sender['email'], $sender['name'])
->to('myemail#gmail.com', 'My Name')
->replyTo($sender['email'], $sender['name'])
->subject('Contact from ' . $sender['name']);
});
I received an email with sender name and my email address. And, when I click on rely, it replys to myself. That means the replyTo didn't work. How can I fix that?
Sorry if my English is not so good.

Use simple Php mail function in laravel instead of SMTP in Hostgator

I want to use simple php mail function in laravel (using hostgator hosting) instead of smtp what
changes should i do in mail.php and .env to achieve it in
Hostgator
.env file
MAIL_DRIVER=mail
MAIL_HOST=mail.mydomail.com
MAIL_PORT=587
MAIL_USERNAME=my email address
MAIL_PASSWORD=email password
MAIL_ENCRYPTION=none //as SSL is disabled
I want to implement php mail function in laravel but due to some reason its not working although when i run simple php script i.e
SIMPLE PHP SCRIPT that i tested with hostgator it is sending mail I just want to implement the same using laravel
Below is my index.php
$to = "mailhostingserver#gmail.com";
$subject = "This is subject";
$message = "This is simple text message.";
$header = "From:support#domain.in \r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else { echo "Message could not be sent..."; } ?>
I just want to implement the above index.php code in laravel

Resources