Welcome Guest, Not a member yet? Register   Sign In
Email smtp: mail not send - no error
#1

I'm trying to send an e-mail from my website using the build in e-mail class and smtp. When using the mail protocol, the mail is send without incident. With smtp the mail is not send but printDebugger() doesnt show any debug information. I'm at a loss. I even went as far as simply echoing the ourput from ->send(), it seems to be empty (both with echo and print_r).

At first I used the smtp server I use to config my e-mail client. But that gave an extreme smtp timeout (even setting the SMTPTimeout vaslue to 180 didnt solve that. So I asked my host and they adviced using localhost with the settings I added below. I rather not ask them about the problem I'm facing now unless I'm certain there isn't a problem with my code. If they find a problem with my code I get charged for the support. If it's server related the support is free.

Here is the function I am writing to send the e-mail. Any insight you might have would be greatly appreciated. If you need more information, don't hesitate to ask!

PHP Code:
function sendMail($from = array(), $to = array(), $message = array(), $type 'html'$attachments false)
    {
        $config['mailType'] = $type;
        $config['protocol'] = 'smtp';
        $config['SMTPHost'] = 'localhost';
        $config['SMTPUser'] = '********@m*******ossteam.nl';
        $config['SMTPPass'] = '********';
        $config['SMTPPort'] = 587;
        $config['SMTPTimeout'] = 180;
        //$config['SMTPCrypto'] = 'ssl';
            
        $email 
= \Config\Services::email();
        $email->initialize($config);
        $email->setFrom($from['email'], $from['name']);
        $email->setTo($to['email'], $to['name']);
        $email->setSubject($message['subject']);
        $email->setMessage($message['content']);        
        
if ($email->send(false))
        {
            $email->clear();
            return true;
        }
        else
        {
            $email->printDebugger();
            exit;
        }
    
Reply
#2

Let me write here the codes which work perfectly for me.

1. Upload your PHPMailer folder into your root folder, beside your App, public etc folders

2. Go to Config / Autoload, find your lines starting with public $psr4 =  (the line might be 42) :

PHP Code:
public $psr4 = [
        
APP_NAMESPACE => APPPATH// For custom app namespace
        
'Config'      => APPPATH 'Config',
        
'App' => APPPATH// To ensure filters, etc still found

        
'Forms' => ROOTPATH.'forms',
        
'PHPMailer\\PHPMailer'=> ROOTPATH.'PHPMailer/src' 
    
]; 

3. On your Controller, include this just before your class ControllerName extends BaseController {...

PHP Code:
//mailer

use PHPMailer\PHPMailer\PHPMailer;
use 
PHPMailer\PHPMailer\OAuth;
use 
PHPMailer\PHPMailer\Exception;
use 
PHPMailer\PHPMailer\SMTP

4. mailing starts

PHP Code:
//mailing starts

$member_email $this->request->getVar('member_email');
$name $this->request->getVar('member_name');
$my_email '[email protected]'

$mail = new PHPMailer();

$mail->isSMTP();
$mail->Timeout 20;
$mail->SMTPDebug 0;
//Set the hostname of the mail server
$mail->Host 'this might be your server name. Check your mail settings';
$mail->Port 587;
$mail->SMTPSecure 'tls';
$mail->SMTPAuth true;
$mail->Username "[email protected]";  
$mail
->Password "Password";  
$mail
->setFrom('[email protected]''Welcome to my website ');
$mail->addAddress($member_email'');

//Set the subject line
        
$message 'Thanks for registering. You can login on our 
<a href="http://mywebsite.com/my_folder/login" target="_blank">Login</a> page.<br><br>Regards,<br>My Website.com'
;
  
$name_greeting 
'Dear ' .$name;

$mail->isHTML(true);
    $mail->Subject 'My Web Site - Welcome.';
    $mail->Body    "$name_greeting,<br /><br />$message";
    $mail->CharSet 'utf-8';

$mail->send();

    
$session = \Config\Services::session();
    
$session->setFlashdata('success''Thanks for registering. You wil receive an email etc.');
    return 
redirect()->to(site_url('en/login'));

//mailing ends 
Reply
#3

Thanks demyr, for your reply. Is there any way of doing this without using a third party library? The way I understand the documentation is that the build in Email class of Codeigniter 4 should be able to do this. And again, when using the build in class but not use smtp it words without a glitch.
Reply
#4

(05-09-2021, 04:58 AM)BFlokstra Wrote: Thanks demyr, for your reply. Is there any way of doing this without using a third party library? The way I understand the documentation is that the build in Email class of Codeigniter 4 should be able to do this. And again, when using the build in class but not use smtp it words without a glitch.

You're welcome. Well, If I'm not wrong I haven't tried sending email without third party or even if I did someday, I do not remember. Because PHPMailer is the best one for PHP, you know, and it has been working really well. Plus, if you need anything, you can find the solution around easily, for example attaching a document or sending to BCC etc. Plus, when I posted a question on stackoverflow, the creator answered ! He helped me. He is always around. The codes I shared with you have been used in all projects of mine for any customer. Also, I sometimes implement an email form within my control panel, attach a pdf or a qr code etc.. And my customers have no problem. It works really well.

And lastly, I do not have enough experience to answer your question about sending email without a third party library. Sorry for this.
Reply
#5

That's okay demyr. I'll keep looking. I rather not install a third party library if I don't have to for two simple reasons.
I found that (when not sending via smtp) the build in Email class of CI4 is much simpler than PHPMailer (in the past I used PHPMailer and later SwiftMailer). And the documentation seems to indicate that it should be possible with the build in Email class. So why install an third party library then?

Second reason is that webspace is at a premium right now. Sure, PHPMailer doesnt take up a lot of space. But every little bit helps. We are a small non profit foundation that was established this february. So funds are low the first year. We hope to get our own server within the coming two years and be able to host our own website. Right now we have to make use of shared hosting with limited storage. And since we will be using a lot of e-mail we just have to make smart use of the webspace.

But if I can't get smtp to work with the build in Email class of CI4, I probably will be looking into using PHPMailer (or SwiftMailer, which I am more familiar with).

I must say that adding an attachment to the mail I send with the build in class was really easy. All I had to do was add this line:
$this->_email->attach($filename);

But thanks for your insight! I'll keep it in mind if I can't get smtp to work. But I'm starting to think it might actually be a server issue instead of a code issue. Our website was migrated to a different shared server last week. On the old server smtp worked fine.
Reply
#6

Then I would advise you to check the host name of your server for mails. The servername in the url might be the answer for you.
Reply
#7

that’s what I did originally but I got extreme timeouts. (Way over 180 seconds). The webhost advised to use localhost as the smtp server host name but then I get no errors but the mail isn’t send. It doesn’t show up in the servers mail log either.
Reply
#8

Try to set

$config['SMTPCrypto'] = 'tls';

Port 587 usually works with 'tls' whereas port 465 works with 'ssl'
Dirk B.
Abatrans Software
No SEO spam - see forum guidelines
Reply




Theme © iAndrew 2016 - Forum software by © MyBB