Send Email From PHP Using SMTP Authentication

Discussion in 'Site & Server Administration' started by redinlove, Feb 5, 2008.

  1. #1
    This is useful article. In this article I am showing how to send email from PHP using SMTP Authentication for WHM/cPanel Servers.

    Everybody knows that normal php mail functions sending emails as a nobody and this is cause to spam emails. Now check my amazing solution

    Login to your root SSH and copy/use following command line to your SSH line.

    pear install Mail;pear install Net_SMTP;pear install Auth_SASL;



    After above command, you can now use the following php script and send real emails.


    <?php
    require_once "Mail.php";
    $from = "MyName <name@myemail.com>";
    $to = "TargetName <name@targetemail.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";
    $host = "mail.mailserver.com";
    $username = "name@myemail.com";
    $password = "my_password";
    $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);
    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>
     
    redinlove, Feb 5, 2008 IP
  2. mad-I.T.

    mad-I.T. Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hi,

    i made this php script in my website:

    <?php
    require_once "Mail.php";
    $from = "JM <jmd@subtlestorm.com>";
    $to = "jaaan <jaaan_44@yahoo.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";
    $host = "mail.energoenergydrink.com";
    $username = "admin@energoenergydrink.com";
    $password = "adminenergo";
    $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);
    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>

    here's the link of the script above:

    http://www.energoenergydrink.com/contact3.php


    but then the following errors are displayed:

    Warning: main(Mail.php) [function.main]: failed to open stream: No such file or directory in /home/energo/public_html/contact3.php on line 66

    Fatal error: main() [function.require]: Failed opening required 'Mail.php' (include_path='/usr/lib/php:.:/usr/php4/lib/php:/usr/local/php4/lib/php') in /home/energo/public_html/contact3.php on line 66



    can you help me figure out what's wrong?
     
    mad-I.T., Jun 4, 2008 IP
  3. xous

    xous Active Member

    Messages:
    173
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Hi,

    It appears that you either do not have PEAR mail package installed or it is not in your include paths.


    Login to your root SSH and copy/use following command line to your SSH line.

    pear install Mail;pear install Net_SMTP;pear install Auth_SASL;
     
    xous, Jun 4, 2008 IP
  4. chandan123

    chandan123 Prominent Member

    Messages:
    11,586
    Likes Received:
    578
    Best Answers:
    0
    Trophy Points:
    360
    #4
    have u done this

     
    chandan123, Jun 4, 2008 IP
  5. mad-I.T.

    mad-I.T. Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have here screenshots of my cpanel:

    FIRST-IMAGE=http://www.energoenergydrink.com/cpanel-01.jpg

    SECOND-IMAGE=http://www.energoenergydrink.com/cpanel-02.jpg

    as you can see, i think PEAR mail package has been installed on the server. oh btw, actually im supposed to use mail () function of php but then the technical support of the hosting company told me that they don't support php functions and advised me to use SMTP authentication instead. =)
     
    mad-I.T., Jun 5, 2008 IP
  6. khongwir

    khongwir Peon

    Messages:
    266
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It still dosen't work. How can i find include paths?
     
    khongwir, Mar 14, 2010 IP
  7. Xironet

    Xironet Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,
    I am very new to PHP. I have got a very simple HTML form as below:

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="en-gb" http-equiv="Content-Language" />
    <meta content="text/html; charset=windows-1252" http-equiv="Content-Type" />
    <title>Contact Page</title>
    </head>

    <body>

    <form name="feedbackform" action="form-mailer2.php" method="post">
    &nbsp;<label id="Label1">Name: <input name="Name" tabindex="1" type="text" /><br />
    E-Mail: <input name="Email" tabindex="2" type="text" /><br />
    Comment:
    <textarea cols="20" name="Comment" style="height: 98px" tabindex="3"></textarea><br />
    <br />
    <input name="Submit" tabindex="4" type="submit" value="submit" />
    <input name="Reset" style="width: 54px" tabindex="5" type="reset" value="reset" /></label><br />
    </form>

    </body>

    </html>

    and I have a form handler as below:

    <?php
    require_once "Mail.php";

    //below the text between qoutes are exactly the same for FROM and for TO
    $from = "Contact<name@mydomain.com>";
    $to = "MyName<name@mydomain.com>";
    $subject = "Form Submission";
    $body = "Hi,\n\nThis is just a test message to find out if web forms work with SMTP authentication.";

    //below is the SMTP for my server, the username is actually my email address which is the same as my username, and the password is the actual password for the email.
    $host = "mail.*****.com";
    $username = "****@*****.com";
    $password = "********";

    $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);

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

    The form does NOT work.
    The HTML file is saved as contact.html.
    The php file is saved as form-mailer2.php.

    My server is in fact Windows 2008 running PHP5 on it. The server requires SMTP authentication. I can not get the form working and when I fill the form clicking submit, I get error (HTTP500 Internal server error) The website can not display the page.

    I do not understand why this simple form doesn't work. Is there anyone out there who can help me please?

    I look forward to hearing from you very soon.

    many thanks.

    Hamid
     
    Last edited: Jan 14, 2013
    Xironet, Jan 14, 2013 IP
  8. sumeet.jain.71

    sumeet.jain.71 Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    Hi
    New in php.Getting this errro please help me.I am using windows hosting..
    Fatal error: Class 'Mail' not found in C:\inetpub\vhosts\lionsclubdgnfuture.org\httpdocs\mail.php on line 13
     
    sumeet.jain.71, Jan 31, 2013 IP