SMTPMail-Talk

Summary: Talk page for SMTPMail.
Maintainer: Petko
Users: (View? / Edit)

This space is for User-contributed commentary and notes. Please include your name and a date along with your comment.

Is it possible to use curl function and options from php ?

CarlosAB February 19, 2018, at 12:11 AM

Unfortunately those are limited and many SMTP features are missing. --Petko February 19, 2018, at 01:07 AM

I have found this in the curl forum, from 2011:

curl_setopt($sess, CURLOPT_URL, "https://$host:$port");
curl_setopt($sess, CURLOPT_CONNECT_ONLY, true) # option to just connect to server and send nothing
curl_setopt($sess, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($sess, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($sess, CURLOPT_CAINFO, "cacert.pem"); // local root CA copy, has to be absolute path
curl_setopt($sess, CURLOPT_CAPATH, "./");
curl_setopt($sess, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($sess, CURLOPT_HTTPHEADER, "");

$out = "AUTH LOGIN\r\n";
$out .= base64_encode($user) . "\r\n";
$out .= base64_encode($pass) . "\r\n";
$out .= "MAIL FROM: <$from>\r\n";
$out .= "RCPT TO: <$isto>\r\n";
$out .= "DATA\r\n";
$out .= "To: $isto\r\n";
$out .= "From: $from\r\n";
$out .= "Subject: $subj\r\n\r\n";
$out .= "$mesg";
$out .= "\r\n.\r\n";
$out .= "QUIT\r\n";

curl_setopt($sess, CURLOPT_CUSTOMREQUEST, $out . "\r\n");
curl_exec($sess); 
$resp = curl_exec($sess);
$info = curl_getinfo($sess);

curl_close($sess);

echo $resp;
print_r($info);

I have not tested yet but sounds promising.

I prefer just the php only features approach and not relying on any shell, for those out there that like me use shared hosts and cpanel and no access to any shell utils.

CarlosAB February 19, 2018, at 01:55 PM

Indeed this looks promising, I'll try it. BTW I use this recipe on a shared hosting plan (OVH). --Petko February 19, 2018, at 07:58 PM

Lucky you sir, I don't have any access to /bin or /usr/bin, as I am jailed at my home dir, but an account there is dirty cheap and support is quite responsive. You don't have to test this snippet, I was not trying to add more to your stack which must be respectful, but if you feel freely and by yourself inclined to do so, it will surely lessen my guilt. Is that the case? I always do unit testing using cli php, as it is faster to do it all on a shell:


#! /usr/bin/php
<?php

error_reporting('E_ALL ^ E_NOTICE');

# while true;do vi script.php;./script.php;done

$mailopt= array(
  'host' => 'smtp.gmail.com',
  'port' => '675',
  'user' => 'some@account.com',
  'pass' => 'password',
  'from' => 'me name',
  'isto' => 'another@account.com',
  'subj' => 'Just a test ',
  'mesg' => '1, 2 ,3 testing !',
);

function curlmail($arr){

foreach($arr as $k => $v) $$k = $v;

curl_setopt($sess, CURLOPT_URL, "https://$host:$port"); 
curl_setopt($sess, CURLOPT_CONNECT_ONLY, true); # option to just connect to server and send nothing
curl_setopt($sess, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($sess, CURLOPT_SSL_VERIFYHOST, false);
#curl_setopt($sess, CURLOPT_CAINFO, "cacert.pem"); # local root CA copy, has to be absolute path
#curl_setopt($sess, CURLOPT_CAPATH, "./");
curl_setopt($sess, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($sess, CURLOPT_HTTPHEADER, "");

$out = "AUTH LOGIN\r\n";
$out .= base64_encode($user) . "\r\n";
$out .= base64_encode($pass) . "\r\n";
$out .= "MAIL FROM: <$from>\r\n";
$out .= "RCPT TO: <$isto>\r\n";
$out .= "DATA\r\n";
$out .= "To: $isto\r\n";
$out .= "From: $from\r\n";
$out .= "Subject: $subj\r\n\r\n";
$out .= "$mesg";
$out .= "\r\n.\r\n";
$out .= "QUIT\r\n";

curl_setopt($sess, CURLOPT_CUSTOMREQUEST, $out . "\r\n");
curl_exec($sess); 

$resp = curl_exec($sess);
$info = curl_getinfo($sess);

curl_close($sess);

print_r($info);
echo $resp;

}

curlmail($mailopt);

?>

I still have not tested it as I have to poke some holes in the firewall.

I also use a php cli skel, to turn some of those units into shell programs and I improve it over time.

CarlosAB February 20, 2018, at 11:16 AM

Talk page for the SMTPMail recipe (users?).