When I run this
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "xyssoftware#gmail.com";
$to = $from;
$subject = "PHP Mail Test script";
$message = "This is a test to check the PHP Mail functionality";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>
page email works.
But when I hit my mail::send method it runs to this error:
Swift_TransportException in StreamBuffer.php line 268: Connection
could not be established with host mailtrap.io [Connection refused
#111]
IF you are sending an email from your webserver and have sendmail and postfix installed.
Then you have to change your email driver from your .env to MAIL_DRIVER=mail
MAIL_DRIVER=mail
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
if u are in development then you can simply change the driver to log.
MAIL_DRIVER=log
Hope this helps
Related
I have laravel 7.3, and sql server 16,
i need to connect to the database sql server, but i have this error :
SQLSTATE = 08001 error- No connection could be made because the target machine actively refused it.
and there is the .env file
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1/instancename
DB_PORT=1433
DB_DATABASE=Database
DB_USERNAME=sa
DB_PASSWORD=12345678`
But when i use the simple php file to connect the connexion is ok.
this is the php script :
<?php
$serverName = "127.0.0.1/instancename";
$connectionInfo = array( "Database"=>"Database", "UID"=>"sa", "PWD"=>"12345678");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connexion établie.<br />";
}else{
echo "La connexion n'a pu être établie.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Manny thanks for your help
i want to send email using SMTP protocol Via server.my server is host gator.
enter image description here
You need configure user and pass in configs
specify your problem better
thanks... i done it with same code...
just change the port then its working....
here is code
function do_email($msg=NULL, $sub=NULL, $to=NULL, $from=NULL){
$this->load->library('email');
$config = array();
$config['protocol']='smtp';
$config['smtp_host']='localhost';
$config['smtp_port']='587';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from($from, $system_name);
$this->email->to($to);
$this->email->subject($sub);
$this->email->message($msg);
$email = $this->email->send();
}
So I made a custom artisan command that will dump an sql file from my database.
Here's what is inside the command.
I named it db:dump with this syntax:
protected $signature = 'db:dump';
Next is the command itself:
public function handle()
{
$ds = DIRECTORY_SEPARATOR;
$host = env('DB_HOST');
$username = env('DB_USERNAME');
$password = env('DB_PASSWORD');
$database = env('DB_DATABASE');
$ts = time();
$path = database_path() . $ds . 'backups' . $ds . date('Y', $ts) . $ds . date('m', $ts) . $ds . date('d', $ts) . $ds;
$file = date('Y-m-d-His', $ts) . '-dump-' . $database . '.sql';
$command = sprintf('mysqldump -h %s -u %s -p\'%s\' %s > %s', $host, $username, $password, $database, $path . $file);
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
exec($command);
}
So all of the info needed are inside my .env file. If it would help, I'll also provide what's inside it:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:3WuXKyS70VX+V/Ic5QjuVcmFbzsqTkJehiQA7q9s9gk=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_salesandinventory
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
With that info, everything's ready and hopefully it could have worked.
But whenever I execute the command in my command prompt:
php artisan db:dump
It always says "Access is denied."
I lost any lead here, I don't know what could be the problem.
EDIT: When I replace the command with just $this->info('TEST'); it will work. It's like a console.log command or some sort. What I wanted to say is that there's something wrong in my command that restricts me from doing it.
From the comments, you describe that the command your script is generating and trying to execute is (as seen when you dd($command) instead of trying to exec($command)):
mysqldump -h 127.0.0.1 -u root -p'' db_salesandinventory > <output-file>
where <output-file> is in your home directory so presumably writable. You also mentioned that running that command manually fails with the same permissions error. So the problem has nothing to do with Laravel, but your access to your MySQL server.
First of all test if your username and password are correct. Try, on your console:
mysql -u root -p db_salesandinventory
(without -h). You will be prompted for a password, enter the one you are using in your script. If it does not work, you are using the wrong u/p, fix those and try your script again.
If it does work, it means your root user is not allowed to connect to 127.0.0.1. For MySQL, localhost is not the same as 127.0.0.1, and you'll need to enable access for your root user using that IP, as described in this question.
I managed to find the problem and did some alternative ways to prevent it.
Unfortunately, I cannot access my C:\Users\PCNAME\ and my project is saved there. I'm trying to dump the database inside the project path but it can't be accessed.
So as an alternative, I changed the path of where it will be dumped, I set it to C:\salesandinventory\backups\
Here's the whole code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MySqlDump extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'db:dump';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Runs the mysqldump utility using info from .env';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$ds = DIRECTORY_SEPARATOR;
$host = env('DB_HOST');
$username = env('DB_USERNAME');
$password = env('DB_PASSWORD');
$database = env('DB_DATABASE');
$mysqlpath = 'C:\xampp\mysql\bin\mysqldump';
$ts = time();
// $path = 'database' . $ds . 'backups' . $ds . date('Y', $ts) . $ds . date('m', $ts) . $ds . date('d', $ts) . $ds;
$path = 'C:\salesandinventory\Backups' . $ds . date('Y', $ts) . $ds . date('m', $ts) . $ds . date('d', $ts) . $ds;
$file = date('Y-m-d-His', $ts) . '-dump-' . $database . '.sql';
$command = sprintf($mysqlpath . ' --user=' . $username . ' --password=' . $password . ' --host=' . $host . ' ' . $database . ' > ' . $path . $file);
if (!is_dir($path))
{
mkdir($path, 0755, true);
}
exec($command);
}
}
The bottomline is, there's a problem with my folders with it being not accessible. I still don't know why, I tried opening command promt as an Administrator. But it still doesn't work.
I have problem when sending email from my server to somebody account
i mean that i need to send email from
myemail#mydomain.com to any another account Whether Live, hotmail or gmail etc.
of course that from PHP script
I have been created email account from my CPanel, and i tried to send email by this code.
$config['protocol'] = 'smtp';
$config['smtp_host'] = "mail.mydomain.com";
$config['smtp_user'] = "myemail#mydomain.com";
$config['smtp_pass'] = "password";
$config['smtp_port'] = "25";
$this->load->library('email',$config);
$this->email->from("myemail#mydomain.com", 'test');
$this->email->to("actualemail#live.com"); // the user email
$this->email->subject("hello");
$this->email->message("test test test");
if (!$this->email->send()) {
echo "error";
//$this->email->print_debugger();
exit;
}
//$this->email->print_debugger();
//exit;
echo "success";
Note that i'm using codeigniter framework.
The strange is there is no any problem in sending email , i get success message, but when i go to my receive email account i not found
any email. what to do, I'm boring form a lot of attempts :( .
try this:
$to = 'you#gmail.com';
$subject = 'my subject';
$msg = 'Hi..........';
$headers = "From: info#domain.com\r\nReply-To: info#domain.com";
$mail_sent = #mail( $to, $subject, $msg, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
It directly takes details from your server & sends mail.
This works fine on my site.
I have been following this documentation here:
https://www.mailjet.com/docs/code/php/codeigniter
In my config folder I created a php file as shown above like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://in.mailjet.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'API key';
$config['smtp_pass'] = 'Pass Key';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "\r\n";
?>
But I am still getting the following error:
An Error Was Encountered
Non-existent class: Email
When I try to load the lib like this in a controller:
$this->load->library('email');
$this->email->from('your_sender#address.com', 'You');
$this->email->to('recipient#example.com');
$this->email->subject('My first email by Mailjet');
$this->email->message('Hello from Mailjet & CodeIgniter !');
$this->email->send();
Cheers.
You haven't got any file Email in System directory