Get value of TO in cakephp 3 email layout - cakephp-3.0

I need to add this in the layout of the email I send out:
We sent this email to: [EMAIL ADDRESS] where [EMAIL ADDRESS] needs to be replaced with the email address of the user this email is being sent to.
How do I get get value of TO in the layout?

Could you just set this as a template variable using viewVars like this:
$Email = new Email();
$Email->emailFormat('html');
$Email->from(['me#me.com' => 'Me Name']);
$Email->to($sendTo);
$Email->subject('My Subject');
$Email->viewVars([
'sendTo' => $sendTo,
]);
Then just access $sendTo in your template.

Related

How To send PDF to a specific user from database via email?

This is my code for generate PDF and send it to admins. The problem it is sending all the bills stored in database to all. But I want to send specific bill to specific user. I have connected two tables using foreign key named property_id. I want to send bill to an admin whose property_id is similar to bill property_id. How to do that?
code::
$users = User::where('user_type', 'admin')->get();
foreach($users as $user) {
$data["email"] = $user->email;
$data["title"] = "From Admin";
$data["body"] = "Bill To Pay";
$pdf = PDF::loadView('emails.myTestMail', $data);
$mail = Mail::send('emails.myTestMail', $data, function($message) use($data, $pdf) {
$message->to($data["email"], $data["email"])
->subject($data["title"])
->attachData($pdf->output(), "Bill.PDF");
});
}
Everything is working fine. But it is sending all the information stored in bill table to all user. I want to send specific data to specific user.
Thank You.

Sending email with database content in yii2

I want to send email in yii2. I store the body of my email in sqlyog. But, I have an error when I get information from database as the body of my email. The error is:
quoted_printable_encode() expects parameter 1 to be string, object
given
. How can I solve it? This is my code:
$pesan = \frontend\models\Pesan::find()->select(['pesan'])->where(['kategori' => 'notifikasi_awal'])->one();
$message = $pesan;
$email = \Yii::$app->mailer->compose()
->setFrom([\Yii::$app->params['adminEmail'] => 'Sistem Informasi Paket'])
->setTo($tujuan)
->setSubject("[Pemberitahuan ]")
->setHtmlBody($message)
->send();
The instruction
$pesan = \frontend\models\Pesan::find()->select(['pesan'])
->where(['kategori' => 'notifikasi_awal'])->one();
return a model then an object
if you want a value you can obtain this value this way
$message = $pesan->your_message_field;

Django email message as HTML

I have an email template that I use to send emails of different kinds. I'd rather not keep multiple email HTML templates, so the best way to handle this is to customize the message contents. Like so:
def email_form(request):
html_message = loader.render_to_string(
'register/email-template.html',
{
'hero': 'email_hero.png',
'message': 'We\'ll be contacting you shortly! If you have any questions, you can contact us at meow#something.com',
'from_email': 'lala#lala.com',
}
)
email_subject = 'Thank you for your beeswax!'
to_list = 'johndoe#whatever.com'
send_mail(email_subject, 'message', 'from_email', [to_list], fail_silently=False, html_message=html_message)
return
When the email is sent however, the html codes don't work. The message appears as it is exactly, angled brackets and all. Is there a way for me to force it to render as HTML tags?
Use EmailMessage to do it with less trouble:
First import EmailMessage:
from django.core.mail import EmailMessage
Then use this code to send html email:
email_body = """\
<html>
<head></head>
<body>
<h2>%s</h2>
<p>%s</p>
<h5>%s</h5>
</body>
</html>
""" % (user, message, email)
email = EmailMessage('A new mail!', email_body, to=['someEmail#gmail.com'])
email.content_subtype = "html" # this is the crucial part
email.send()
Solved it. Not very elegant, but it does work. In case anyone's curious, the variable placed in the email template should be implemented as so:
{{ your_variable|safe|escape }}
Then it works! Thanks guys!
You can use EmailMultiAlternatives feature present in django instead of sending mail using send mail. Your code should look like the below snipet.
from django.core.mail import EmailMultiAlternatives
def email_form(request):
html_message = loader.render_to_string(
'register/email-template.html',
{
'hero': 'email_hero.png',
'message': 'We\'ll be contacting you shortly! If you have any questions, you can contact us at meow#something.com',
'from_email': 'lala#lala.com',
}
)
email_subject = 'Thank you for your beeswax!'
to_list = 'johndoe#whatever.com'
mail = EmailMultiAlternatives(
email_subject, 'This is message', 'from_email', [to_list])
mail.attach_alternative(html_message, "text/html")
try:
mail.send()
except:
logger.error("Unable to send mail.")

How to setup send HTML email with mail gem?

I am sending email using the Mail gem. Here's my code:
require 'mail'
require 'net/smtp'
Mail.defaults do
delivery_method :smtp, { :address => "smtp.arrakis.es",
:port => 587,
:domain => 'webmail.arrakis.com',
:user_name => 'myname#domain.com',
:password => 'pass',
:authentication => 'plain',
:enable_starttls_auto => true }
end
Mail::ContentTypeField.new("text/html") #this doesnt work
msgstr= File.read('text2.txt')
list.each do |entity|
begin
Mail.deliver do
from 'myname#domain.com'
to "#{entity}"
subject 'a good subject'
body msgstr
end
rescue => e
end
end
end
I don't know how to set up the content type, so that I can format my email as html for example. Though I actually just wish to be able to define bold text like my email client does: bold text. Does anybody know which content-type I need to specify in order to achieve this, and how to implement it with mail?
Just a note, the code above works fine for sending plain text email.
From the documentation
Writing and sending a multipart/alternative (html and text) email
Mail makes some basic assumptions and makes doing the common thing as
simple as possible.... (asking a lot from a mail library)
mail = Mail.deliver do
to 'nicolas#test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel#test.lindsaar.net.au>'
subject 'First multipart email sent with Mail'
text_part do
body 'This is plain text'
end
html_part do
content_type 'text/html; charset=UTF-8'
body '<h1>This is HTML</h1>'
end
end
#Simone Carletti's answer is essentially correct, but I was struggling with this and didn't want a plain text portion to my email and a separate HTML portion. If you just want the entire email to be HTML, something like this will work:
mail = Mail.deliver do
to 'nicolas#test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel#test.lindsaar.net.au>'
subject 'First email sent with Mail'
content_type 'text/html; charset=UTF-8'
body '<h1>This is HTML</h1>'
end
I may have missed it, I didn't see anything in the Mail gem documentation describing how to do that, which I would think would be more common than making a multipart message. The documentation only seems to cover plain text messages and multipart messages.

How do you get Amazon SES to send upon submission of form in Padrino

Following the instructions here: http://www.padrinorb.com/guides/padrino-mailer
I have the delivery method added on the app.rb file:
class OscarAffiliate < Padrino::Application
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
enable :sessions
set :delivery_method, :smtp => {
:address => "email-smtp.us-east-1.amazonaws.com",
:port => 587,
:user_name => 'AKIAIQ5YXCWFKFXFFRZA',
:password => 'AqMNMFecKSYR/TRu8kJgocysAL5SmIUsu2i8u/KAfeF/',
:authentication => :plain,
:enable_starttls_auto => true
}
But via the generation through Padrino and the Mailer generation, I do not have the recommended "sessions" controller in which this should belong:
post :create do
email(:from => "tony#reyes.com", :to => "john#smith.com", :subject => "Welcome!", :body=>"Body")
end
Am I missing something?
I have the form for a basic data collection at an office and just need an email to be sent to 5 recipients with all the form fields in the message body.
Thanks
It appears to me that you're trying to email a person (or multiple people) after a form is submitted. Possibly you're saving information from that form to a database. I think that you are a little confused on how to use Padrino mailers. Allow me to clarify: In order to send an email, using Padrino's mailer functionality, with a full body of content, you must create a Padrino Mailer (I've outlined this below). Then you must configure that mailer so that you may pass variables to it when you call it. Those variables can then be used in the view, which your mailer renders into the email body before sending the email. This is one way of accomplishing what it appears you are trying to do and it is probably the most straight-forward. You can find more information about this procedd under "Mailer Usage" on the help page you provided in your question. I've outlined an example usage, tailored to what I believe your needs are, below.
Instructions
I threw together this code sample and tested it against my AWS account; it should work in production.
In your app/app.rb file, include the following (you have already done so):
set :delivery_method, :smtp => {
:address => 'email-smtp.us-east-1.amazonaws.com',
:port => 587,
:user_name => 'SMTP_KEY_HERE',
:password => 'SMTP_SECRET_HERE',
:authentication => :plain,
:enable_starttls_auto => true
}
Then create a Mailer in app/mailers/affiliate.rb:
# Defines the mailer
DemoPadrinoMailer.mailer :affiliate do
# Action in the mailer that sends the email. The "do" part passes the data you included in the call from your controller to your mailer.
email :send_email do |name, email|
# The from address coinciding with the registered/authorized from address used on SES
from 'your-aws-sender-email#yoursite.com'
# Send the email to this person
to 'recipient-email#yoursite.com'
# Subject of the email
subject 'Affiliate email'
# This passes the data you passed to the mailer into the view
locals :name => name, :email => email
# This is the view to use to redner the email, found at app/views/mailers/affiliate/send_email.erb
render 'affiliate/send_email'
end
end
The Affiliate Mailer's send_email view should be located in app/view/mailers/affiliate/send_email.erb and look like this:
Name: <%= name %>
Email: <%= email %>
Finally, you can call your mailer from inside whatever method (and controller) you're accepting form submissions from. Be sure to replace the strings with actual form data. In this example, I used a POSTed create action, which did not save any data (thus the strings with fake data):
post :create do
# Deliver the email, pass the data in after everything else; here I pass in strings instead of something that was being saved to the database
deliver(:affiliate , :send_email, "John Doe", "john.doe#example.com")
end
I sincerely hope that this helps you in your journey with Padrino, and welcome to the Stack Overflow community!
Sincerely,
Robert Klubenspies