Laravel use view while sending email from Gmail API - laravel-5

I am trying to send emails using Gmail API. It worked fine if I use raw body, but I am unable to figure out how to use Laravel Views as the body when sending the email from Gmail API . Or use Gmail API to send using Laravel Mail
Here is my code:
$strSubject = 'Test mail using GMail API';
$strRawMessage = "To: Someone <someone#domain.com>\r\n";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= "MIME-Version: 1.0\r\n";
$strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$strRawMessage .= "this is body";
$service = new Google_Service_Gmail($client);
try{
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message($client);
$msg->setRaw($mime);
$service->users_messages->send("me", $msg);
}
catch(Exception $e){
\Log::info($e->getMessage());
}

First thing, you should be able to generate a "mailable" class with php artisan make:mail command. This way, you will be able to use laravel mail api, meaning be able to use view and stuff.
Second thing, laravel should be able to send the mail for you, in order to be able to do it it requires some configuration.
What version of Laravel are you using?

Related

How to send Multiple mail Using laravel?

i'm new to laravel, i need to send multiple emails using laravel it is possible ?
i'm try :-
Mail::send('mail.Vehicle_request', compact('result_email'), function($message) use ($result_email) {
$message->to($result_email->email, $result_email->first_name . ' ' . $result_email->last_name)
->subject('Vehicle Request');
});
Thank You
You can use mailable and send mail to multiple recipients adding them in the loop. You can refer more in the documentation link.
https://laravel.com/docs/8.x/mail
foreach (['taylor#example.com', 'dries#example.com'] as $recipient) {
Mail::to($recipient)->send(new OrderShipped($order));
}

Google recaptcha when posting to different server

I have a form with recaptcha V2:
https://www.fisherwallace.com/pages/do-you-qualify-to-use-the-device
It posts to a different server and then redirects back to a different page on the server with the recaptcha form.
Recaptcha site says: "We detected that your site is not verifying reCAPTCHA solutions." I assume it's due to posting to the different server.
NOTE: You'll see I have a clumsy workaround at the moment to address the fact that the recaptcha does not challenge automatically. Without the workaround, the recaptcha is there but nothing happens on submit.
I found some sample PHP code for the server side...
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}
if(isset($_POST['comment'])){
$comment=$_POST['comment'];
}
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo '<h2>Thanks for posting comment</h2>';
} else {
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}
Clearly lots of this code is moot since the user does not actually load the POST server page.
But will this part get the callback recaptcha needs?
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
If the IP is a mismatch, can i hard code it using the IP from the originating server?
IP is optional
You need to do file_get_contents 'method' => 'POST' for siteverify

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

mail() on https

I setup a website that uses PHP mail(). I can get it to successfully run if I hit the script on http:// but if I switch to https:// it does not work! I use Godaddy for hosting and have a certificate purchased through them. I am not sure if I need to setup anything in the php.ini file or not. My mailscript looks like this:
$from_respondent_email = "calendar#mydomain.com" ;
$headers_respondent_email = "From: $from_respondent_email";
$subject_respondent_email = "Technical Support Request";
$body_respondent_email = "We received a support ticket from mydomain.com:\n\n";
$body_respondent_email .= "Test block of text";
$send = mail("myname#gmail.com", $subject_respondent_email, $body_respondent_email, $headers_respondent_email);
$to ="myname#gmail.com";
$subject = "Technical Support Request";
$message = "We received a support ticket from mydomain.com:\n\n";
$from = "calendar#mydomain.com";
$headers = "From: $from_respondent_email";
mail($to,$subject,$message,$headers);
Try this. It'll work. I used this in my website and its working :)

Could not instantiate mail function in joomla 2.5.4

I am trying to send mail from my component when the user registers. I tried all three i.e PHPMail, Sendmail and SMTP but none works. I am testing it in localhost (WampServer 2.2) and the Joomla version is 2.5.4. I am not sure this is WampServer problem or Joomla or my own code. This is my code:
function postmail($name, $receiver){
jimport('joomla.mail.helper');
$mailer =& JFactory::getMailer();
$config =& JFactory::getConfig();
$sender = array($config->getValue( 'config.mailfrom'),$config->getValue( 'config.fromname' ) );
$mailer->setSender($sender);
$recipient = $receiver;
$mailer->addRecipient($recipient);
$body = "<body><br><p>";
$body .= "<strong>$name,</strong> ";
$body .= "</p><br></body>";
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$send =& $mailer->Send();
if ( $send != true ) {
//echo 'Error sending email: ' . $send->message;
return false;
} else {
//echo 'Mail sent';
return true;
}
}
It gets the error 'could not instantiate mail function'.
i am sure there are many reasons causing this problem. but to test you can always create a mail server of your own by installing this tool here http://www.toolheap.com/test-mail-server-tool/
you don't have to change anything except that you put localhost as the smtp host in joomla mail settings and then in your webserver's php.ini, add the smtp=localhost. thats it, it should work.
this is just to test whether your code is correct and you are using the correct mail settings in joomla. if this doesn't work then, i am sure the problem is not in your code but in your mail settings.
To understand what the problem is you really need to get the error message.
Change
//echo 'Error sending email: ' . $send->message;
to
echo 'Error sending email:'.$send->get('message');
then run it again. The error that you get should tell us why it isn't instantiating. Let us know what it says so we can help you out.

Resources