I am trying to access gmail via php running under wampserver 2.4. For simplicity, this is from a localhost (the actual application is much more complex).
Following some standard examples, I am able to both receive (via pop or imap) and send (via smtp) emails through gmail -- but ONLY if the "Less secure apps" feature of gmail is enabled
(https://www.google.com/settings/security/lesssecureapps). Of course, one must first enable pop and/or imap on the gmail settings page
(https://mail.google.com/mail/u/0/#settings/fwdandpop)
While this works, I would like php to work with a "more secure technology" -- i.e. SSL and TLS. Any ideas?
The following code is used:
for retrieving email.
$ato="{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX";
$auser="myaddress#gmail.com";
$apwd="mypwd";
$mbox = imap_open($ato,$auser,$apwd,NULL,1) or die("can't connect: " . print_r(imap_errors())); // just do one login
$stuff=imap_check($mbox); // get and print basic information (such as # of emails)
var_dump($stuff);
and for sending email (using the PEAR package)
Note that for the following to work, I had to first enable the php_openssl php setting (using the php - phpSettings option under the wampserver popup menu).
require_once "Mail.php";
$from = 'myemail#gmail.com';
$to = 'someone#foo.org ';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'myemail#gmail.com',
'password' => 'mypwd'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
Based on various postings, I have tried enabling ssl_module and imagemap_module under apache extensions; and the php_sockets under php settings. They made no difference. I have read that configuring php with imap-ssl can matter, but I have no idea how to do that.
Here is the proper response I get when using the pop example above (using my username and password) -- when gmail's "access for less secure apps" is enabled:
object(stdClass)[1]
public 'Date' => string 'Thu, 22 Jan 2015 01:06:37 -0500 (Eastern Standard Time)' (length=55)
public 'Driver' => string 'pop3' (length=4)
public 'Mailbox' => string '{gmail-pop.l.google.com:995/pop3/notls/ssl/novalidate-cert/user="myemial#gmail.com"}INBOX' (length=99)
public 'Nmsgs' => int 153
public 'Recent' => int 1
and when I Disable "less secure apps"
Array ( [0] => [AUTH] Web login required: https://support.google.com/mail/bin/answer.py?answer=78754 [1] => Too many login failures ) can't connect: 1
BTW: using IMAP
$ato="{imap.gmail.com:993/imap/imap/ssl}INBOX";
yields:
Array ( [0] => Can't open mailbox {imap.gmail.com:993/imap/imap/ssl}INBOX: invalid remote specification ) can't connect: 1
Gmail considers only the OAUTH2 login method to be secure. See the following article for details: https://developers.google.com/gmail/xoauth2_protocol
Related
I am using AWS SES on a website with shared hosting platform (not on AWS)
When I send emails using the SMTP it works perfectly. When I attempt to call and use the API, no email is sent, no error message is received.
There are two reasons I want to use the API instead of SMTP
1) According to Amazon, the API is faster.
2) One of my transactional emails is a reminder to clients to log in, and/or pay their account. SMTP cannot send email to more than 50 (if I am not mistaken). For that reason, I need to call the SendRawEmail.
Please help me with my code to send the email and to implement SendRawEmail
Here is my code:
require ('PHPMailer/PHPMailerAutoload.php');
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
// Create an SesClient. Change the value of the region parameter if you're
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'eu-central-1'
]);
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'info#mydomain.com';
// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
$recipient_emails = ['info#example.com'];
// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
//$configuration_set = 'ConfigSet';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'.
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">'.
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'.
'AWS SDK for PHP</a>.</p>';
$char_set = 'UTF-8';
try {
$result = $SesClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails,
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body,
],
'Text' => [
'Charset' => $char_set,
'Data' => $plaintext_body,
],
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
],
],
// If you aren't using a configuration set, comment or delete the
// following line
'ConfigurationSetName' => $configuration_set,
]);
$messageId = $result['MessageId'];
echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
echo "\n";
}
?>
Check your error log. I had the same issue:
require 'AWS/aws-autoloader.php';
For other SDK uses (such as API calls) you may need to create a .aws folder to include credentials
When I try to run this code, I get a strange error I cannot find an answer to:
// Pear Mail Library
require_once "Mail.php";
$from = 'someone#gmail.com';
$to = 'someoneelse#gmail.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'someone#gmail.com',
'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 error is:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]
I looked for an answer but could not find it anywhere. Please help.
i have the same problem and mine stopped working suddenly. My suspicion is the SSL certificates associated with my name server, or in your case your name server are expired. My hosting site has incompetent help as they have been at it for two weeks and still can't figure it out. So check the name server SSL certificates.
The unknown error code of -1 can be confusing but when it shows up on Pear Mail, then most likely you need to install Mail and Mail_Mime
pear install Mail Mail_Mime
SMTP errors, if any, will always be specifically reported. If this also happens, then install Net_SMTP also.
I am a complete noob in AWS. I configured AWS SES today and now I am able to send an email to a recipient using this code.
<?php
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
define('SENDER', 'sender email');
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
define('RECIPIENT', 'recipient email');
// Replace smtp_username with your Amazon SES SMTP user name.
define('USERNAME','my username');
// Replace smtp_password with your Amazon SES SMTP password.
define('PASSWORD','my password');
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
define('HOST', 'email-smtp.us-west-2.amazonaws.com');
// The port you will connect to on the Amazon SES SMTP endpoint.
define('PORT', '587');
// Other message information
define('SUBJECT','Hello from Driffle!');
define('BODY','Test Email');
require_once 'Mail.php';
$headers = array (
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
$smtpParams = array (
'host' => HOST,
'port' => PORT,
'auth' => true,
'username' => USERNAME,
'password' => PASSWORD
);
// Create an SMTP client.
$mail = Mail::factory('smtp', $smtpParams);
// Send the email.
$result = $mail->send(RECIPIENT, $headers, BODY);
if (PEAR::isError($result)) {
echo("Email not sent. " .$result->getMessage() ."\n");
} else {
echo("Email sent!"."\n");
}
?>
But when I try to send an html formatted email. The output email returns plain text. Looking for a solution to send html emails through Amazon SES.
Found it. I was doing it a bit wrong, but this works...
Add these headers:
'Mime-Version' => '1.0',
'Content-Type' => 'text/html; charset="ISO-8859-1"',
Then submit the body as HTML markup (keep it simple as possible to begin with - it does not need to be a "page" necessarily).
bh
added content-type in $headers . It worked for me
require_once 'Mail.php';
$headers = array (
'Content-Type' => "text/html; charset=UTF-8", // <- add this line
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
I've been creating this project, where the system accepts registration and then send the information thru mail to the owner. I'm using PEAR: Mail Mime to do this. Sending of mail is actually working on my local computer, but after uploading this project to a hosting site (GoDaddy), it's not sending anymore.
I did an error check to see what hinders the sending of mail, and I received this message:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm using a GMail account to send email messages, and use this code to do it:
/* INCLUDE PREREQUISITES IN ORDER TO SEND AN EMAIL */
include('../../application/third_party/Mail-1.3.0/Mail-1.3.0/Mail.php');
require_once '../../application/third_party/Mail_Mime-1.10.0/Mail_Mime-1.10.0/Mail/mime.php';
/* HEADER INFORMATION */
$from = '<***#gmail.com>';
$to = '<***#gmail.com>';
$subject = 'Someone registers';
$crlf = "\r\n";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
// CREATING THE MIME MESSAGE
$mime = new Mail_mime($crlf);
$html = '
<html>
<body>
Message here
</body>
</html>';
// SETTING THE BODY OF THE EMAIL TO HTML
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
// SENDER INFORMATION
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$errormessage = '<p>'.$mail->getMessage().'</p>'; // ERROR MESSAGE
$mail->getUserInfo();
}
Yes, I turned on the Access for less secure apps of the GMail sender account.
And yes, the error displaying is turned on.
ini_set('display_errors', 1);
error_reporting(E_ALL);
Any solutions that I might have missed?
Found an answer regarding the outgoing mail in this - Sending email through gmail SMTP on GoDaddy.
It is because of the hosting provider, GoDaddy, which restricts the SSL port of GMail (465), and other ports such as 25 and 587. Thanks to this article -Troubleshooting PHPMailer Problems - of GitHub for clearing that up. Users are resorted to use the mail-server of GoDaddy instead.
But I still managed to send an email using GMail by doing:
$smtp = Mail::factory('smtp');
/* SETTINGS WILL BE IN DEFAULT (E.G. HOST:LOCALHOST, PORT: 45) */
instead of
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
But the receiver receives the sent email as "spam" - result will be that the receiver won't notice the incoming emails from the system (not a good choice). And there are no records in the Sent items of the sender (means you "used" the email to send an email, but not entirely).
So, we are back with the mail-server of GoDaddy. First, you have to determine the relay server of your hosting account by checking this What is the name of my hosting account's relay server?, which was published by GoDaddy themselves. In my case, Linux (cPanel), which I'll be using localhost as my host.
$smtp = Mail::factory('smtp', array(
'host' => 'localhost',
'auth' => true,
'username' => '***#***.com',
'password' => '***'
));
But the weird thing is, there are still no records of sent items. Hope a GoDaddy representative reads this.
I'm having a problem sending emails out through SMTP using Amazons SES. My code is relatively simple and looks like this:
require_once "Mail.php";
require_once 'Mail/mime.php';
$stmp_info= array (
'host' => 'ssl://email-smtp.us-east-1.amazonaws.com',
'port' => 465,
'auth' => true,
'username' => 'xxxxxx',
'password' => 'xxx'
);
$headers = array (
'From' => 'sender#example.com',
'To' => 'test#example.com',
'Subject' => 'Test Message'
);
$mime = new Mail_mime("\n");
$mime->setTXTBody('Hello World');
$mime->setHTMLBody('<p>Hello World</p>');
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', $stmp_info);
$mail = $smtp->send('test#example.com', $headers, $body);
The problem is I always get this error:
Failed to connect to email-smtp.us-east-1.amazonaws.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]
But this works for testing a connection:
[root#job-server-1 ~]# $ echo quit | nc -v email-smtp.us-east-1.amazonaws.com 25
-bash: $: command not found
Connection to email-smtp.us-east-1.amazonaws.com 25 port [tcp/smtp] succeeded!
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-376766033
Yet if I ping the address, it does not work. I've tried port 25, 465 and 587 for both tls and ssl. The weird part is it works on my local host and this error only occurs when I try to connect from my rackspace instance. Does anyoen have an idea of what can be causing this?
Verify your network setting with your instance. If you are using VPC , please do check your subnet and routing policies.
Its blocking to connect ses smtp from your instance.
Also fill Request to Remove Email Sending Limitations to remove some restrictions on port 25
hope it helps !!