I am trying to send an email threw a laravel controller
this is my code:
$email_sender = 'endritsheholli#gmail.com';
$email_pass = 'blablablablablabla';
$email_to = 'endritsheholli.a#gmail.com';
// Backup your default mailer
$backup = \Mail::getSwiftMailer();
try{
//https://accounts.google.com/DisplayUnlockCaptcha
// Setup your gmail mailer
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587))
->setUsername($email_sender)
->setPassword($email_pass);
$mailer = new Swift_Mailer($transport);
// Set t
he mailer as gmail
\Mail::setSwiftMailer($gmail);
$data['emailto'] = $email_sender;
$data['sender'] = $email_to;
//Sender dan Reply harus sama
$message = (new Swift_Message('Reset Password'))
->setFrom([$email_sender => 'EndritSheholli'])
->setTo([$email_to , 'endritsheholli.a#gmail.com' => 'ESheholli'])
->setBody('Here is the message itself')
;
$result = $mailer->send($message);
}catch(\Swift_TransportException $e){
$response = $e->getMessage() ;
echo $response;
}
the error that is returned is:
Class 'App\Http\Controllers\Swift_SmtpTransport' not found
I have installed the composer package and I have tried a lot of proposals in the internet but I cannot find a solution.
You must declare Swift_SmtpTransport in use section or write full namespace of class in your code. For example:
use Swift_SmtpTransport;
// other code
or
$transport = new \Swift_SmtpTransport
Related
I try to send emails via Swift-Mailer. According to the documentation, it should work like this, but I neither get a mail nor an error message.
Here is the code of the PHP Swift file:
<?php
require_once '/composer/vendor/autoload.php';
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername('*******#gmail.com')
->setPassword('password')
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['john#doe.com' => 'John Doe'])
->setTo(['infinity.community.work#gmail.com', 'other#domain.org' => 'A name'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
?>
And here is the composer.json:
{
"require": {
"swiftmailer/swiftmailer": "^6.0"
}
}
Check again with this syntax and make sure the email or the password are spelt correctly:
$trp = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('*********#gmail.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($trp);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(['john#doe.com' => 'John Doe'])
->setTo(['infinity.community.work#gmail.com', 'other#domain.org' => 'A name'])
->setBody('Here is the message itself');
$mailer->send($message);
EDIT: worked successfully with SwiftMailer version 5.4
I have used this swiftmailer
The PHP code seems to work but ultimately this does nothing.
I hope someone dealt with this repo could help me.
<?php
require_once 'swiftmailer/vendor/autoload.php';
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465))
->setEncryption('ssl')
->setUsername('alice#wonderland')
->setPassword("zxcfA!0987")
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['john#doe.com' => 'John Doe'])
->setTo(['bob#cling.com' => 'Bob'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
I have a simple code for sending mails. sometime $host might not available or my SMTP server might be down but what happens in these cases swiftmailer throws lot of exceptions and $result despite of returning true or false give me a complete mess of errors.
so how i can turn off the errors for this page of code but not for the whole library?
$transport = Swift_SmtpTransport::newInstance(self::$host, 25)
->setUsername(self::$username)
->setPassword(self::$password);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Custom Sheets");
$message->setFrom(array('EDB#abc.com.pk' => 'Approval of Custom Duty Sheet'));
$message->setTo($to);
$message->setBody($html,'text/html');
//Send the message
$result = $mailer->send($message);
You can catch that exception and handle it:
try {
$transport = Swift_SmtpTransport::newInstance(self::$host, 25)
->setUsername(self::$username)
->setPassword(self::$password);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Custom Sheets");
$message->setFrom(array('EDB#abc.com.pk' => 'Approval of Custom Duty Sheet'));
$message->setTo($to);
$message->setBody($html,'text/html');
//Send the message
$result = $mailer->send($message);
} catch(Exception $e) {
// handle error here
}
Turning off errors / warnings / notices in general:
error_reporting(-1);
So I am trying to setup swift mailer to work with the Mandrill API, but it keeps throwing the following error:
Failures:Array ( [0] => example#email.com ) (I have a proper email in this place in my code)
My code is as follows:
$subject = 'Hello from Mandrill, PHP!';
// approved domains only!
$from = array('example2#email.com' =>'Your Name');
$to = array(
'example#email.com' => 'Recipient1 Name'
);
$text = "Mandrill speaks plaintext";
$html = "Mandrill speaks HTML";
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
$transport->setUsername(getenv('my#mandrillemail.com'));
$transport->setPassword(getenv('mymandrillpass'));
$swift = Swift_Mailer::newInstance($transport);
$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');
// Pass a variable name to the send() method
if (!$swift->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
What is going wrong?
Try using SSL and port 465.
$xport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 465, 'ssl');
$xport->setUsername('mandrilluser')
->setPassword('mandrillpass');
See if this works for you.
My problem ended up being that I had to change from using the actual Mandrill account password to the API in the ->setPassword() variable.
Since this is purely an error with your credentials, cross check if the password you are using is actually the token generated by Mandrill or not .
Password doesn't mean the 'password' of your account !!
I am doing some testing prior to working on some production code and need to figure out how to do an auto e-mail.
The below script runs fine and the result of the send method returns 1, as if it sends. However, nothing ever makes it to the recipient.
require_once '/home/absolut2/lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.mysite.com', 25)
->setUsername('myuser')
->setPassword('password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
//Mail
$transport = Swift_MailTransport::newInstance();
*/
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Subject')
->setFrom(array('rp#mysite.com' => 'RP'))
->setTo(array('rp#gmail.com'))
->setBody('Here is the message itself');
//Send the message
$result = $mailer->send($message);
echo "Messages sent: " . $result;
The code itself seems fine, so I guess something else is wrong. Either check the spam queue of the recipient or maybe just the address was rejected.
Find out if addresses were rejected.
You can do that with this code:
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}