$config = array('auth' => 'login',
'username' => '****#gmail.com',
'password' => '****',
'port' => '25',
'ssl' => 'tls');
$transport = new Zend_Mail_Transport_Smtp('smtp.googlemail.com', $config);
what should i do after that, where can i put the body and the recipient address.
Its described in the manual of Zendframework
Zend_Mail::setDefaultTransport($transport);
Then somewhere else instanciate Zend_Mail, write your mail and send it.
See the documentation for full examples (although the Zend docs admittedly often aren't great).
Based on a comment here:
$mail = new Zend_Mail();
$tr = new Zend_Mail_Transport_Smtp(...);
$mail->setFrom('...', 'Server');
$mail->addTo($to, '....');
$mail->setSubject($subject);
$mail->send();
Zend_Mail::setDefaultTransport($tr);
$mail->setBodyText($body);
Your example shows an empty link so it wont display anything.
Unless this is a modified example you used to post on here?
Does the following display anything when you run it, if not what do you get.
$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText($form->getValue('body'));
$mail->setBodyHtml('my link');
$mail->setFrom($certtime['email'], $certtime['first_name'] . $certtime['last_name']);
$mail->addTo($form->getValue('reciever'));
$mail->setSubject('My Certificate');
$mail->send($smtpHost);
Related
I am stuck, constantly getting this error from PHPMailer:
"SMTP: Could not Authenticate"
I am using XOAuth2 of PHPMailer. I have tried dump and die on PHPMailer object, it shows the details configured correctly. I have matched refresh token, clientId and clientSecret but they are fine too.
My code is like this:
$phpMailer->SMTPAuth = true;
$phpMailer->SMTPSecure = 'tls';
$phpMailer->SMTPAutoTLS = false;
$phpMailer->Host = 'smtp.gmail.com';
$phpMailer->Port = 587;
$phpMailer->AuthType = 'XOAUTH2';
$path = base_path().'/Modules/Emails/credentials.json';
$credentials = json_decode(file_get_contents($path), true)['web'];
$phpMailer->setOAuth(
new OAuth(
[
'provider' => new Google(
[
'clientId' => $credentials['client_id'],
'clientSecret' => $credentials['client_secret'],
]
),
'clientId' => $credentials['client_id'],
'clientSecret' => $credentials['client_secret'],
'refreshToken' => $payload->refreshToken,
'userName' => $payload->from_email,
]
)
);
$phpMailer->setFrom($payload->from_email, "Haider Wain");
$phpMailer->addAddress('haiderwayne.9#gmail.com');
$phpMailer->Subject = 'Test phpmailer';
$phpMailer->CharSet = 'utf-8';
$phpMailer->msgHTML('hello test');
$phpMailer->AltBody = 'This email is HTML; enable HTML mail if you cannot see it.';
$true = $phpMailer->send();
dd($true);
I finally found the solution after 2 days. SMTP is only accepting full access "mail.google.com" for some reason. Any other scope given is rejected. After finding the solution I googled for why this is happening, and this guy explains a bit:
Using `gmail.send` scope with SMTP MSA
I'm trying to develop a web application that send email throught Gmail API.
But I'm getting this error :
Call to undefined method Google_Service_Gmail_Message::toSimpleObject()
Here is my code :
// LOAD GOOGLE LIBRARY
$this->CI->load->library('master_google');
$this->CI->load->library('master_phpmailer');
$client = $this->CI->master_google->getClient($data);
$mail = $this->CI->master_phpmailer;
$mail->setFrom($data->sender_email, $data->sender_name);
$mail->addReplyTo($data->response_email, $data->response_name);
$mail->addAddress($data->email);
$mail->Subject = $data->subject;
$mail->msgHTML(htmlspecialchars_decode($data->body));
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$encoded_message = base64url_encode($mime);
// Gmail Message Body
**$message = new Google_Service_Gmail_Message();**
$message->setRaw($encoded_message);
// Send the Email
$service = new Google_Service_Gmail($client);
$email = $service->users_messages->send('me',$message);
if($email->getId()){
return array('stat' => true, 'msg' => '');
} else {
return array('stat' => false, 'msg' => '');
}
The error is generated on this line :
$message = new Google_Service_Gmail_Message();
Any help is appreciated.
I found what was the problem lol, I had a model with the same name : Google_model, I rename that to Mygoogle_model and it works :D
I think the method toSimpleObject() does not exist in your gmail-api class.
I am trying to send email to multiple recipient address in cake php 3.
my codes are :
$this->loadModel('AsIndividualDetails');
$EmailDetails = $this-> AsIndividualDetails->find('all',['fields'=>'email']);
$EmailDetails = $EmailDetails->toArray();
foreach ($EmailDetails as $key => $a) {
$this->loadModel('DomainEmailDetails');
$DomainEmailDetails = $this-> DomainEmailDetails->find('all')->first();
$DomainEmailDetails = $DomainEmailDetails->toArray();
$host = 'ssl://'.$DomainEmailDetails['host_name'];
$username = $DomainEmailDetails['user_name'];
$password = $DomainEmailDetails['user_password'];
$port = $DomainEmailDetails['port'];
$email_to = $a['email'];
$senderName = 'abc';
$email_id ='xyz110#gmail.com';
Email::configTransport('WebMail', [
'className' => 'Smtp',
'host' => $host,
'port' => $port,
'timeout' => 30,
'username' => $username,
'password' => $password,
'client' => null,
'tls' => null,
]);
////////// SEND MAIL
$email = new Email('WebMail');
$email ->template('default','default')
->emailFormat('both')
->from([$username => $senderName])
->to($email_to)
->replyTo($email_id)
->subject('Client Message');
$response = $email->send('My msg');
if($response){
echo 'success';
}else{
echo 'failed';
}
}
When I run this script just only one mail send successfully and after that an error has come :
Cannot modify an existing config "WebMail"
How to solve this error and send mail to all recipient mail address.
If you really need set the config inside a loop, you could delete it before rewrite the config:
use Cake\Mailer\Email;
Email::dropTransport($key);
See Class Email API for more info
Make your email configuration outside of the loop. You don't want to try and establish the configuration every time you send the emails - just one time. Then send all the emails based on that one configuration.
I'm trying to set up amazon SES with PHP. I've scoured the internet and the documentation for AWS PHP SDK but I only see outdated scripts on how to include the actual library and send e-mail. Does anyone here have a working script for using Amazon SES with PHP?
This is the closest I've found to test the script but it doesn't work:
require 'src/aws.phar';
use Aws\Common\Enum\Region;
use Aws\Ses\SesClient;
try {
$ses = SesClient::factory(array(
'key' => 'AKIAJ4ERVU6XXXXXXX',
'secret' => 'kMgagzJmB4Xjw7UD+Md0KNgW+CTE2jCXXXXX/',
'region' => Region::US_EAST_1
));
$ses->verifyEmailIdentity( array(
'EmailAddress' => 'jason#aol.com'
));
}
catch( Exception $e )
{
echo $e->getMessage();
}
First, you would want to disable/delete and generate new keypairs so that you would not have to face bad situation as there are scrapers/bots and people who might misuse your access key-pairs.
As for your original question, you didn't describe what didn't work for you. Anyway, since you wanted a working version, check the source code of this: https://github.com/daniel-zahariev/php-aws-ses which will give you enough idea to get yours working.
This is the working code that i got for you, let me know if this helps
<?php
require 'aws.phar';
use Aws\Ses\SesClient;
$client = SesClient::factory(array('region'=>'us-east-1','version'=>
'latest','credentials' => array('key' => 'xxxxx','secret' => 'xxxxxx',)));
$msg = array();
$msg['Source'] = $message['from'];
$msg['Destination']['ToAddresses'][] = $to_address;
$msg['ReplyToAddresses'][] = $from;
$msg['Message']['Subject']['Data'] = $subject;
$msg['Message']['Subject']['Charset'] = "UTF-8";
$msg['Message']['Body']['Html']['Data'] = $body;
$msg['Message']['Body']['Html']['Charset'] = "UTF-8";
try{
$result = $client->sendEmail($msg);
$logmsg = "Passed ".from." - ".to_address;
} catch (Exception $e) {
$logmsg = "Failed ".$message['from']." - ".$to_address;
error_log('Failed Email '.from." - ".$to_address);
}
Im trying to read mails from a gmail apps account by using Zend Framework. I've just transfered the Zend Framework dir to my server (path: /Zend/library/).
How do I load the Zend Framework and the Mail module? And how do I further read the mail?
I've tried the following with no results:
$path = 'Zend/library/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
I believe the syntax for reading the inbox is something like:
$mail = new Zend_Mail_Storage_Imap(array('host' => 'imap.gmail.com', 'user' => "name#domain.com", 'password' => "mypassword", 'ssl' => 'SSL'));
EDIT
The following code works:
$path = 'Zend/library/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
$mail = new Zend_Mail_Storage_Imap(array('host' => 'imap.gmail.com',
'user' => 'mail#domain.com',
'password' => 'password',
'ssl' => 'SSL'));
echo $mail->countMessages();`
... but when i try to echo unread emails:
echo "Unread mails:\n";
foreach ($mail as $message) {
if ($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) {
continue;
}
// mark recent/new mails
if ($message->hasFlag(Zend_Mail_Storage::FLAG_RECENT)) {
echo '! ';
} else {
echo ' ';
}
echo $message->subject . "\n";
}
I get the following message:
Fatal error: Uncaught exception 'Zend_Mail_Storage_Exception' with message 'cannot login, user or password wrong' in /var/www/zvinx.dk/test/Zend/library/Zend/Mail/Storage/Imap.php:279 Stack trace: #0 /var/www/zvinx.dk/test/gmail.php(11): Zend_Mail_Storage_Imap->__construct(Array) #1 {main} thrown in /var/www/zvinx.dk/test/Zend/library/Zend/Mail/Storage/Imap.php on line 279
It says the username or password is wrong, which is weird cause I didnt change it from when it was working... How come this error occur?
the gmail settings are a little tricky. try:
$mail = new Zend_Mail_Storage_Imap(array('host' => 'imap.gmail.com',
'user' => 'mail#domain.com',
'port' => '993',
'password' => 'password',
'ssl' => 'tls',
'auth' => 'login'
));
NOTE: the gmail are using the SSL/TLS protocol which apparently is different than the standard SSL.
You really don't think that you can start using Zend Framework without reading/learning about the basics of the framework? At least take a look at the quickstart on how to use the framework with the autoloading features and then dive into the Zend_Mail documentation, more specifically the part that says "Reading Mail Messages"
There are the login setting i use to read emails via IMAP and dump attached files
public function imapAction()
{
$config = array('host'=> 'imap.gmail.com',
'user' => 'xx',
'password' => 'xx',
'ssl' => 'SSL',
'port' => 993);//995 pop, imap 993
$mail = new Zend_Mail_Storage_Imap($config);
$maxMessage = $mail->countMessages();
$this->logger->info($maxMessage);
for ($i = $maxMessage; $i <= $maxMessage; $i++)
{
$message = $mail->getMessage($i);
$this->logger->info($i.'Mail from '.$message->from.':'.$message->subject);
if($message->isMultipart())
{
$this->logger->info("has attachments");
$part = $message->getPart(2);
$cnt_typ = explode(";" , $part->contentType);
$name = explode("=",$cnt_typ[1]);
$filename = $name[1];//It is the file name of the attachement in browser
//This for avoiding " from the file name when sent from yahoomail
$filename = str_replace('"'," ",$filename);
$this->logger->info($filename);
$attachment = base64_decode($part->getContent());
$fhandle = fopen($filename, 'w');
fwrite($fhandle, $attachment);
fclose($fhandle);
}
}
}
I had the same issue and this instruction has helped me.
Quit all mail clients that are accessing the affected Gmail account. This means the Mail app on the iPhone and any other place you are accessing your Gmail from such as a computer.
Open browser and navigate to this page: http://www.google.com/accounts/DisplayUnlockCaptcha
Enter your full Gmail address, password and type the characters you see in the picture. Touch the unlock button to verify your account.
Try to read mails from a gmail apps account by using Zend Framework. Your Gmail access should be restored.
If you encounter this error and you are 100% sure about the password you provided it might come from the 2 factor authentication you set on your google account.
Google help gives indications on what to do in this case. I manage to get access to my account by generating an AppPassword in my case