Sending email via PHP code not working

Discussion in 'Email' started by OliverFreeman, Aug 8, 2012.

  1. Hi all. I've copied the suggested php email code from the support section of Winhost, however I still can't get it to work. Do you need to further specify the SMTP or is that already in the code? The code:


    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?PHP
    require_once "Mail.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.isgaust.net.au";
    $username = <[email protected]>";
    $password = "xxx";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    ?>
    </body>
    </html>
     
    Last edited by a moderator: Oct 14, 2015
  2. Elshadriel

    Elshadriel Winhost Staff

    Are you getting an error message?
     
  3. Hi. If I publish it as HTML or ASPX it comes up as below

    $from, 'To' => $to, 'subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); ?>

    which tells me there is an error in the code but I can't find it for the life of me.

    If I publish it as .php, it says it cannot display the page.
     
  4. The username/password doesn't have to reflect the postmaster account, does it? It can be any email account you've got set up?
     
  5. So I've managed to reduce the faulty code to

    $host, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); ?>

    now by changing the code to this:

    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?php
    require_once "Mail.php";
    $from = "[email protected]";
    $to = "[email protected]";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.isgaust.net.au";
    $username = "[email protected]";
    $password = "testaccount";
    $headers['From'] = "[email protected]";
    $headers['To'] = "[email protected]";
    $headers['subject'] = "email test";
    $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' =>

    $username, 'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    ?>
    </body>
    </html>

    So it looks like it didn't like the alternative way of casting the first array. Now I've got to work out how to re-write the second array so it works.
     
  6. Elshadriel

    Elshadriel Winhost Staff

    You should be able to authenticate with any valid account, but you can try the postmaster first.
     
  7. My problem starts out identical to that of Oliver Freeman, and I don't have the luxury of working through it as he did. Who can I hire to fix it?
     
  8. Are you still having a problem? I've (finally) got something working.
     
  9. I am new to PHP and I cannot send an email from code. Any help would be appreciated.

    Thanks
    Joe
     
  10. are you still having any issues? what errors are you getting?
     
  11. Hi Joe,

    Don't use the variables in the $smtp = Mail:: line. Put the host, username and password directly in the line. This worked for me.

    $smtp = Mail::factory('smtp', array ('host' => 'mail.DOMAIN', 'auth' => true, 'username' => 'postmaster@DOMAIN', 'password' => 'XXXX'));

    Kind regards,

    Ferry
     

Share This Page