How to send email with Zend FrameWork 2 and Hostgator - email

I've got a code I made myself for sending email using Zend Framework2 via gmail SMTP, my code works flawlessly in local host and in some free hosting sites but when running from Hostgator I get this:
File: vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php:225
Message: Connection refused
This is my Code:
use Zend\Mail\Transport\SmtpOptions;
use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp;
$options = new SmtpOptions(array(
'name' => 'Gmail.com',
'host' => 'smtp.gmail.com',
'port'=> 587,
'connection_class' => 'login',
'connection_config' => array(
'username' => 'user#gmail.com',
'password' => 'password',
'ssl'=> 'tls',
),
));
$message = new Message();
$message->addTo('him#gmail.com')
->addFrom('user#gmail.com')
->setEncoding("UTF-8")
->setSubject('test')
->setBody('test');
$transport = new Smtp();
$transport->setOptions($options);
$transport->send($message);

I contacted Hostgator support and they solved my problem, the thing is hostgator servers does not allow smtp relay, so you just have to use an email account that exists within the hostgator server (one created for your domain I suggest). And thats all the problem was about :)

Related

Mail Exception only on server

Stage
I have a web application which I deployed recently. Application needs to send emails for several reasons.
I am using yandex business email service for my domain, have many email accounts for my domain, and I can use those email addresses without any problem.
I have written some code to send email on my Laravel application, using Laravel's Mail::send. See it below at #2. And you can see my config/mail.php settings at #1 below.
I am testing my application on my windows computer, with WAMP server.
My production server is VPS server running Linux. PHP version 5.5.29.
Problem
Everything works perfectly on my local test environment;
I can send mails, and they are delivered to hotmail, gmail or my domain's emails without any problem.
On the server however, I receive an error when I try to send an email. See #3 for error.
On the same server, I am able to send emails using PHPMailer with same email accounts, email settings and credentials, and without any problem.
Question and thoughts
Everything works on my local environment as expected and that makes me think it is a problem with my server configurations, PHP version, configuration or extensions, but I do not have the knowledge to judge that.
What have I tried and did not worked
Commented these lines to send email from default mail address.
And also changed my default email address with another one, which is also working everywhere except on production server of this project;
//$transport = Mail::getSwiftMailer()->getTransport();
//$transport->setUsername($username);
//$transport->setPassword($password);
Disabled mail encryption. Set;
'encryption' => '',
Tried different driver;
'driver' => 'mail',
1) My config/mail.php
'driver' => 'smtp',
'host' => 'smtp.yandex.com',
'port' => 587,
'from' => ['address' => 'name#domain.com', 'name' => 'name'],
'encryption' => 'tls',
'username' => 'name#domain.com',
'password' => 'password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
2) Code I use
Mail::send($message, $data, function ($message) use ($account, $to, $subject)
{
$acc = config("mail.accounts.$account");
$senderAddress = array_get($acc, 'address');
$username = array_get($acc, 'username', $senderAddress);
$password = array_get($acc, 'password', '');
$senderName = array_get($acc, 'name', $acc['address'], $senderAddress);
$replyToAddress = array_get($acc, 'replyTo.0', $senderAddress);
$replyToName = array_get($acc, 'replyTo.1', $senderName);
$message->from($senderAddress, $senderName)
->replyTo($replyToAddress, $replyToName)
->subject($subject)
->to($to);
$transport = Mail::getSwiftMailer()->getTransport();
$transport->setUsername($username);
$transport->setPassword($password);
});
Explanation for code above:
This code basically allows me to send emails to any email account,
and from many email accounts I have. And like I said, this works perfectly on my local test environment.
3) The Error
Swift_TransportException in AuthHandler.php line 181:
Failed to authenticate on SMTP server with username
"name#domain.com" using 2 possible authenticators
4) Working PHPMailer Script
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.yandex.com";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = $sender; // SMTP account username
$mail->Password = $password; // SMTP account password
$mail->SetFrom($sender, 'Name');
$mail->AddReplyTo($sender, 'Name');
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, $to);
$mail->Send();
It's hard to say what the problem can be. Or why it works on your development environment. But a quick google search gave me the following settings for smpt.yandex.com:
mail server address — smtp.yandex.com;
connection security — SSL;
port — 465.
So, it shouldn't work on your dev environment. Why it does work is a mystery to me.
see: https://yandex.com/support/mail/mail-clients.xml

Cake Email Times Out During Send

