I can send email from PHP using the mail( ) function.

but sending using phpmailer fails. Why would that be?

the error message when I set Host to localHost is "relaying blocked". When
I specify the mail server name I get "smtp connect failed".


// here is the mail( ) function code that works:
<?php

$from_email = 'steve@xxxxxxx' ;
$to_email = 'lae0901@xxxxxxx' ;
$server = 'exchangeit.abc.com' ;
$subject = 'email sent using php mail function' ;

ini_set("SMTP", $server );
ini_set("sendmail_from", $from_email );

// The message
$message = "The mail message was sent with the following mail
setting:\r\nSMTP = " . $server .
"\r\nsmtp_port = 25\r\nsendmail_from = " . $from_email ;

$headers = "From: " . $from_email ;

// Send
mail( $to_email, $subject, $message, $headers);
echo "Check your email now....<BR>";
?>

but this similar code using phpmailer is failing.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '/usr/local/zendsvr6/share/php-mailer/src/Exception.php';
require '/usr/local/zendsvr6/share/php-mailer/src/PHPMailer.php';
require '/usr/local/zendsvr6/share/php-mailer/src/SMTP.php';

header("Content-type: text/html");

$from_email = 'steve@xxxxxxx' ;
$server = 'exchangeit.abc.com' ;

ini_set("SMTP", $server );
ini_set("sendmail_from", $from_email );

$from_name = 'Steve Richter' ;
$to_email = 'lae0901@xxxxxxxxx' ;
$bcc1 = '' ;
$subject = 'email sent using php mailer.' ;

$messageText =
'<p style="font-family:arial,helvetica;font-size:16px;">test
message</p>' ;

echo 'from email: ' . $from_email . '<br>' ;
echo 'to email: ' . $to_email . '<br>' ;
echo 'subject: ' . $subject . '<br>' ;

$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "localhost";
// $mail->Host = $server;
// $mail->SMTPAuth = false ;

//Set who the message is to be sent from
$mail->setFrom($from_email, $from_name);

//Set who the message is to be sent to
$mail->addAddress($to_email ) ;

if ( strlen($bcc1) > 0)
{
$mail->addBcc($bcc1);
}

//Set the subject line
$mail->Subject = $subject ;
$mail->Body = $messageText ;
$mail->IsHTML(true) ;

$mail->msgHTML($messageText, __DIR__);

//send the message, check for errors
$errorInfo = '' ;
if (!$mail->send())
{
$errorInfo = "Send error: " . $mail->ErrorInfo;
echo $errorInfo . '<br>' ;
}
else
{
echo "<br>email successfully sent to " . $to_email . '<br>' ;
}

?>

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.