Issue using my Gmail account to send email in Laravel 9 - php

I am trying to use my Gmail account to send emails in a Laravel 9 project. I have a custom domain and have setup a custom gmail workspace account. Using that account, I generated an app password, and used that to send emails on my local serve, and it works. Here are the settings in the .env for the mail server:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=example#silkweb.ca
MAIL_PASSWORD="***********"
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=example#silkweb.ca
MAIL_REPLYTO_ADDRESS=xyz#gmail.com
MAIL_FROM_NAME="${APP_NAME}"
I am able to send emails using the above configuration on the local server on my Mac. However, when I use exactly same settings on the production server (my domain silkweb.ca), I get an error message:
Symfony\Component\Mailer\Exception\TransportException PHP 8.0.26. 9.24.0
Expected response code "250/251/252" but got code "550", with message "550 5.7.1 Relaying denied".
Here are the details about the request sent by PHP
curl "https://silkweb.ca/password/email" \
-X POST \
-H 'cookie: XSRF-
TOKEN=eyJpdiI6IlNKbExabVZGZFdVMFRDL3ZBdFZnWUE9......' \
-H 'accept-language: en-US,en;q=0.9' \
-H 'accept-encoding: gzip, deflate, br' \
-H 'referer: https://silkweb.ca/password/reset' \
-H 'sec-fetch-dest: document' \
-H 'sec-fetch-user: ?1' \
-H 'sec-fetch-mode: navigate' \
-H 'sec-fetch-site: same-origin' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,.....\
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36' \
-H 'content-type: application/x-www-form-urlencoded' \
-H 'origin: https://silkweb.ca' \
-H 'upgrade-insecure-requests: 1' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"' \
-H 'cache-control: max-age=0' \
-H 'content-length: 74' \
-H 'connection: keep-alive' \
-H 'host: silkweb.ca' \
-F '_token=WQnq9y7JklvDiU5hqORymYvorevDOgOReoPPB2uc' -F 'email=rktaxali#gmail.com'
Any suggestion, how to fix this issue?

