How can I send email from localhost using swiftmailer? - email

How can I send email from localhost using Swiftmailer? also, how can I configure mail catcher with Swiftmailer. Mailcatcher is working with PHP mail() function but not with swiftmailer, I am using vagrant.
require_once ('/include/SwiftMailer/vendor/autoload.php');
$transport = new Swift_SendmailTransport('localhost', 25);
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['xxxxxxxxx' => 'xxxx'])
->setTo(['xxxxxxxxxx' => 'xxxx'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);

Related

Send emails from external mail server in Magento 1.9

I'm trying to get Magento to send transactional emails via our external mail server. I've tried using the SMTP Pro extention, but that didn't work.
I've changed the getMail function in /app/code/core/Mage/Core/Model/Email/Template.php to:
public function getMail()
{
if (is_null($this->_mail)) {
$my_smtp_host = 'xxx';
$my_smtp_port = '587';
$config = array(
'port' => $my_smtp_port, 'auth' => 'login',
'username' => 'xxx',
'password' => 'xxx' );
$transport = new Zend_Mail_Transport_Smtp($my_smtp_host, $config);
Zend_Mail::setDefaultTransport($transport);
$this->_mail = new Zend_Mail('utf-8');
}
return $this->_mail;
}
but the emails are still sending from the local server.
(I know editing core files is bad, this is solely for testing purposes.)
Any ideas on where I'm going wrong?
you may achieve that by setting up a relay between magento server and external mail server. no code changes are necessary if it is an option.

Send mail to multiple recipients from data base using zf2

This is my first application using Zend Framework 2. I want to send an email to recipients retreived from a database, but each time my code displays an error. This is my code:
$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');
if (!$user->offsetExists('id')) {
$idconnected = '0';
} else {
$idconnected = $user->offsetGet('id');
$mail = $db->query("SELECT email FROM user WHERE id =" . $idconnected)->execute()->current();
$message = new Message();
$message->addTo($mail, 'eee#web.com')
->addFrom('xxxx#gmail.com')
->setSubject('Invitation à l’événement : Soirée Latino');
$message->addCc('xxxx#hotmail.com')
->addBcc("xxxx#hotmail.com");
// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'host' => 'smtp.gmail.com',
'connection_class' => 'login',
'connection_config' => array(
'ssl' => 'tls',
'username' => 'xxxx#gmail.com',
'password' => '*********'
),
'port' => 587,
));
$html = new MimePart('<b>Invitation for the event: Latin Night, orgonized by Mr. Jony Cornillon <i>Date : 06/04/2015</i></b>');
$html->type = "text/html";
$body = new MimeMessage();
$body->addPart($html);
//$body->setParts(array($html));
$message->setBody($body);
$transport->setOptions($options);
$transport->send($message);
}
And this is the error:
5.1.2 We weren't able to find the recipient domain. Please check for any
5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
5.1.2 or other punctuation after the recipient's email address. a13sm19808164wjx.30 - gsmtp
I tried with this code, and now it works fine :
foreach ($mail as $recip) {
$message->addTo($recip);
$message->addTo('samehmay#hotmail.com', 'eee#web.com')
->addFrom('maysameh0#gmail.com')
->setSubject('Invitation à l’événement : Soirée Latino');
}

Sent email appears in recipient's spam folder in cakePHP using gmail account?

I am Using cakephp for one of my project. for sending email I am using cakeEmail. For that I created one gmail account for sending emails(i.e. used in code for send mail from that account).Mail sending works but sent mail appears recipient's spam folder.
Also in gmail account that mail dosen't appear sent mail folder.
code is as bellow :
In /app/Config/email.php file is :
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'username#gmail.com',
'password' => 'password',
'transport' => 'Smtp'
);
}
and code in my controller file for password recovery is :
public function _sendemail($user_data,$temporary_password){
$email = new CakeEmail();
$email->config('gmail');
$email->template('welcome', 'password_recovery_email'); //template
$email->emailFormat('html');
$email->viewVars(array(
'temporary_password'=>$temporary_password,
'user_data'=>$user_data
));
$email->from(array('username#gmail.com' => 'Password Recovery'));
$email->to($user_data['User']['email_address']);
$email->subject('password recovery email');
$result=$email->send();
}
Please tell me what should I do to get all sent emails to appears in inbox rather than spam folder.
Thanks
A lot of reasons can make your email be considered a SPAM.
Maybe Gmail can't check the sender (smtp source/your developing machine) as a trusted sender. Have you tried uploading your php script to another server (production) then sending it from there?
Have you tried changing the message content/subject to something different from "Recover your password"?
Have you tried sending the same message to another recipient?
Gmail uses a lot of rules to check whether an email is a spam or not, check out this link: https://webapps.stackexchange.com/questions/5773/how-gmail-and-other-mail-services-detects-a-mail-as-a-spam
Try using this code rather than cakephp's methods:
$subject = "Your Email Subject";
$to = "Recipient's Email Address";
$from = "Your Email Address";
$additional_headers = "From: $from\r\n";
$additional_headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$additional_headers .= "MIME-Version: 1.0\r\n";
$additional_headers .= "Return-Path: $from\r\n";
$additional_headers .= "X-Priority: 3\r\n";
$additional_headers .= "X-Mailer: PHP\r\n";
$message = "Your Email Body";
mail($to, $subject, $message, $additional_headers, "-f $from")

