Laravel Send Email with HTML Elements - php

I am trying to send email using SMTP in Laravel everything is good but the html elements is coming along in the email body.
i.e.,
I am getting the below as mail
<h2>Welcome</h2><br><p>Hello user</p><br><p>Thanks</p>
Instead of
WelcomeHello userThanks
Here is my Code :
What is the thing i am missing to make it applied on the content of the email
$msg = "<h2>Welcome</h2><br><p>Hello user</p><br><p>Thanks</p>"
$message->setBody($msg);
$message->to('user#gmail.com');
$message->subject('Welcome Mail');

Try this...
$msg = "Welcome
Hello user
Thanks"
$message->setBody($msg);
$message->to('user#gmail.com');
$message->subject('Welcome Mail');

One possible solution is to wrap your HTML code in body tag.
Take a look at Laravel mail api too.

Seems like you want to send an email as HTML format:
mail($to, $subject, $message, $headers);
You will have to use the $headers parameter.
The last parameter, the headers, are optional for the function but required for sending HTML email, as this is where we are able to pass along the Content-Type declaration telling email clients to parse the email as HTML.
Source
Or, if you just want to add breaking lines and you leave HTML elements:
$msg = "Welcome \n Hello user \n Thanks"

Related

How to do newline email body in laravel?

I want to send some information on mail body. When I try its works but Google shows trimmed content(...).
My code is like this:
$name = $e->name;
$code=$request->code;
$mes2="loretmtext,";
$mes3="loremtext";
$mes4="Code:".$code;
$mes5="e:".$name;
$mes6="text"
//and i send mail function
$msg= $mes2."<br />".$mes3."<br />".$mes4."<br />".$mes5."<br />".$mes6."<br />";
How can i solve?
Need to add "Content-Type: text/html; in header mail request to use html tag <br /> in you massage
look this:
Send HTML in email via PHP

Is there a way to format the email display?

All the information are from a form. when submit the form, I use php mail() to send all the information to my email box, but I don't know how to format all the informatioin which from the submitted form.
is there a way to format the email which displays as the following style.
if using mail() can get that,how do i do? or there is another way to get that, thank you.
Don't use mail().
Instead use the phpmailer class.
http://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.phpmailer.php
http://phpmailer.worxware.com/
You call the class in your code.
require "includes/class.phpmailer.php";
Then you format your mail strings using HTML and a bit of common sense. To format the way you want you can use p and line breaks. Images, links, most of what you would think to use in html. It has worked well for me.
Tip: You don't want to include external css so your html might have to be a bit old school with text-align and setting width in using style.
edited.
call the include
require "includes/class.phpmailer.php";
then, set a few variables such as your message ... do your formatting.
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($emailAddress, "Your Reply Name"); // could be $replayemail if set
$mail->AddAddress($mailto); // who you are emailing
$mail->SetFrom($emailAddress, "Your From Name"); // from email
$mail->Subject = $subject; // keep it simple, email header
$mail->MsgHTML($message); // format fun stuff in $message
$mail->Send();
You need to learn about PHP HTML mail.
IMO best school for PHP beginners: http://www.w3schools.com/php/func_mail_mail.asp
Set the headers like this
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
mail($to,$subject,$message,$headers);
This would allow you to send html in mail.You can apply styling too.

avoid getting HTML tags in email

I wrote a PHP script for sending email. I am using a CMS which is based on PHP MySQL. I am able to send emails, and people can receive the eamils as well, but the problem is they get HTML tags in their email.
As I am writing the mail by using that CMS and for which i have to write some HTML tags there like </br>, <p> etc. The code for sending mail is below. What should I change so that people can get nice email without HTML tags, but formatted HTML mail. Thanks for helping me out.
$sentemail = mail($email_user, $subject, $message,"From: ABC<xyz#.com>\nX-Mailer: PHP/".phpversion());
you have to set the header to send html email, like that: $headers = 'Content-type: text/html; charset=utf-8' . "\r\n";, and then use the header in mail() function instead the From.. statement. Check documentation for more info http://php.net/manual/en/function.mail.php (example #4)

What e-mail header is needed for a message to be displayed as 'via:'?

I have noticed that some mails come with the from address like :
Adam Mannet via www.findyourfriend.com.
What headers do I need to pass to the native PHP mail() function to accomplish this when sending e-mail?
I think the via tag is added by the mail server when somebody uses an external (from his domain) smtp server.
Per example,
my email is iceduck#iceduck.net but I send email via Gmail's smtp, you will see From iceduck#iceduck.net via Gmail.com.
If you want to make the via appear then you'll have to send your mail through the smtp server you want to appear in the via.
You can check out this link : http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
I don't think the solution is in the header.
Best
You should check PHP mail documentation. There is a description how to modify 'From' header. You should use additional_headers to change that header information and include 'friendly name' beside your e-mail address.
<?php
// The message
$message = "Message content";
$headers = 'From: User Name <user#example.com>';
// Send
mail('test#example.com', 'My Subject', $message, $headers);
?>
This should make your from address look like: 'User Name via www.example.com'

How to get the message body from PHPMailer?

I use PHPMailer to send email via SMTP. It works, but it doesn't save the sent emails in
sent items. I want to make to sent emails in the sent items, any idea?
I know can use imap_append function to achieve it. But, how to do that?
The PHPMailer seems doesn't has the function to return the $message.
if ($return = $mail->Send()) {
$stream = imap_open("{{$host}:993/imap/ssl}Sent Items", $user_name, $user_pwd);
imap_append($stream, "{{$host}:993/imap/ssl}Sent Items" , $message , "\\Seen");
imap_close($stream);
};
How to get the message body from PHPMailer?
The instance variables from PHPMailer are all public.
Looking at the source you can just use:
$mail->Body
If you want the plain body including the all boundaries you could also use:
$mail->CreateBody();
See:
http://phpmailer.svn.sourceforge.net/viewvc/phpmailer/trunk/phpmailer/class.phpmailer.php?revision=452&view=markup
The PHPMailer seems doesn't has the function to return the $message.
Yes, it does. At least in version 5.2.28 there is a method getSentMIMEMessage() to get the full MIME message (different from the email body). That method only works after sending the email, or calling the preSend() method.

Categories