I am not sure what was the issue, but smtp.gmail.com has started working. I cleared cache and congig cache (php artisan cache:clear and php artisan congig:clear). I also changed the encryption to tls and port to 587. I also removed quotes (") from the passwrod. Then, first I used my personal gmail account with smtp.gmail.com and it worked. Later I tried my custom gmail account (from my domain silkweb.ca) and that I also worked. The following settings are working for me:
# Google SMTP configuration
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_ENCRYPTION=tls
MAIL_PORT=587
MAIL_USERNAME=example#silkweb.ca
MAIL_PASSWORD=**********
MAIL_FROM_ADDRESS=example#silkweb.ca
When I was searching for answers for my issue on the net, I came across an article Google: https://support.google.com/a/answer/176600?hl=en
As per this article, the recommended method to send emails from a gmail account is smtp-relay.gmail.com.

Related

Map php web socket server inside container to the host

I have a docker-compose.yml file that looks like below
version: "3.9"
services:
php-env:
build: .
volumes:
# This is where php files outside will be located inside the container
- ./src:/var/www/html
ports:
- 9000:80
# mysql_db is also the server name when entering username and password to login to php my admin
mysql_db:
image: mysql:latest
# NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
# (this is just an example, not intended to be a production configuration)
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
# Mysql root password change below
MYSQL_ROOT_PASSWORD: root
# MYSQL_DATABASE: myDb
# MYSQL_USER: user
# mysql_db container name above is also the server name when entering username and password to login to php my admin
# default username is root
phpmyadmin:
image: phpmyadmin:latest
restart: always
ports:
# This is the local host port example: http://localhost:9001/
- 9001:80
environment:
- PMA_ARBITRARY=1
Below is my Docker file
FROM php:8.0-apache
# Simple libmaridb install
# RUN apt-get update -y && apt-get install -y libmariadb-dev
RUN apt-get update && \
apt-get install \
#libzip is for installing packages
libzip-dev \
wget \
grep \
curl \
git \
unzip \
libmariadb-dev \
-y --no-install-recommends
#Install php sockets
RUN docker-php-ext-install sockets
# Install mysqli
# RUN docker-php-ext-install mysqli
# Install pdo mysql
RUN docker-php-ext-install zip pdo_mysql
# copy composer dependency manager installer shell script
COPY ./install-composer.sh ./
# Copy php.ini file
COPY ./php.ini /usr/local/etc/php
# Composer, Git.... will all be installed in php-env container
# Clean up packages and install composer
RUN apt-get purge -y g++ \
&& apt-get autoremove -y \
&& rm -r /var/lib/apt/lists/* \
&& rm -rf /tmp/* \
&& sh ./install-composer.sh \
&& rm ./install-composer.sh
# Change the current working directory
# WORKDIR /var/www/
WORKDIR /var/www/html
What i am trying to create is a web socket server and a client where the websocket server writes the time and the client displays the time
Below is my websocket.php server file
<?php
$address = '0.0.0.0';
$port = 12345;
// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
$client = socket_accept($server);
// Send WebSocket handshake headers.
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
'H*',
sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($client, $headers, strlen($headers));
// Send messages into WebSocket in a loop.
while (true) {
sleep(1);
$content = 'Now: ' . time();
$response = chr(129) . chr(strlen($content)) . $content;
socket_write($client, $response);
}
?>
Below is my wesocket.html client file
<html>
<body>
<div id="root"></div>
<script>
var host = 'ws://0.0.0.0:12345/websocket.php';
var socket = new WebSocket(host);
socket.onmessage = function(e) {
document.getElementById('root').innerHTML = e.data;
};
</script>
</body>
</html>
Below is how i started my php server where i access the container via docker exec then i start the server
Below is how i am accessing my container
docker exec -it dockercomposefiles-php-env-1 bash
Below is how i am starting my server
php -q websocket.php
Below is how i am starting my client server
php -S 0.0.0.0:8000 websocket.html
Normally if i am using Xamp i should be able to access the client via http://0.0.0.0:8000/ but if i try to that in a browser its not working because its in the container and not in the host system how can i be able to Map this from the container to the host system so that i can be able to access via the host's system browser

How can i get "HTTP_AUTHORIZATION" key in $_SERVER variable? [duplicate]

This question already has answers here:
Apache 2.4 + PHP-FPM and Authorization headers
(4 answers)
Closed 11 months ago.
I have defined the rule into .htaccess like this:
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
And I have passed "Authorization Bearer Token" into HTTP request like below:
:authority: demo.com
:method: POST
:path: /data-list
:scheme: https
accept: application/json, text/plain, */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,hi-IN;q=0.8,hi;q=0.7,gu-IN;q=0.6,gu;q=0.5
authorization: Bearer IiwiZGVzdCI6Imh0dHBzOlwvXC93Yy1rdW5hbGcubXlzaG9waWZ5LmNvbSIsImF1ZCI6ImI4NzMyYTZkNjcyMGFiNjNlN2IwZTRkNDExNzVhNTZlIiwic3ViIjoiNDQ1MDIxMjI2MjkiLCJleHAiOjE2NDg0NDEwODYsIm5iZiI6MTY0ODQ0MTAyNiwiaWF0IjoxNjQ4NDQxMDI2LCJqdGkiOiIyMjZiM2
content-length: 22
content-type: application/x-www-form-urlencoded
dnt: 1
origin: https://demo.com
referer: https://demo.com/data?hmac=18dc09298e1bd09d95c02ada793f57140804bf42380be07&host=d2Mta3VuYWxnLm15c2lmeS5jb20vYWRtaW4&locale=en-IN&session=55bfc54daf6945e3ca50b2da7f5830d88dcfcb8f4d103b97518166d9fd7b00c9&shop=demo.com&timestamp=1648441021
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
But, using PHP variable $_SERVER or any other PHP variables I am not getting "authorization: Bearer Token".
I am using a digital ocean server for this.
I have tried PHP variables like $_SERVER or any other PHP variables that get "authorization: Bearer Token".
I am getting blank array response for printing $_SERVER['HTTP_AUTHORIZATION'].
Is there any information that I am missing?
Works fine for me with this code in .htaccess :
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
I tried to reproduce the issue but didn't get any error. Try to make the same request.
HTTP-Request
POST /data-list
Host: demo.com
authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9XXXXXXX
In PHP
function getToken(){
$bearerToken = null;
if (isset($_SERVER['AUTHORIZATION'])) {
$bearerToken = $_SERVER['AUTHORIZATION'];
}elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
$bearerToken = $_SERVER["HTTP_AUTHORIZATION"];
}elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
$bearerToken = $_SERVER["REDIRECT_HTTP_AUTHORIZATION"];
}
return $bearerToken;
}
echo getToken(); // This returned a Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9XXXXXXX

Docker, Nginx, localhost does not respond

