I am successully able to send emails via Amazon SES with the code below, but I am trying to set a return path and it doesnt work. When i use ->setReturnPath('bounce#example.com') the emails do not send at all. Can anyone shed some light why, or know how to get it to work?
Any help would be great!
This is the latest swiftmailer (4.2.2)
require_once 'lib/swift_required.php';
require_once 'classes/Swift/Transport/AWSTransport.php';
require_once 'classes/Swift/AWSTransport.php';
require_once 'classes/Swift/AWSInputByteStream.php';
define( 'AWSAccessKeyId', 'XXXXX' );
define( 'AWSSecretKey', 'XXXXX' );
//Create the Transport
$transport = Swift_AWSTransport::newInstance( AWSAccessKeyId, AWSSecretKey );
$transport->setDebug( true ); // Print's the response from AWS for debugging.
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance( $transport );
//Create the message
$message = Swift_Message::newInstance()
->setSubject( 'Sample Subject' )
->setFrom( array( 'test#example.com' ) )
->setTo( array( 'to#someone.com' ) )
->setBody( $message_body, 'text/html' )
->addPart( "Please use a HTML compatible web browser to view this email.", 'text/plain' );
$mailer->send( $message );
You can use this Swiftmailer function:
$message = Swift_Message::newInstance();
$headers = $message->getHeaders();
$headers->addPathHeader('Your-Header-Name', 'person#example.org');
I've just tried it with similar code, and it worked. swiftmailer is now up to version 4.3.0, so it could be something fixed in that. Otherwise about the only difference in my code is I don't have the addPart() that you have.
Also, had you checked your php error logs? :-)
Related
I am sending an email using swiftmailer in symfony2, but I would like to add a specified PDF file as a file attachment to the email. How would I do that?
Here is my current code:
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('recipient#example.com')
->setBody(
$this->renderView(
'HelloBundle:Hello:email.txt.twig',
array('name' => $name)
)
)
;
$this->get('mailer')->send($message);
You have several options to attach a document to an email using swift mailer.
From the symfony doc:
$message = Swift_Message::newInstance()
->setFrom('from#example.com')
->setTo('to#example.com')
->setSubject('Subject')
->setBody('Body')
->attach(Swift_Attachment::fromPath('/path/to/a/file.zip'))
;
$this->getMailer()->send($message);
If you want to upload a file from buffer, you can do this:
$attach=getPdfFunction(); //binary
Swift_Attachment::newInstance($attach, 'document.pdf','application/pdf');
You can add your attachement using this line:
$message->attach(\Swift_Attachment::fromPath($attach));
The following code should do it:
$attachment = \Swift_Attachment::fromPath('subpath/to/attachment');
$message->attach($attachment);
Im really stuck with this swiftmail method of sending email with attachment. My emails never seem to be delivered. I send the email and it just ruturns without any errors but when I check my mail nothing is delivered. Please help! I troubleshooted everything and everything works except for the attach() function. I dont know whats wrong. Heres my code.
<?php
//I didnt add my validations and variables above.....
require_once('./swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 25)
->setUsername('user')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject('Online Form')
->setFrom(array($from_email => $full_name))
->setTo(array('email#mail.com' => 'Jack'))
->setBody(''.$message_temp.'')
->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])
->setFilename($_FILES['attachment']['name']));
$result = $mailer->send($message);
?>
Oops! Never mind, I solved it myslef.
I assigned $_FILES['attachment']['tmp_name'] to a temporary variable and it worked!
Dont know why but that solved it for me.
Here's my code;
// Swiftmail commands ====================================
require_once('./swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 587)
->setUsername('email#host.com')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject_temp)
->setFrom(array($from_email => $full_name))
->setTo(array('email#host.com' => 'Jack'))
->setBody($message_temp)
->attach(Swift_Attachment::fromPath($file_temp_name)
->setFilename($name_of_file));
$result = $mailer->send($message);
// Swiftmail commands ====================================
Where $file_temp_name = $_FILES['attachment']['tmp_name']; and
$name_of_file = basename($_FILES['attachment']['name']);
I ve tried everything, i don't know how to fix this, so, i am using this Swift Mailer library to send a confirmation email. So here is the code from my index.php
if($confirm){
//include the swift class
include_once 'inc/php/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){
//email sent
$action['result'] = 'success';
array_push($text,'Thanks for signing up. Please check your email for confirmation!');
}else{
$action['result'] = 'error';
array_push($text,'Could not send confirm email');
}
}
And my send_email function is in another php file functions.php
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl)
->setUsername('my email here')
->setPassword('my password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('somedomain.com' => 'somethinghere'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
The error that i am getting is
Fatal error: Call to undefined method Swift_MailTransport::setUsername() in /srv/disk7/something/www/something/signup/inc/php/functions.php on line 31
How can i fix this? I am a beginner in php.
I'm guessing Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl) fails to create the expected instance.
BTW, shouldn't it be Swift_MailTransport::newInstance('smtp.gmail.com',465,'ssl') (i.e. smtp.gmail.com and ssl in quotes)?
Swift_MailTransport, from the documentation...
...sends messages by delegating to PHP's internal mail() function.
In my experience -- and others' -- the mail() function is not particularly predictable, or helpful.
Another thing it does not have is a setUsername or setPassword method.
I'd say you want to use Swift_SmtpTransport
One more thing; as pointed out by others, your string arguments should be quoted, ie
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
Looks like you haven't included the Swift Mail class.
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);
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 !!