Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sending SMTP e-mail with curl/php
<?php
function read_cb($ch, $fp, $length) {
return fread($fp, $length);
}
$fp = fopen('php://memory', 'r+');
$string = "From: <no-reply@example.com>\r\n";
$string .= "To: <hdogan@example.com>\r\n";
$string .= "Date: " . date('r') . "\r\n";
$string .= "Subject: Test\r\n";
$string .= "\r\n";
$string .= "Deneme mesaji\r\n";
$string .= "\r\n";
fwrite($fp, $string);
rewind($fp);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'smtps://smtp.example.com:465/example.com',
CURLOPT_MAIL_FROM => '<no-reply@example.com>',
CURLOPT_MAIL_RCPT => ['<hdogan@example.com>'],
CURLOPT_USERNAME => 'username',
CURLOPT_PASSWORD => 'password',
CURLOPT_USE_SSL => CURLUSESSL_ALL,
CURLOPT_READFUNCTION => 'read_cb',
CURLOPT_INFILE => $fp,
CURLOPT_UPLOAD => true,
CURLOPT_VERBOSE => true,
]);
$x = curl_exec($ch);
if ($x === false) {
echo curl_errno($ch) . ' = ' . curl_strerror(curl_errno($ch)) . PHP_EOL;
}
curl_close($ch);
fclose($fp);
@FALL1N1
Copy link

FALL1N1 commented Nov 18, 2021

Works like a charm, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment