Cake Email Times Out During Send - email

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.

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

How to send email with Zend FrameWork 2 and Hostgator

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

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.

How do I send email to my Gmail account using SMTP and Perl?

I don't want to use sendmail to send an email but would prefer to use SMTP. How can I use Perl to send an email to my GMAIL account?
personally I would suggest you to use my module Email::Send::SMTP::TLS
which works pretty well through the TLS of Google Mail.
Thanks.
use Email::Send;
my $mailer = Email::Send->new( {
mailer => 'SMTP::TLS',
mailer_args => [
Host => 'smtp.gmail.com',
Port => 587,
User => 'username#gmail.com',
Password => 'password',
Hello => 'fayland.org',
]
} );
use Email::Simple::Creator; # or other Email::
my $email = Email::Simple->create(
header => [
From => 'username#gmail.com',
To => 'to#mail.com',
Subject => 'Subject title',
],
body => 'Content.',
);
eval { $mailer->send($email) };
die "Error sending email: $#" if $#;
As per the comment, it's not clear if you want to send email via Google's SMTP, or just send email in general (perhaps to your gmail account). You should check out Email::Send and possibly Email::Send::Gmail.
Alternatively, if what you're really asking is how do I move email from somewhere that isn't Gmail to Gmail, I've had very good luck with IMAP using Mail::Box and the Mail::Box::IMAP4::SSL backend. You can see an example of use here.
I've always used and had very good luck with Mail::Sender.
Another possibility you might want to look at is using the Email::Send::Gmail module from CPAN. This will allow you to send email from your Gmail account to any account (for example, to yourself)
There are muliple SMTP modules on CPAN, for example Net::ESMTP. Also, sendmail very probably does use SMTP to communicate with mail servers, so what's your real reason for not wanting to use it?
Email::Send (as used in Fayland Lam's answer) is deprecated:
Email::Send is going away... well, not really going away, but it's
being officially marked "out of favor."
This works for me, using the preferred Email::Sender:
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS ();
use Email::Simple ();
use Email::Simple::Creator ();
my $smtpserver = 'server';
my $smtpport = 587;
my $smtpuser = 'username';
my $smtppassword = 'password';
my $transport = Email::Sender::Transport::SMTPS->new({
host => $smtpserver,
port => $smtpport,
ssl => "starttls",
sasl_username => $smtpuser,
sasl_password => $smtppassword,
});
my $email = Email::Simple->create(
header => [
To => 'mymail#gmail.com',
From => 'sender#example.com',
Subject => 'Hi!',
],
body => "This is my message\n",
);
sendmail($email, { transport => $transport });
If you aren't familiar with CPAN (Comprehensive Perl Archive Network) I recommend you to bookmark that site. It contains third party (mostly well tested) libraries.
An example showing how to send emails using perl: http://www.perlfect.com/articles/sendmail.shtml
I happen to use MIME::Lite, which is a wrapper around Net::SMTP to simplify the process of building email objects, file attachments, and sending the payload.
If you're not familiar with installing modules, check:
On Windows, use the ActiveState Perl Package Manager (in start menu)
On Unix, use CPAN: $ sudo cpan Module::Name
On hosted Unix accounts: How can I install a CPAN module into a local directory?
If you just don't like sendmail, another option is to use Postfix, another MTA.
Here are the instructions I followed to get it setup on my machine, using gmail:
http://souptonuts.sourceforge.net/postfix_tutorial.html
This might be useful too, if you get a warning about failing to verify a certificate from Thawte Premium Server CA.
http://ubuntuforums.org/archive/index.php/t-894355.html