Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 938071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:34:54+00:00 2026-05-15T21:34:54+00:00

I have a PHP mail script that successfully sends emails to everything BUT GMail

  • 0

I have a PHP mail script that successfully sends emails to everything BUT GMail addresses, so now I’m trying to create one with PEAR’s Mail and Mail_Mime packages that can send to GMail. I’ve gotten the impression that this is only possible if I connect through GMail’s SMTP server to send the messages. Upon trying a simple test script, I get the following error:

unable to set sender to
[username@gmail.com]

There is nothing wrong with the address, and this site suggests that if there’s nothing incorrectly formatted about the address, then it’s a server connectivity issue. But how do I troubleshoot a connectivity problem in this situation? Or is there something else I’m doing wrong? Or is there another, easier way to get a PHP script to successfully send mail to GMail?

My code is as follows (email address and password changed, obviously)

$from = "username@gmail.com";
$to = "username@gmail.com";
$subject = "Test";
$crlf = "\n";
$text = 'Text message';
$html = '<html><body>HTML message</body></html>';
$headers = array (
    'From' => $from,
    'Return-Path' => $from,
    'Subject' => $subject
);

$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);

$smtp = Mail::factory(
    'smtp',
    array (
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'auth' => true,
        'username' => "username@gmail.com",
        'password' => "password",
        'debug' => true
    )
);

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

if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    echo 'FAILURE';
} else {
    echo 'SUCCESS';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:34:55+00:00Added an answer on May 15, 2026 at 9:34 pm

    I can’t imagine why a simple username@gmail.com address wouldn’t work, so it’s probably another error masquerading as that. Make sure you’re not accidentally suppressing error messages (put a call to error_reporting(E_ALL) before you call send).

    One thing to keep in mind, though, is that you do not need to use GMail’s SMTP server to send mail to GMail addresses. You can use one provided with your hosting to the same effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.