Request help with smtp

ICHaps

New member
Hello.

May I please request assistance with regards sending mail via smtp?

I've written so far, for a basic test:-
<?php
$from = "noreply@happyappi.com";
$to = "test@outlook.com";
$subject = "Test email using PHP SMTP";
$body = "This is a test email message";
$host = "server.eukhost.co.uk";
$port = "587";
$username = "noreply@happyappi.com";
$password = "wcooC~~*tyU";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent....!!</p>");
}
?>

However I can't get it to work. The site log seems to report there's any error with the line I've coloured above.

Could someone please spot where I'm going wrong, or should I just change hosts, if everything looks ok?

Thank You.
 
There seems an issue with the instantiation of the Mail class. You haven't included the necessary PEAR Mail package and its related files in script.
You can check out below update code for your script,
Code:
<?php
require_once "Mail.php"; // Include the PEAR Mail package

$from = "noreply@happyappi.com";
$to = "test@outlook.com";
$subject = "Test email using PHP SMTP";
$body = "This is a test email message";
$host = "server.eukhost.co.uk";
$port = "587";
$username = "noreply@happyappi.com";
$password = "wcooC~~*tyU";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);

$smtp = Mail::factory('smtp', array (
    'host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password
));

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

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

Just make sure to install PEAR Mail package on your server.
 
you are trying to send an email using PHP's PEAR Mail package and SMTP. However, there are a few issues in your code.
Firstly, you need to make sure that the Mail package is properly included in your script. You can include it like this:


PHP:
require_once "Mail.php";

Make sure you have the Mail package installed or include it via Composer.
Secondly, you are missing the creation of the $headers array. You should use the mail::send method with the headers, not directly passing the headers to the send method. Update your code like this:


PHP:
<?php
require_once "Mail.php";


$from = "noreply@happyappi.com";
$to = "test@outlook.com";
$subject = "Test email using PHP SMTP";
$body = "This is a test email message";
$host = "server.eukhost.co.uk";
$port = "587";
$username = "noreply@happyappi.com";
$password = "wcooC~~*tyU";


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


$smtp = Mail::factory('smtp', array (
    'host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password
));


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


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

Make sure to adjust the host, port, username, and password according to your SMTP server configuration. Also, ensure that the PEAR Mail package is properly installed or included in your project.
 
Last edited:
Hello.

Thank you for your replies..
Just a brief update I've moved happyappi.com to another host, and it appears to be working fine.
 
Back
Top