How to set reply-to when using Swiftmailer - php

How can reply-to be set when using Swiftmailer. The docs mentioned the function setReplyTo() but without specific instructions on how to use it.
Any help will be appreciated.

I can confirm that the setReplyTo method works the same way as (for example) the setCC method.
$message->setReplyTo(array(
'person1#example.org',
'person2#otherdomain.org' => 'Person 2 Name',
'person3#example.org',
'person4#example.org',
'person5#example.org' => 'Person 5 Name'
));

For those who experienced the same confusion as me, you are not able to run $recipients->setReplyTo() on a Swift_RecipientList but you are able to do so on the Swift_Message directly:
$recipients = new Swift_RecipientList;
// this is correct
$recipients->addTo('some#email.com');
// this method does not exist so this does not work
$recipients->addReplyTo('some.other#email.com');
$message = new Swift_Message($subject, $message, 'text/html');
// you can, however, add the reply-to here like this
$message->setReplyTo('some.other#email.com');
// and of course sending the e-mail like this with the reply to works
$swift->send($message, $recipients, 'some#email.com');

Related

PHP Gmail API get message info for From,To, Cc, Bcc, Reply-To, Body, Attachments

I'm using Gmail API https://developers.google.com/gmail/api/v1/reference/ .
But I can't find single message info like- FROM, TO, CC, BCC, REPLY-TO with name and address.
Need output as-
$msg['fromAddress'] = 'fromemail#gmail.com';
$msg['fromName'] = 'From Name';
$msg['to'] = array('John Lok' => 'johnlok#gmail.com', 'Smither Fan'=>'smither#gmail.com');
$msg['cc'] = array('krish Hosa' => 'krish23#gmail.com', 'Singam Hat'=>'singhamso#gmail.com');
$msg['bcc'] = array('Loban' => 'lobandash#gmail.com', 'Krain Root'=>'krainroot#gmail.com');
$msg['body'] = 'sample Message Text';
$msg['attachments'] = array('Flower.jpg' => 'https://filelink.com/files/Flower-d9edkifsk3edmfsdf94rffjofs.jpg', 'Bill List.pdf'=>'https://filelink.com/files/Bill-List-34324234mfskfsofsc.pdf');
$msg['reply-to'] = array('From Name' => 'fromemail#gmail.com');
Gmail API requires that those mail headers fulfills the requirements of RFC2822. Then, all those headers should be compiled in a URL-safe string as defined in RFC4648. Here you can see an example of this approach to send mails.
I understand that you need to read those headers from existing mails. If that is correct, then you only have to reverse the former approach. First you would need to use users.messages.get() to read an instance of the existing mail. In the raw field it is stored in the previously mentioned URL-safe string. Please, ask me any further doubts.

Add variables to CodeIgniter email call

I have been trying to create variables to parse into the email class for sending mails in codeigniter. Assuming I want to send a mail to the logged in user, I decided to create a variable
uemail = $_SESSION['email'];But then when I decided to parse it into the email class as $this->email->to($uemail);, does not send the email to the mail. But then when I use $this->email->to('email#email.com'); the mail is sent to the email correctly so I'm thinking I'm experiencing problem with parsing the variable. Doing $this->email->to($_SESSION['email']); too does not work. Please help me out. Thank you .
I just updated my question with detailed codes. I hope this helps. See below;
<?php
$this->Sendmails_model->sendmail();
$uemail = $_SESSION['email'];
$this->email->to($uemail);
$this->email->subject('Email subject here');
$maildata = array(
'firstName' => $_SESSION['first'],
'content' => 'Email content here'
);
$body = $this->load->view('emails/basic',$maildata,TRUE);
$this->email->message($body);
$this->email->send();
?>
For setting session variables you have to use this syntax,
$this->session->userdata('session_variable');
Looking up the link in #Vickel 's comment, $_SESSION['session_variable'] may also be used in CodeIgniter. Hence no problem with the codes.
From your comment, the issue was with the server receiving the email, hence we may consider this question solved.

Swift mailer default sender

