Skip to content

Commit

Permalink
Add a complete working EXAMPLE
Browse files Browse the repository at this point in the history
  • Loading branch information
hyper-prog committed Feb 18, 2021
1 parent 95d8920 commit bfff024
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
17 changes: 17 additions & 0 deletions EXAMPLES/complex_docker_compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Using of this example
=====================

Run the create_certs.bash to generate certificates for server and client

./create_certs.bash

Edit the data/server.json according to your settings

Start the container of restgomail

docker-compose up -d

Now you can send messages to the container with the php sample

php ./send_sample_request.php

19 changes: 19 additions & 0 deletions EXAMPLES/complex_docker_compose/create_certs.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Generate certificates for server and client

SERVERCERTNAME="restgomail"
CLIENTCERTNAME="rgmclient"

openssl genrsa -out ${SERVERCERTNAME}.key 2048
openssl ecparam -genkey -name secp384r1 -out ${SERVERCERTNAME}.key
openssl req -new -x509 -sha256 -key ${SERVERCERTNAME}.key -out ${SERVERCERTNAME}.crt -days 365

openssl genrsa -out ${CLIENTCERTNAME}.key 2048
openssl ecparam -genkey -name secp384r1 -out ${CLIENTCERTNAME}.key
openssl req -new -x509 -sha256 -key ${CLIENTCERTNAME}.key -out ${CLIENTCERTNAME}.crt -days 365

mv ${SERVERCERTNAME}.key ./data/
mv ${SERVERCERTNAME}.crt ./data/

cp ${CLIENTCERTNAME}.crt ./data/${CLIENTCERTNAME}.crt
17 changes: 17 additions & 0 deletions EXAMPLES/complex_docker_compose/data/server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"restgomail": {
"smtpHost": "smtp.gmail.com",
"smtpPort": "587",
"smtpAuthRequired": true,
"smtpAuthPassword": "gmailpasswordsample",
"smtpAllowedFromAddressOnly": "sampleuser@gmail.com",
"httpsListenPort": "44325",
"tlsKeyFile": "restgomail.key",
"tlsCertFile": "restgomail.crt",
"allowOnlyKnownCertificates": true,
"knownCertificates": {
"civisdbContainer": "@rgmclient.crt"
},
"waitSecondsAfterSmtpreq": 10
}
}
8 changes: 8 additions & 0 deletions EXAMPLES/complex_docker_compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
restgomail:
image: hyperprog/restgomail
container_name: restgomail
ports:
- "44325:44325"
volumes:
- ./data:/restgomail
53 changes: 53 additions & 0 deletions EXAMPLES/complex_docker_compose/send_sample_request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

$json =
'{
"sendmail": {
"from": "sampleuser@gmail.com",
"to": "target.address@mailbox.com",
"subject": "Test from PHP",
"bodyhtml": "This is received through <h1>RestGoMail</h1>"
}
}';

$r = SendHttpPostWithCert("https://127.0.0.1:44325/sendmail",$json,"rgmclient.crt","rgmclient.key");
print("Received:".$r);

function SendHttpPostWithCert($url, $postdata, $certfile, $keyfile)
{
$curl = curl_init();

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);

curl_setopt($curl, CURLOPT_SSLKEY, $keyfile);
curl_setopt($curl, CURLOPT_SSLCERT, $certfile);

curl_setopt($curl, CURLOPT_CERTINFO, true);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);
$info = curl_getinfo($curl,CURLINFO_CERTINFO);

print "You can check this certificate:"
print "\n=============================\n";
$c = $info[0]['Cert'];
$c = preg_replace('/\-+BEGIN CERTIFICATE\-+/','',$c);
$c = preg_replace('/\-+END CERTIFICATE\-+/','',$c);
$c = trim($c);
print $c;
print "\n=============================\n";

curl_close($curl);

return $result;
}

0 comments on commit bfff024

Please sign in to comment.