How can I transform a PHPMailer connection to Laravel .env? - laravel

I have a situation where an app is sending its mails with PHPMailer and next configuration:
$mail->isSMTP();
$mail->Host = 'myhost.dev';
$mail->SMTPAuth = false;
$mail->setFrom($from, $from_name);
$mail->addAddress($to, $to_name);
if ($replyTo != '') $mail->addReplyTo($replyTo, $replyToName);
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $body;
As you can see, SMTP server doesn't require authentication and all is working ok. That's all data I have about the mail server. Now, I need to make the same thing but instead of using PHPMailer I want to implement the sending method of Laravel, but I don't have any authentication credential for setting my .env file.
How can I pass the same configuration to my Laravel development? I've tried many combinations but nothing happens. This was the last one:
MAIL_DRIVER=smtp
MAIL_HOST=myhost.dev
MAIL_PORT=25
MAIL_USERNAME=no-reply#host.com
MAIL_PASSWORD=''
MAIL_ENCRYPTION=''

PHPMailer has no idea about Laravel, so you need to pass through the information yourself. Laravel provides the env() global function to retrieve data from the environment, and you would use it like this:
$mail->Host = env('MAIL_HOST');
if (!empty(env('MAIL_USERNAME')) {
$mail->SMTPAuth = true;
$mail->Username = env('MAIL_USERNAME');
$mail->Password = env('MAIL_PASSWORD');
}
and so on, for any other configuration variables you need. I'm not sure exactly what Laravel package defines the env function, but you will need to make sure it's loaded and within scope before calling it.

Related

How to access variables from database in Email class?

I need to set SMTPUser, Pass and Host in Email Class as variables taken from MySQL database. I can set them in Controllers like this:
$configModel = new ConfigModel();
$config = $configModel->where('id', 1)->find();
$user = $config[0]['mail_user'];
$pass = $config[0]['mail_pass'];
$server = $config[0]['mail_server'];
but how to use them in Email Class? Should I set them as some session variables and access them in Email class? Is it safe?
Additionaly, I've also tried doing email initialization in my Controller like this:
$emailConfig['SMTPUser'] = $user;
$emailConfig['SMTPPass'] = $pass;
$emailConfig['SMTPHost'] = $server;
$emailConfig['SMTPPort'] = 465;
$emailConfig['SMTPCrypto'] = 'ssl';
$emailConfig['protocol'] = 'smtp';
$email->initialize($emailConfig)
but it gives me error:
Sender address rejected: need fully-qualified address
So logging to email is successful, but it cannot sent mails for some reason. Is this option missing some configuration?
I'd rather use default Email Class provided by CI4 with database variables.

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.

how to make working SMTP_validateEmail on live site using php

Not working SMTP_validateEmail on live site using php
https://code.google.com/p/php-smtp-email-validation/
<?php
/**
* Example 1
* Validate a single Email via SMTP
*/
// include SMTP Email Validation Class
require_once('smtp_validateEmail.class.php');
// the email to validate
$email = $_POST['name'];
// an optional sender
$sender = $_POST['name'];
// instantiate the class
$SMTP_Validator = new SMTP_validateEmail();
// turn on debugging if you want to view the SMTP transaction
//$SMTP_Validator->debug = true;
// do the validation
$results = $SMTP_Validator->validate(array($email), $sender);
// send email?
if ($results[$email]) {
echo 1;
} else {
echo 0;
}
enter code here
?>
i am having problem on live site but it works fine in localhost.it shows the print_r($results) in the localhost but shows array(0) result in live.i also find $this->sock value in the class smtp_validateEmail.class function validate(email) value empty in live. how to solve this issue any help will be very thankful.
That "Add your web server ip address to var $nameservers = array(‘192.168.0.1’); and if your web server has Port number then update it here var $port = (Port number); Hope you will get the output by making this changes." Only work if you are sending mail with that script but cant tell if that mail checked is good one or not... I have been testing the code, try all changes, but still cant check other mails outside my domain. I can only make it work on my local and webserver host. Still cant call other servers to check if other mails from outside my domain are valid. Working on it.
Complete downloads are here for others to test : https://code.google.com/p/php-smtp-email-validation/downloads/list
Add your web server ip address to var $nameservers = array(‘192.168.0.1’);
and if your web server has Port number then update it here var $port = (Port number);
Hope you will get the output by making this changes.

SMTP Codeigniter email error '354 End data with .'

I'm having trouble getting the Codeigniter email library working on a new server.
This code has previously worked before but has recently stopped working and I cannot for the life of me figure out why. Essentially, here is the code:
$this->email->from('example#example.com', 'Intranet');
$this->email->to($c['email']);//This is def a valid email
//$name is being obtained from elsewhere
$this->email->subject('Time manager reminder');
$this->email->message("
{$name[0]},
<br/><br/>You haven’t completed your time for today. Please don’t forget to do so<br/><br/>
Intranet
");
$this->email->send();
And the email.php config password
$config['mailtype'] = "html";
$config['priority'] = 1;
$config['protocol'] = "smtp";
$config['smtp_host'] = "my server..";
$config['smtp_user'] = "u/n";
$config['smtp_pass'] = "p/w";
$config['smtp_timeout'] = 1;
$config['validate'] = true;
The error I receive from $this->email->print_debugger(); is as follows:
data: 354 End data with .
The following SMTP error was encountered:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
You can view the full error on pastebin here http://pastebin.com/y9UeaEGY
I've crossed out the emails in all places, but I can assure you they are valid and used email addresses.
I'd appreciate any help you can offer. Thanks
I figured out the reason. Apparently, in the config, you need to set
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
manually in the email.php file. Codeigniter doesn't seem to do it by default. Odd :)

Code Igniter email config to config.php

I've put my email configurations in the config.php but I don't know how I can access them in my controller.
In my config.php:
/*EMAIL CONFIG*/
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'pass';
$config['smtp_port'] = '465';
$config['mailtype'] = 'html';
Should I make an instance of the super global object?
Thanks in advance.
You don't put email config settings in config.php. You make a new file called email.php and place it in the config folder. CI will automatically detect the email config setting from that file.
Look under "Setting Email Preferences in a Config File"
http://ellislab.com/codeigniter/user-guide/libraries/email.html
You can access it with:
$this->config->item('protocol');
Also, see the relevant chapter in CodeIgniter User Guide

Resources