I'm trying to set a default sender to all messages in Swift Mailer 4.3.0, but couldn't find a proper solution. I want to avoid using ->setFrom() in every message, since it'll be a future headache. I could use a constant for that, but I was looking for a more elegant solution.
$message = Swift_Message::newInstance()
->setSubject('subject')
->setFrom(array('sender#domain.com' => 'Sender'))
->setTo(array('recipient#domain.com'))
->setBody('Message');
Thanks!
You can't.
As far as I know, you can't omit this parameter.
A From: address is required and is set with the setFrom() method of the message. From: addresses specify who actually wrote the email, and usually who sent it.
This is maybe the best solution:
->setFrom(MyClass::FROM_EMAIL);
Once thing you can try, is to create an instance once in your application, and clone it when you want to send a new mail without re-defining the from part:
// somewhere early in your app
$message = Swift_Message::newInstance()
->setFrom(array('sender#domain.com' => 'Sender'));
And then, somewhere else:
$newMessage = clone $message;
$newMessage
->setSubject('subject')
->setTo(array('recipient#domain.com'))
->setBody('Message')
->send();
If you use a SwiftMailer ≥ 4 and are familiar with Composer, you can use a Defaults Plugin for SwiftMailer to achieve this.
First install the package:
composer require finesse/swiftmailer-defaults-plugin
Second set up a Swift_Mailer instance:
$mailer = new Swift_Mailer(/* your transport here */);
$mailer->registerPlugin(new Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin([
'from' => ['sender#domain.com' => 'Sender']
]));
And then send emails without specifying the from address:
$mailer->send((new Swift_Message())
->setSubject('subject')
->setTo(array('recipient#domain.com'))
->setBody('Message'));

php Email Class

I have a php script that sends an email. It currently uses the php mail() function.
That function then connects to sendmail feature on the linux machine. However for this implementation we were wanting to create a php script that doesn't rely on sendmail being setup or even existing so that we can move to code to any machine and have all the functionality self contained.
First off is this even possible? I've seen a few downloadable classes online so I think that it is.
Second has anyone done this before and do you know a good class or place to start?
I do need to send attachments along with the email.
PHPMailer is a very nice library which you can use to send mails via SMTP, so you don't have to rely on sendmail. PHPMailer also provides an easy way to attach files to your email.
You may also want to consider Zend_Mail. We've been using it for awhile now without issue.
There are lots of examples out there - I use swiftmailer ... http://swiftmailer.org/ or http://code.google.com/p/swiftmailer/
example usage :
require_once 'swift/lib/swift_required.php';
$smtp = Swift_SmtpTransport::newInstance('smtp.host.tld', 25)
->setUsername(' ... ')
->setPassword(' ... ');
$mailer = Swift_Mailer::newInstance($smtp);
$message = Swift_Message::newInstance('Your subject');
$message
->setTo(array(
'user1#example.org',
'user2#example.org' => 'User Two',
'user3#exmaple.org' => 'Another User Name'
))
->setFrom(array('your#address.com' => 'Your Name'))
->attach(Swift_Attachment::fromPath('/path/to/doc1.pdf'))
->attach(Swift_Attachment::fromPath('/path/to/doc2.pdf'))
->setBody(
'Here is an image <img src="' . $message->embed(Swift_Image::fromPath('/path/to/image.jpg')) . '" />',
'text/html'
)
->addPart('This is the alternative part', 'text/plain')
;
if ($mailer->send($message))
{
echo "Message sent!";
}
else
{
echo "Message could not be sent.";
}

Help With Sending a Mail in PHP Using Mail Function

My code-
$from = "From:Company\n\r";
$mesg = include('mail.html');
function mail($u,'hey',$mesg);
basically, i want to send the mail, where the message needs to be the mail.html.
help...the mesage needs to be sent to $u.
Here's an example (composed from the examples in the manual) of how you can do this when using Swift Mailer:
<?php
require_once 'lib/swift_required.php';
//Create the message
$message = Swift_Message::newInstance()
//Give the message a subject
->setSubject('Your subject')
//Set the From address with an associative array
->setFrom(array('your#email.com' => 'Your Name'))
//Set the To addresses with an associative array
->setTo(array('recipient#domain.org' => 'Recipient\'s name'))
//Set CC
->setCc('support#example.com')
//Give it a body
->setBody(file_get_contents('mail.html'))
;
//Create the Mailer
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
//Send the message
$result = $mailer->send($message);
This won't work because include() includes a file and evaluates (interprets the contents), you are trying to make it act like a function that returns a value which it is not.
What you need to do is read the contents of the file into a variable using file_get_contents() or a simillar function and then use it as the message body.
I advice you to use the PHPMailer Class really it will give you a lot of facilites and features and also the download contains examples you will need to include the class file then use it its free. for mor information go to this link
Don't use function in front of mail, just do
mail($emailto, $subject, file_get_contents('mail.html'), $headers);

Categories