Home > Blockchain >  PHP - How to set an email title with CURLOPT_PUT?
PHP - How to set an email title with CURLOPT_PUT?

Time:10-10

I am working on a cron job that implements email functionality. It essentially sends out email reports.

I am able to send out an email using CURLOPT_PUT as follows:

//create email body etc
$to = '[email protected]';
$server = 'smtps://smtp.gmail.com:465';
$message = 'test message';
$emailFile = fopen("php://temp", 'w ');
fwrite($emailFile, $message);
rewind($emailFile);
$fstat = fstat($emailFile);
$size = $fstat['size'];

//initialize and authenticate cURL
$ch = curl_init($server); 
curl_setopt($ch, CURLOPT_USERPWD, '[email protected]' . ':' . 'samplepass');
curl_setopt($ch, CURLOPT_MAIL_RCPT, array("<" . $to . ">"));
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $emailFile);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);

fclose($emailFile);
curl_close($ch);

The issue is that I am not able to set the title (a.k.a subject) of the email, it arrives as "No Subject". Is it possible to get this done using my current set up, and if so, how can I achieve that?

I am aware that CURLOPT_POST can achieve this, but that causes me other issues, and is not an option for me at the moment.

CodePudding user response:

I found a way to fix the issue. You can add a subject (title) to the email by pre-appending the second argument of the fwrite() function as follows:

$subject = "email title";
fwrite($emailFile, "Subject: " . $subject . "\n" . $message);

However, as others suggested, if you can, it is better to use more modern libraries like SwiftMailer or PHP Mailer to achieve this functionality since CURLOPT_PUT has been deprecated.

  • Related