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
Related
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
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 am new in swiftmailer, I am having this kind of error;
Fatal error: Cannot redeclare class Swift_Mime_Headers_DateHeader in /home/hosting/public_html/lib/classes/Swift/Mime/Headers/DateHeader.php on line 17
I don't know what to do, please help
My code is this:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your 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('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
It keeps failing as per my below check and die. Any reason why? The SMTP settings are correct...
require_once '../lib/swift_required.php';
// Create the Transport the call setUsername() and setPassword()
$transport = Swift_SmtpTransport::newInstance('smtp.server.com', 25)
->setUsername('email#server.com')
->setPassword('password')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Creating a test message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$numSent = $mailer->send($message);
if ($mailer->send($message))
{
die('Mail sent');
} else {
die('Error: Mail failed');
}
I need to send mail through localhost (in both LAMP and WAMP) using PHP. How can I do this? I read many tutorials on this requirement and yet didn't get any solutions. I read that using SMTP we can do this but how will I get the credentials for using SMTP? Hope that someone will help me to do this.
Thank you in advance.
There are many ways to send mail in PHP.
You can use PHPs mail function.
http://php.net/manual/en/function.mail.php
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('caffeinated#example.com', 'My Subject', $message);
?>
SwiftMailer is also worth looking at
It has lots of features for sending mail in different ways (transport types, attachments etc), and it's easy to use.
http://swiftmailer.org/
http://swiftmailer.org/docs/sending.html
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your 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('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);