I'm trying to use CakeEmail in a web application, but I keep running into a timeout error. All my Googling and Stacking only give me the idea that something is not configured correctly, but I can't seem to find what config option I'm missing or filling incorrectly. I'm trying to send using my Gmail account.
Gmail Config:
public $gmail = array(
'host' => 'ssl://66.249.93.111',
'port' => 465,
'timeout' => 30,
'username' => 'my_gmail_account_name',
'password' => 'my_gmail_account_password',
'transport' => 'Smtp'
);
in app/Config/email.php
Email code:
$Email = new CakeEmail('gmail');
$Email->from(array('my_gmail_account_name' => 'Dev'));
$Email->to('my_gmail_account_name');
$Email->subject('Export Email Test');
$Email->send('This is a test email for ExportJobs.');
(As an additional note, the code that runs here is as part of a Cake Console program, so these methods are called when I run Console/cake file_name from the command line; also, that IP is the Gmail SMTP IP. When I try using the name, I get some DNS issue).
Does anyone happen to see what I'm missing?
Thanks for your time!
I found the problem I was having; it's a pretty silly error.
I completely forgot that to use the gmail domain for SMTP, I have to preface the domain name as "smtp.gmail.com". Once I did that it used SMTP and worked just fine.

Zend_Mail Could not open socket

i'm using Zend_Mail to send out an email from Aruba PEC.
$config = array('auth'=>'login',
'ssl' => 'ssl',
'port' => 465,
'username'=>'user#pec.it',
'password'=>'pass');
$tr = new Zend_Mail_Transport_Smtp('62.149.152.91',$config);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$mail->setBodyHtml('Mail test');
$mail->setFrom('user#pec.it');
$mail->addTo('marco#email.it');
$mail->setSubject('Test');
$mail->send();
but i receive this error: " Could not open socket ".
Now if i try to exceute this script on my local pc it works correctly, but wen i try to exceute it on server in my local area network i receive this error.
than i i try with another ip address (no certifaction account) it works correctly.
(so i think that is a problem of configuration server??)
thanks
You need to have OpenSSL installed on the PC. Is your server Windows? It is a bit of a pain if you are.

I do i setup sendmail in php.ini on wampserver 2.1 to post form data on local machine ?

I have created some forms using zendframework on my local machine that send the form content via email.
I would like to test the functionality locally and have read some posts regarding configuring the php.ini file to do this but not sure which is the correct method ?
can anyone help me with this, many thanks
On Windows you will have to use SMTP to send the message. There is a drop in fake sendmail for Windows but it still requires an SMTP server.
You could use your ISP's sendmail server if they offer one, or you can set one up on the local machine. 1, 2, 3, 4
Since you are using Zend Framework, you can alternatively use Zend_Mail to send through an SMTP server (Zend_Mail can also use sendmail, but since it isn't configured, you can't use that transport). In that case see Sending via SMTP, SMTP Authentication, and Securing SMTP Transport.
Here is some sample code for sending an SMTP message with AUTH and TLS security.
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('ssl' => 'tls',
'port' => '465', // 25 if no ssl
'auth' => 'login',
'username' => 'user',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.example.com', $config);
$mail = new Zend_Mail();
$mail->addTo('user#domain')
->setSubject('Mail Test')
->setBodyText("Hello,\nThis is a Zend Mail message...\n")
->setFrom('sender#domain');
try {
$mail->send($transport);
echo "Message sent!<br />\n";
} catch (Exception $ex) {
echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}
Also note, your ISP may not require you to auth at all if you are sending from one of their IP addresses, but you probably do have to authenticate with your username and password, in which case you will want to use TLS.

How can I send mail to Gmail with an attachment, in Perl?

How can I send mail to Gmail using Perl? Here's what I'm trying:
my $mailer = Email::Send->new(
{
mailer => 'SMTP::TLS',
mailer_args => [
Host => 'smtp.gmail.com',
Port => 587,
User => 'xxx',
Password => 'xxx',
]
}
);
use Email::Simple::Creator; # or other Email::
use File::Slurp;
#arrIrc = read_file("$ircFile");
my $email = Email::Simple->create(
header => [
From => 'xxx',
To => "$configList{email}",
Subject => "The summary of logfile $channelName",
],
body => "#arrIrc",
);
Use Net::SMTP::SSL to talk to GMail.
See MIME::Lite inline images on Perlmonks for an example.
If you want to send mail to Gmail, you do the same thing you would do to send mail anywhere. If you want to send mail through Gmail, there is the Email::Send::Gmail module. Merely typing your question in Google led me to Sending Mail Through Gmail with Perl by Mark Sanborn.
You can use MIME::Lite to compose a message, which you then send to your local sendmail process. However, in order to talk to gmail's servers you need to have SSL certificates set up. There's probably more detailed instructions for that on superuser.