I hope you're all doing well during this particular time.
I'm stuck on a problem concerning my nginx+php image, it's my first image I'm building from scratch (with Google), so I'm not sure everything is correct.
This image will serve me in multiple home projects, I'm using this image in a Gitlab Container Registry.
I'm using this Dockerfile :
FROM alpine:latest
COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker
COPY --from=docker/compose:latest /usr/local/bin/docker-compose /usr/bin/docker-compose
RUN adduser -S www-data -u 1000
RUN apk upgrade -U
RUN apk add --update --no-cache \
git \
bash \
vim
RUN apk --update --no-cache add php7 php7-fpm php7-mysqli php7-json php7-openssl php7-curl \
php7-zlib php7-xml php7-phar php7-intl php7-dom php7-xmlreader php7-ctype php7-session \
php7-mbstring php7-gd nginx curl
RUN apk add --update --no-cache nginx supervisor
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative \
&& composer clear-cache
ENV PATH="${PATH}:/root/.composer/vendor/bin"
COPY config/nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p var/cache var/log var/sessions \
&& chown -R www-data var
VOLUME /srv/api/var
VOLUME /var/www/html
VOLUME /var/www/src
COPY index.php /var/www/html/
COPY src/run.sh /run.sh
RUN chmod u+rwx /run.sh
EXPOSE 80
ENTRYPOINT [ "/run.sh" ]
CMD ["init"]
Which calls this bash script, for Symfony migration:
#!/bin/bash
INIT=false
MIGRATION=false
CREATEDB=true
for DOCKER_OPTION in $#
do
case "$DOCKER_OPTION" in
init )
INIT=true
shift
;;
migration )
MIGRATION=true
shift
;;
with-existing-db )
CREATEDB=false
shift
;;
* )
break
;;
esac
done
APP_ENV=${APP_ENV:-dev}
init_project () {
composer install --prefer-dist --no-progress --no-suggest --no-interaction
chmod +x bin/console && sync
php bin/console assets:install
if $CREATEDB
then
php bin/console doctrine:schema:update -f
php bin/console doctrine:fixtures:load -n
fi
}
if $MIGRATION;
then
php bin/console doctrine:migrations:migrate --no-interaction -vvv
chown -R www-data:www-data /srv/api/var/
fi
if $INIT;
then
mkdir -p /var/nginx/client_body_temp
chown www-data:www-data /var/nginx/client_body_temp
mkdir -p /var/run/php/
chown www-data:www-data /var/run/php/
touch /var/log/php-fpm.log
chown www-data:www-data /var/log/php-fpm.log
if [ "$APP_ENV" != 'prod' ];
then
init_project
fi
exec supervisord --nodaemon --configuration="/etc/supervisord.conf" --loglevel=info
fi
exec "$#";
Launched by this Docker-compose:
version: "3.7"
services:
lamp:
build:
context: .
dockerfile: ./Dockerfile
ports:
- 80:80
volumes:
- .:/var/www/html/
- ./src:/var/www/src/
With this Nginx configuration:
user www-data;
worker_processes 4;
error_log /dev/stdout;
pid /tmp/nginx.pid;
events {
worker_connections 1024;
}
http {
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
image/svg+xml svg;
}
client_body_temp_path /tmp/client_body;
fastcgi_temp_path /tmp/fastcgi_temp;
proxy_temp_path /tmp/proxy_temp;
scgi_temp_path /tmp/scgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
# mime types
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
access_log /dev/stdout;
error_log /dev/stdout;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
Everything seems to be going well.
Unfortunately I can't display my index.php (php_info).
When I do
curl -vvv http://localhost:80
* Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
And When I do
curl -vvv http://localhost:80 --trace-ascii dump.txt
== sync, corrected by elderman ==
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 73 bytes (0x49)
0000: GET / HTTP/1.1
0010: Host: localhost
0021: User-Agent: curl/7.69.1
003a: Accept: */*
0047:
== Info: Empty reply from server
== Info: Connection #0 to host localhost left intact
I've searched the net, but my skills are quite limited in the configuration of nginx.
Do you have any ideas to submit to me.
thank you for your reply,
For VIM, he's already add here :
RUN apk add --update --no-cache \
git \
bash \
vim
So i try with this Dockerfile, whitout PHP/Composer (It's pretty minimalist?) :
FROM alpine:latest
COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker
COPY --from=docker/compose:latest /usr/local/bin/docker-compose /usr/bin/docker-compose
RUN adduser -S www-data -u 1000
RUN apk upgrade -U
RUN apk add --update --no-cache \
git \
bash \
vim
RUN apk --update --no-cache add nginx curl
RUN apk add --update --no-cache nginx supervisor
COPY config/nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p var/cache var/log var/sessions \
&& chown -R www-data var
VOLUME /srv/api/var
VOLUME /var/www/html
VOLUME /var/www/src
COPY index.php /var/www/html/
COPY src/run.sh /run.sh
RUN chmod u+rwx /run.sh
EXPOSE 80
ENTRYPOINT [ "/run.sh" ]
CMD ["init"]
For
curl -vv http://localhost:80
* Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection 0
And for
curl -vv http://localhost:80 --trace-ascii dump.txt
== Info: Trying ::1:80...
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 73 bytes (0x49)
0000: GET / HTTP/1.1
0010: Host: localhost
0021: User-Agent: curl/7.69.1
003a: Accept: */*
0047:
== Info: Recv failure: Connection reset by peer
== Info: Closing connection 0
What do you think ?

