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
...
};
Related
After going through my website the user can email me a file and a brief description to go along with it. However once the user of my website clicks the submit button, he goes to a page that says "this webpage is unavailable" and I don't get an email.
I have been using PHP and HTML for this part of my website and I don't know why it isn't working.
PHP
<?php
mail('Example#gmail.com', $_POST['Subject'], $_POST['Content']);
?>
HTML
<form method="post" action="email.php">
<input type="file">
<input type="text">
Content Goes Here
<br>
<br>
<input type="Submit">
</form>
Try to use something like libmail class for sending Emails, in most cases it fixes the problem.
If even after making it work with libmail you will get an issues, try to use SMTP along with libmail.
Cheers.
Sure, here is example of usage:
Download php_libmail class with this link http://webi.ru/base/files/tovar/php_libmail_2_1.zip
Then use this code:
<?php
include "libmail.php"; // including the class
$m= new Mail; // create instance
$m->From( "asd#asd.com" ); // from
$m->To( "who#asad.com" ); // to
$m->Subject( "Subject zzz" ); // subject
$m->Body( "Hey, pal" ); // body
$m->Cc( "copy#asd.com"); // copy of email, if need
$m->Bcc( "bcopy#asd.com"); // hidden copy of email, if need
$m->Priority(3) ; // priority of message, i think from 1 to 5
$m->Attach( "asd.gif","", "image/gif" ) ; // attachment, if need
$m->smtp_on( "smtp.asd.com", "login", "password" ) ; // via SMTP, if need
$m->Send(); // And the magic Send ;)
echo "Message body:<br><pre>", $m->Get(), "</pre>";
?>
For make functionality you need, just create simple HTML form with enctype="multipart/form-data" attribute and add any fields you want, files, inputs, texts, any of those.
And then in you PHP script accept those field values via global $_POST variable and pass accepted values into the libmail instance ;)
For accepted files use global $_FILES variable.
You can simply use PHP Mailer for sending any mail.
It is very helpful and easy way to do this kind of work.
Code would be like-
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("recepient1#example.com", "Recepient Name");
$mail->addAddress("recepient1#example.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("reply#yourdomain.com", "Reply");
//CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Here is a illustrative example given for this.
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
I am new to PHPMailer and I want to send an HTML mail with this class. But I Get the message that the body is empty.
This is my code:
<?php
$bericht .= 'my html and php code that format the mail';
require_once('class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = preg_replace('/[\]/','',$bericht);
$mail->SetFrom('email#adres', 'Name');
$address = "email#adres";
$mail->AddAddress($address, "");
$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);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
What am I doing wrong?
I think there is a problem with your preg_replace(). If I try to run this on my server I get this warning:
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 3
Did you try the code without the preg_replace(), i.e., just by passing $bericht to MsgHTML()?
i think there is an error in you code
$bericht .= (my html and php code that format the mail);
should be
$bericht = 'my html and php code that format the mail';
and then instead of this
$body = $bericht;
$body = preg_replace('/[\]/','',$body);
its easier to do this
$body = preg_replace('/\[\]/','',$bericht);
I misunderstand why you need to do this INCREMENT with the variable if it has no need.
$bericht .= ...;
And yes, where's the string value within quotes right in there?
I am using PHPMailer to send HTML emails. I first set the HTML body of the email using HTML and PHP variables after first calling ob_start.
Here's my code:
<?php
ob_start();
?>
..... a bunch of VALID HTML
<?
$body = ob_get_contents();
require_once('class.phpmailer.php');
// Send to Me
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport
try {
$mail->AddReplyTo('info#example.com', 'Company Name');
$mail->AddAddress('me#example.com', 'My Name');
$mail->SetFrom('info#example.com', 'Company Name');
$mail->Subject = "Contact Form Confirmation";
$mail->AltBody = "This is a contact form submitted through the main website.";
$mail->WordWrap = 50; // set word wrap
$mail->Body = $body;
$mail->IsHTML(true); // send as HTML
$mail->Send(); // Try to send email
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// end try
For some reason, this line:
$mail->Body = $body;
causes the email not to send. If I replace it with:
$mail->Body = "This is a test email.";
the email sends perfectly.
If my HTML contained within $body is just a valid HTML page with CSS in the head, and no Javascript or anything, why won't the email send?
Is there another way to do this? Please help!!
Turns out, for some reason, the emails were not sending if they contained a fax number. Totally strange. I tested my entire markup, and after ONLY removing a 10 digit phone number, the emails finally went through. I'm not a newbie - literally that's the only text I removed and the emails sent fine.
HAS ANYONE SEEN THIS BEFORE??
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!";
}