Sending mail through Localhost (in both WAMP and LAMP) - php

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

Related

PHP GMail SMTP Failure

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

New in swiftmailer

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

PHP SwiftMailer Not sending

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);
}

Swiftmailer 4 does not retrieve bounces as $failedRecipients

I am trying this code (from http://swiftmailer.org/docs/sending.html):
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//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'))
->setBody('Here is the message itself')
;
//Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('receiver#domain.org', 'other#baddomain.org' => 'A name');
foreach ($to as $address => $name)
{
$message->setTo(array($address => $name));
$numSent += $this->send($message, $failedRecipients);
}
printf("Sent %d messages\n", $numSent);
The problem is that if I sent an email to a bad domain swiftmailer recognize it as a correct sent email and $failedRecipients is empty. In my mail box I have returned a failure notice.
Why does Swiftmailer not recognize this mail as as a failure, and does not populate $failedRecipients Array?
Swiftmailer only takes care to hand the email over to the mail-server. Everything else is not related to Swiftmailer.
What you get is a bounce message, and you need to process them on your own, because the email itself actually was a syntactically mail address that was not rejected by the first server.
That btw is the case for any other mailing library and even the php mail function. You might be looking for a bounce processing application or code.
Related: Bounce Email handling with PHP?

trying to send mail using swift mailer, gmail smtp, php

Here is my code:
<?php
require_once 'Swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
->setUsername('me#ff.com')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('me#ff.com' => 'MY NAME'))
->setTo(array('you#ss.com' => 'YOU'))
->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
?>
AFter RUNNING GOT THIS ERROR...
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""' in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php:406
Stack trace:
#0 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(299): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('', Array)
#1 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(107): Swift_Transport_AbstractSmtpTransport->_readGreeting()
#2 /home/sitenyou/public_html/Swift/lib/classes/Swift/Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start()
#3 /home/sitenyou/public_html/sgmail.php(16): Swift_Mailer->send(Object(Swift_Message))
#4 {main} thrown in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php on line 406
GMail's SMTP requires encryption. Use:
Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl");
there is missing the ssl parameter, it should be something like that
Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
Tested and work fine
Swift SmtpTransport - Code (send a email)
The SMTP of GMAIL is: smtp.googlemail.com
The Full Code:
<?php
$pEmailGmail = 'xxxx#gmail.com';
$pPasswordGmail = '********';
$pFromName = 'MundialSYS.com'; //display name
$pTo = 'xxxxxx#xxxx.xxx'; //destination email
$pSubjetc = "Hello MundialSYS"; //the subjetc
$pBody = '<html><body><p>Hello MundialSYS</p></html></body>'; //body html
$transport = Swift_SmtpTransport::newInstance('smtp.googlemail.com', 465, 'ssl')
->setUsername($pEmailGmail)
->setPassword($pPasswordGmail);
$mMailer = Swift_Mailer::newInstance($transport);
$mEmail = Swift_Message::newInstance();
$mEmail->setSubject($pSubjetc);
$mEmail->setTo($pTo);
$mEmail->setFrom(array($pEmailGmail => $pFromName));
$mEmail->setBody($pBody, 'text/html'); //body html
if($mMailer->send($mEmail) == 1){
echo 'send ok';
}
else {
echo 'send error';
}
?>
I cannot be sure, but I think that Gmail's port is 587 using TLS, which is not SSL, but a newer version of it. You should check into that, because I think you are placing the wrong construction code.
Best of luck!
I have managed to get this working without the SSL, here is how:
$transport = Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
->setUsername('contact#columbussoft.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject)
->setFrom(array($emailTo=>$name))
->setTo(array($emailTo=>'Neo Nosrati'))
->addPart($body,'text/plain')
->setReturnPath('other#columbussoft.com');
I'm using the "Messages Swift Mailer" bundle in Laravel 3 and having the same issue. After some testing, in my case, the solution was to set the same email address that I used in the SMTP authentication on the "from" parameter.
I was trying to use a different address and that was triggering the "swiftmailer expected response code 220 but got code with message" error.
Hope that helps.
I got same error before and i added "ssl" parameter in Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") like osos said.
IT WORKS!! thanks..:D
this is my code:
<?php
require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('XXXXXXX#gmail.com')
->setPassword('XXXXXXX');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('THIS IS THE SUBJECT')
->setFrom(array('XXXXXXX#gmail.com' => 'YOUR NAME'))
->setTo(array('XXXXXXX#gmail.com' => 'YOU'))
->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
?>
For google apps, in addition to setting to port 465 and ssl as recommended in the accepted answer, you may have to enable allow less secure apps setting, as per https://stackoverflow.com/a/25238515/947370

Categories