Sending PHP emails with HTML/CSS - php

I'm trying to send emails with PHP. I would like to add some style with CSS (no matter if inline, internal or with external file).
I've tried PHPmailer, but it fails to recognize some elements (such as body), media queries (#media) and declarations (max-width, inline-block, etc...). I'm now trying to use SwiftMailer, but online documentation doesn't mention anything about styling.
Just for sake of clarity, here's the snippet I would like to use: JsFiddle
Any ideas to send PHP emails with working HTML/CSS?

You could use a CSS inliner tool like http://templates.mailchimp.com/resources/inline-css/
to convert your 'normal' template (made like a normal HTML page) to an email-friendly template. This is needed because mail clients doesn't recognise separate elements.
I currently use a template based on the ones made available by Zurb, http://zurb.com/ink/

It also depends on the receiver's email client. Not all clients accept all css. Use a litmus test of some sort to check with different e-mail clients which css you can use. Tables often work with email clients, a lot of them don't accept divs I believe.

I use PHPMailer to send emails with PHP, and it never fails to recognize these elements (for me anyway).
To use it, i create a fonction :
function sendMail($name, $from, $message, $to, $subject)
{
$mail = new PHPMailer();
$body = "<html><head></head><body style=\"background-color: #F2F1F0;\"><p>".$message."</p></body></html>";
$mail->SetFrom($from, $name);
$mail->AddReplyTo($from, $name);
// Only if you want to send an attachment, send a variable $object to the function
//$mail->AddAttachment($object);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($to, $name);
if(!$mail->Send()) {
$result = $mail->ErrorInfo;
}
else
{
$result = "Mail sent successfully";
}
return $result;
}
And an exemple to use it :
// Preparation of the message
$message = '<p class="img"><img src="http://mywebsite.com/img/myImage.png" width="220px"></p>';
$message .= '<p class="txt">Hello Mr Dupond !</p>';
$message .= '<p class="txt">We are glad to have you among us</p>';
$message .= '<p class="txt">Se you soon on our Website !</p>';
$message = utf8_decode($message);
// variables needed
$name = "Mr Oktopuss";
$from = "contact#oktopuss.eu";
$to = "contactAddress#domain.ext";
$subject = "The subject of this mail !";
// Send the mail and receive the result
$result = sendMail($name, $from, $message, $to, $subject);
echo $result;
Maybe that could be help you

Related

add e-mail recipient into php script

