PHP SMTP Not working with Amazon SES - php

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 !!

Related

PHP pear-mail smtp not working

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.

How to send html email using Amazon SES in PHP

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

PEAR: Mail Mime - Can't send email after uploading to hosting site - GoDaddy

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.

Using imap_pop under php on wamp server 2.4

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

cakephp 2.0 smtp email

I am trying to send a email message using CakePhp 2.0. in my controller i use this code (i know it's fine, i took it from the cookbook) :
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail("myConfig");
$email->from(array('from#example.com' => 'From Example'));
$email->to($to);
$email->subject($msgtitle);
$ok = $email->send($content);
and in app/config/email.php i have this config :
<?php
class EmailConfig {
public $myConfig = array(
'host' => 'mail.myServer.com',
'port' => 587,
'username' => 'mYaccount',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
the problem is the server answers with :
SMTP Error: 530 5.7.0 Must issue a STARTTLS command first.
the account name is correct, as is the password. The config works when loading it up in thunderbird, the connection to the smtp server is set up as :
server name : mail.myServer.com
port : 587
connection security : STARTTLS
authentication : normal password
user name : mYaccount
Are you certain your SMTP supports tls? Try sending the ehlo command:
telnet 1.2.3.4 25
ehlo testing
You should see something like:
250-STARTTLS
in the list.
If you see it, then it is most likely not enabled. You will need to enable it. If you do not see it, you will need to add it.
public $smtp = array(
.................,
'tls' => true
);
Below code is working for me over GoDaddy server using CakePHP SMTP Email:
Email.php file inside config folder - CakePHP 2.4 MVC version:
// for Live Server GoDaddy.com domain
public $smtp = array(
'transport' => 'Smtp',
'host' => 'ssl://smtpout.asia.secureserver.net', <-- important
'port' => 465, <-- important
#'timeout' => 30,
'username' => 'no-reply#godaddy-domain.com',
'password' => 'password',
#'tls' => false,
#'log' => false,
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
And here is the controller file code below:
// Controller Code to Send Actual Email
// email configuration
$Email = new CakeEmail('smtp');
$Email->from(array('no-reply#godaddy-domain.com' => 'App Name'))
->sender('no-reply#godaddy-domain.com', 'App Name')
->to(array($email))
->bcc(array('xyz#xyz.com'))
->subject('Test Email from GoDaddy')
->emailFormat('both')
->send($hash.'<br><strong>My</strong> message 45 قبل الميلاد، مما يجعله أكثر من');
Hope it helps !
Thanks
From the CakePHP Cookbook:
You can configure SSL SMTP servers, like GMail. To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. Example:
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
(...)
Give the following a try:
<?php
class EmailConfig {
public $myConfig = array(
'host' => 'ssl://mail.myServer.com',
'port' => 465,
'username' => 'mYaccount',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
Make sure your
php_openssl.dll
extension is running.
You can check it on thephp.ini file.
If you are using XAMPP php.ini should be on C:\xampp\php
php.ini:
;extension=php_oci8.dll ; Use with Oracle 10gR2 Instant Client
;extension=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client
extension=php_openssl.dll
;extension=php_pdo_firebird.dll

Categories