sending email through exchange server via zend server

I want to send a local e-mail through exchange server
but zend give me this Message
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection
failed because connected host has failed to respond."
this is my code
$mailTransport =
new Zend_Mail_Transport_Smtp('smtpserver.edu.com', array(
'auth' => 'login',
'username' => 'dummy.edu.com',
'password' => '123456',
'port' => '25',
));
Zend_Mail::setDefaultTransport($mailTransport);
$mail = new Zend_Mail();
$mail->setFrom('dummy.edu.com');//anas.azmeh#ucti.edu.my');
$mail->setBodyHtml('some message - it may be html formatted text');
$mail->addTo('dummy.edu.com', 'recipient');
$mail->setSubject('subject');
$mail->send();
I tried the same code in gmail configuration and it works perfectly
please help me as fast as possible
$mail = new Mail\Message();
$mail->setBody("Send Mail");
$mail->setFrom('test#gmail.com', 'Test Site');
$mail->addTo($email_id, 'Test Site');
$mail->setSubject('Your connection is not stablish');
$transport = new Mail\Transport\Sendmail();
$transport->send($mail);
I think that port 25, 23 and 587 are blocked
because I tried to telnet them but it give me fail, so the problem may come from these blocking

email sending from localhost in cakephp using emailcomponent

<?php
class EmailsController extends AppController
{
var $uses=null;
var $components=array(
'Email'=>array(
'delivery'=>'smtp',
'smtpOptions'=>array(
'host'=>'ssl://smtp.google.com',
'username'=>'username#gmail.com',
'password'=>'password',
'port'=>465
)
));
function sendEmail() {
$this->Email->to = 'Neil <neil6502#gmail.com>';
$this->Email->subject = 'Cake test simple email';
$this->Email->replyTo = 'neil6502#gmail.com';
$this->Email->from = 'Cake Test Account <neil6502#gmail.com>';
//Set the body of the mail as we send it.
//Note: the text can be an array, each element will appear as a
//seperate line in the message body.
if ( $this->Email->send('Here is the body of the email') ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
$this->redirect('/');
}
}
?>
above code is my controller responsible for sending emails...
but when i run this function sendEmail() using url http://localhost/authentication/emails/sendemail it shows nothing not even single error or any response... complete blank page. I don't know the reason.
I think I had the same issue a while ago. It might be that you need to change the to address into a value that holds just the address, so instead of Name <email#example.com> you should use email#example.com.
You can check for errors by logging (or debugging) the smtp errors with:
$this->log($this->Email->smtpError, 'debug');
or
debug($this->Email->smtpError);
Good luck. Hope this helps.
/* Auf SMTP-Fehler prüfen */
$this->set('smtp_errors', $this->Email->smtpError);
I would add the Email Config to your email.php file located in /app/Config/email.php , if it doesn't exist copy email.php.default to email.php, Change the smtp settings there
public $smtp = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret'
);
At the top of your Controller above class EmailsController extends AppController add,
App::uses('CakeEmail', 'Network/Email');
Then in your function sendEmail() try,
$Email = new CakeEmail();
$Email->from(array('me#example.com' => 'My Site'))
->to('you#example.com')
->subject('About')
->send('My message');
To test emails what I usually do is send them to the Cake Logs,
**In /app/Config/email.php, include: ( The log output should be /app/tmp/logs/debug.log )
public $test = array(
'log' => true
);
Also doing this add 'test' to your $Email variable like,**
$Email = new CakeEmail('test');
Actually in my case : I got a error message "SMTP server did not accept the password."
After that i follow the below link and issue has been resolved :
Step1 : https://blog.shaharia.com/send-email-from-localhost-in-cakephp-using-cakeemail/
Step2 : Sending Activation Email , SMTP server did not accept the password