I have the following code:
<?php
include 'swiftmailer/lib/swift_required.php';
//*** Create the Transport ***
$transport = Swift_SmtpTransport::newInstance('smtp.My-Hosting', 25)
->setUsername('MyUserName')
->setPassword('Password');
//*** Create the Mailer using your created Transport ***
$mailer = Swift_Mailer::newInstance($transport);
//*** Create a message ***
$message = Swift_Message::newInstance('NameOfInstance')
->setFrom(array('Support#test.com' => 'John Doe'))
->setTo(array("John#test.com" => "John Smith"))
->setBody('This is a test email message') ;
//*** Send the message ***
$result = $mailer->send($message);
?>
How do I use a PHP variable (i.e. $email, $name, etc.) in the setFrom, setTo, and setBody arrays? (I do not want to type individual emails/names).
Provided I understand your question and code (as it's unformatted), you should be able to simply replace the strings with the variable name of your choice.
For example, your original code, formatted, is:
setUsername('MyUserName') ->setPassword('Password');
//*** Create the Mailer using your created Transport ***
$mailer = Swift_Mailer::newInstance($transport);
//*** Create a message ***
$message = Swift_Message::newInstance('NameOfInstance') ->setFrom(array('Support#test.com' => 'John Doe')) ->setTo(array("John#test.com" => "John Smith")) ->setBody('This is a test email message') ;
//*** Send the message ***
$result = $mailer->send($message);
Now, you can simply replace the "John#test.com" with $from_email or whatever variable name you choose ... and so forth with the other strings of text.
setUsername('MyUserName')->setPassword('Password');
//*** Create the Mailer using your created Transport ***
$mailer = Swift_Mailer::newInstance($transport);
//*** Create a message ***
$message = Swift_Message::newInstance('NameOfInstance')->
setFrom(array($from_email => $from_name))->
setTo(array($to_email => $to_name))->setBody($body) ;
//*** Send the message ***
$result = $mailer->send($message);
Related
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);
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?