Accessing Raw POST via php://input for forms with Content Type multipart/form-data not working

We have a proxy PHP script and access POST form data via
$postPayload = file_get_contents('php://input');
which usually works.
(The reason to not use $_POST is that we sometimes have duplicate form input names which PHP suppresses)
Now we have a form with
<form name="form" method="post"
action="/script.php" enctype="multipart/form-data">
In this case file_get_contents('php://input'); returns an empty string.
It can be reproduced with
curl 'http://localhost/script.php' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost/script.php?commandCode=NO_AUTH_REGIST_OPEN_USER&lang=de' -H 'Origin: http://misumi-europe.com.orange.imi.local' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryk3IVneARm3kqJ0fs' --data-binary $'------WebKitFormBoundaryk3IVneARm3kqJ0fs\r\nContent-Disposition: form-data; name="commandCode"\r\n\r\nNO_AUTH_NEXT\r\n------WebKitFormBoundaryk3IVneARm3kqJ0fs\r\n' --compressed
How can I access the RAW POST data in this case?
I was able to fix it like this in .htaccess
<Files "script.php">
# make post data always available in the proxy
php_flag enable_post_data_reading 0
</Files>
multipart/form-data is not send to php://input, only to $_POST.
In php.ini you can set enable-post-data-reading=off to change this but $_POST will always be empty. See http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading
You might set it for single pages using .htaccess
php_value enable-post-data-reading off
There is also an apache hack:
<Location "/backend/XXX.php">
SetEnvIf Content-Type ^(multipart/form-data)(.*) NEW_CONTENT_TYPE=multipart/form-data-alternate$2 OLD_CONTENT_TYPE=$1$2
RequestHeader set Content-Type %{NEW_CONTENT_TYPE}e env=NEW_CONTENT_TYPE
</Location>
See also: Get raw post data

codeigniter Dynamic baseurl to access app via lan break css fontfamily

Problem was i wanted to access my codeigniter app from other devices on the wifi network, i noticed all css breaks; so i edited my base_url to
config.php
$config['base_url']= "http://".gethostbyname($_SERVER['SERVER_NAME'])."/ehit";
and my .htaccess is as
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
now everything loads correctly when i open my app from other devices like
http://192.168.1.61/ehit except for fontsAwsome.
html source
<!-- Core CSS -->
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/ehit/assets/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/ehit/assets/css/googlefont.css">
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/ehit/assets/css/font-awesome.css">
all loads correctly by fontfamily is not defined. inside font-awesome.css i find
#font-face {
font-family: 'FontAwesome';
src: url('../fonts/fontawesome-webfont.eot');
src: url('../fonts/fontawesome-webfont.eot') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff') format('woff'), url('../fonts/fontawesome-webfont.ttf') format('truetype'), url('../fonts/fontawesome-webfont.svg') format('svg');
font-weight: normal;
font-style: normal;
}
so may be the url('.. is the problem; is there a way to fix it ?
i debug and find that the url is correctly still font-family FontAwesome is not there
curl 'http://127.0.0.1/ehit/assets/fonts/fontawesome-webfont.woff' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.5' -H 'Cache-Control: max-age=0' -H 'Connection: keep-alive' -H 'Host: 127.0.0.1' -H 'Origin: http://localhost' -H 'Referer: http://127.0.0.1/ehit/assets/css/font-awesome.css' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'
so where is the problem ?
EDIT
Problem solved when i changed $config to
//$config['base_url']= "http://".gethostbyname($_SERVER['SERVER_NAME'])."/ehit"; //old
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
so what was the problem ?
To application/config/config.php Change localhost to your IP Address
Visit all site on your IP Address
Example:
$config['base_url'] = 'http://192.168.xx.xx/codeigniter-project/';
PC 192.168.xx.xx/codeigniter-project<br>
Mobile 192.168.xx.xx/codeigniter-project<br>
tablet 192.168.xx.xx/codeigniter-project<br>
Good luck.
You need to allow .woff files in .htaccess:
RewriteCond $1 !^(index\.php|assets|woff|eot|img|css|js|robots\.txt|favicon\.ico)
This could be a possible cause.

Categories