CC or forward is not possible to set on mailserver for authorisation mail sent by Joomla itself, but we'd like to store these e-mails.
Question is: how to set it in php of specific plugin? (plugin is sending these e-mails)
code:
// send auth email to user who signed ...
if ($signature_verification = (int)$this->settings->get('security.signature_verification', 0)) {
// unpublished, visitor must verify it first
$this->db->set('published', 0);
$config = JFactory::getConfig();
$from = $config->get('mailfrom', '');
$fromname = $config->get('fromname', '');
$recipient = (string)$this->db->get('email', '');
When I replace last line wth: $recipient = ('my#email.com'), then I get that message, but i want one for visitor and copy for me.
Thanks for advice
OK, actually this piece of code initiates sending of that mail:
if (
$this->sendMail(
$from,
$fromname,
$recipient,
$subject,
$body
) !== true
) {
throw new phpmailerException(JText::_('PLG_CONTENT_CDPETITIONS_EMAIL_SEND_FAILED'), 500);
}
When I make copy of that code, paste it below, and replace $recipient with my e-mail, it works: I have the same message delivered on both adresses. But I need it have it like CC (carbon copy) and have original recipient adress in header of mail, which is delivered to me.
Use the built in mailer methods for Joomla:
$msg = "This is my email message.";
$subject = "Database Update Email";
$to = (string)$this->db->get('email');
$config = JFactory::getConfig();
$fromemail = $config->get('mailfrom');
$fromname = $config->get('fromname');
$from = array($fromemail,$fromname);
$mailer = JFactory::getMailer();
$mailer->setSender($from);
$mailer->addRecipient($to);
$mailer->addRecipient('you1#yourdomain.com');
$mailer->addRecipient('you2#yourdomain.com');
$mailer->addCC('you3#yourdomain.com');
$mailer->addBCC('you4#yourdomain.com');
$mailer->setSubject($subject);
$mailer->setBody($msg);
$mailer->isHTML();
$mailer->send();
This should use PHP to send an HTML email to whomever you want and copy other users on the email through either direct send, CC, or BCC depending on which method you use.

Send different email content with $mail php

I have a small problem here that I'm tried to fix for some couple of hours already. I have a PHP $mail function that send emails to multiple addresses using PHP for loop, like:
foreach($recipients as $name => $email){
$mail->AddAddress($email, $name);
};
Problem is:
I need to get inside the Body message, each email from each user at the time of sending the message, so I can use it as variable inside the body message, I've already tried to use another foreach inside, but no luck.
So the PHP will be something like this:
$mail->Body .='Start of the message';
$mail->Body .='path_url?user_email='.$recipient.'';
$mail->Body .='Endof the message';
Is this possible using $mail?
Thanks!
You should use the same for loop for both email addresses and your content.
With out knowing your code in details, I assume you have array contenting recipients email, name and message or something else.
Some thing like this with out testing it:
foreach ($recipients as $value)
{
$mail->AddAddress($value['email'], $value['name']);
$mail->Body .= 'Start of the message';
$mail->Body .= 'path_url?user_email=' . $value['recipient'];
$mail->Body .= 'Endof the message';
...
the rest of your $mail code if there so
...
};

Joomla mail send() senfing HTML Markup without rendering

I am trying to send Emails from Joomla 2.5
$subject = "Test Subject";
$mail = JFactory::getMailer();
$mail->addRecipient($contact->email_to);
$mail->addReplyTo(array($email, $name));
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($sitename . ': ' . $subject);
$mail->setBody($body);
echo $body;
$sent = $mail->Send();
Variable $body outputs the rendered HTML but when receiving Email I get un-rendered Email.
isHTML as you can probably gather means theemail will be be sent in HTML mode. You should also set the encoding type to base64 which will avoid unwanted characters, as shown below:
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
So add the above code, right before $mail->setBody($body);.
Hope this helps

How to instantiate mail() function and send an email with Joomla2.5?

Based on the Joomla! documentation # http://docs.joomla.org/Sending_email_from_extensions, I'm trying to send emails with the code below:
function sendmail($file,$mailto)
{
$mailer =& JFactory::getMailer();
//var_dump($mailer); exit;
$config =&JFactory::getConfig();
$sender = array(
$config->getValue( 'config.mailfrom' ),
$config->getValue( 'config.fromname' )
);
$mailer->setSender($sender);
$recipient = array($mailto);
$mailer->addRecipient($recipient);
$body = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
$mailer->setSubject('Your subject string');
$mailer->setBody($body);
// Optional file attached
$mailer->addAttachment(JPATH_BASE.DS.'CSV'.DS.$file);
$send =&$mailer->Send();
if ( $send !== true ) {
echo 'Error sending email: ' . $send->message;
} else {
echo 'Mail sent';
}
}
($file is the full path of a file zip and $mailto is a my gmail.)
However, when I send mail, I receive the error:
Could not instantiate mail function.
Fatal error: Cannot access protected property JException::$message in /var/www/html/dai/components/com_servicemanager/views/i0602/view.html.php on line 142
What is causing this error?
Please save yourself some sanity and do not try to use Joomla's mailer implementation. Not only is it unreliable as you've experienced, it handles different charsets and HTML content poorly. Just include and use PHPMailer.
Change
echo 'Error sending email: ' . $send->message;
to
echo 'Error sending email:'.$send->get('message');
then run your code again. The error that you get should tell us why it isn't instantiating.
In joomla send a mail with attachment file
$from="noreplay#gmail.com";//Please set Proper email id
$fromname="noreplay";
$to ='admin#gmail.com';
// Set a you want send email to
$subject = "Download";
$message = "Thank you For Downloading";
$attachment = JPATH_BASE.'/media/demo.pdf';
// set a file path
$res = JFactory::getMailer()->sendMail($from, $fromname, $to,$subject, $message,$mode=1,$cc = null, $bcc = null, $attachment);
if($res)
{
$errormsg = "Mail Successfully Send";
}
else
{
$errormsg ="Mail Not Send";
}
after you have check mail in your inbox or spam folder.
mail in spam folder because not proper set email id in from id.
After several years of Joomla development, I recommend using RSFORM PRO by RSJOOMLA to send mail for you after the visitor to your website fills out the form. It is much easier to manage than having to deal with the internal mail server.

I need to attach a file to an outbound email automatically...Help

I have one form (input.html) that's completed by people working in five different divisions of this business. When the form is submitted it posts to output.php.
Output.php does a number of things:
First it displays all of the input information to the screen as a completed form. (Just to show them their completed document).
It has also created a file called unique_file.html based on two of the input fields in input.html.
Next, output.php has sent an email to one of five email groups, based upon another input field selected in input.html.
Finally (for now), I need the unique_file.html to be an attachment to the email. That is my problem.
I have found scripts for uploading files, I have found many tutorials about uploading files, but I just want to attach the unique_file.html to my outgoing email and I am not seeing how that is done.
Can someone point me in the right direction as to where to start? I am certainly missing the boat on this one and I have probably seen it and not realized it.
If you can use the Zend_Framework you can easily attach file to an email i.e.
$mail = new Zend_Mail();
$at = new Zend_Mime_Part($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->addAttachment($at);
$mail->send();
see the documentation,
You should be able to attach a file also by using Mail_Mime pear package and the normal mail function.
But I think the solution using the Zend framework is much more straight forward.
Cheers.
I find PHPMailer best for this. An example from their site:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Categories