Thursday, December 21, 2017

php - sending mail/email

I've found it tough to reliably send mail from PHP, and the command line in general.

It feels like gmail (my usual testing recipient) really hates old school ways of sending email via scripts - like to the extent stuff doesn't even wind up in the recipient's SPAM box, it just goes away.

I'll still look for a better solution but right now the most reliable thing I've found is


<?php
// Pear Mail Library                                                                                                                                                             
require_once "Mail.php";

$from = '<kirkjerk@gmail.com>';
$to = '<kirkjerk@gmail.com>';
$subject = 'Hi testing mail!';
$body = "Hi,\n\nHow are you, how are thigns going?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'kirkjerk@gmail.com',
        'password' => 'MYPASSWORD'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

?>
As far as I can tell, much of the header stuff (like trying to munge the From) is ignored- plus you have to have your password in plain text in a script which stinks.  I'll try to update this entry if I refine this technique, like getting reply-tos working etc.

No comments:

Post a Comment