phpmailer and gmail

Posted on 2012-08-28
Last Modified: 2012-09-01
require_once 'PHPMailer_5.2.1/class.phpmailer.php';

$mail = new PHPMailer(true);

try {
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "localhost"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "";  // GMAIL username
$mail->Password   = "password";            // GMAIL password
  $mail->From = $mail->Username;
  $mail->FromName = "from";

  $mail->Body = 'body';

  echo "Mail was sent.</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!

echo '<br>end of php';

Open in new window

phpmailer script works on localhost windows desktop

but for shared server linux (,

SMTP -> ERROR: Failed to connect to server: Connection refused (111)
SMTP Error: Could not connect to SMTP host.

and on a dedicated windows host
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (12625808)
SMTP Error: Could not connect to SMTP host.
Question by:rgb192
    LVL 25

    Accepted Solution

    by:, have their own email (SMTP) servers ... why not use them?  (I suspect locking you into using theirs is part of their revenue model).
    LVL 36

    Expert Comment

    by:Loganathan Natarajan
    $mail->Host       = "localhost"; // SMTP server

    are you sure the above "localhost" has configured SMTP server? You should ask your hosting people whether you can use this address to send /receive emails?

    You must check with them whether they've enabled SMTP to send/receive mails
    LVL 36

    Assisted Solution

    by:Loganathan Natarajan
    Check whether this help? Help
    LVL 107

    Assisted Solution

    by:Ray Paseur
    Shared hosting companies often disable this functionality because they have found that it is frequently used by spammers.  GoDaddy and 1and1 have been two of the most notorious hosts for spammers.  Now that spamming is illegal and the law can reach the hosting company, these hosting companies are at legal risk if they leave holes open for spammers.  

    You might want to consider dedicated hosting with RackSpace or some of the offerings from SliceHost.

    Author Closing Comment

    I think I will need to use dedicated hosting for phpmailer

    or just use constant contact or mail()

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now