php pear mail cant send the html formatted mail - php

Hi here my mail is working fine, but i dont know how to send a html formatted mail, because i am using pear mail!here i am using the code to send mail using php , first calling the mail.php and mime.php. $to mention which mail id you want to send a mail, to send a mail you need specify email id and password of email which is the sender mail.
<?php
if(isset($_POST['submit']))
{
require_once "Mail.php";//calling mail function
require_once 'Mail/mimePart.php';
$from = "you#localhost.com";
$to = "you#gmail.com";//your mail id
$CompanyName = $_POST['CompanyName'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$paddress = $_POST['paddress'];
$subject = 'MOSONS Group';
$message = 'PersonName: ' .$CompanyName. "\n";
$message .= 'Mobile: ' .$mobile. "\n";
$message .= 'Email: ' .$email. "\n";
$message .= 'Message: ' .$paddress. "\n";
$host="ssl://smtp.gmail.com";
$port="465";
$username="you#gmail.com";// your email id
$password="yourpassword"; //password
$mime= "MIME-Version: 1.0\r\n";
$type= "Content-type: text/html\r\n";
$headers = array ('From' => $from,'To' => $to ,'Subject'=>
$subject,'Mime'=> $mime,'Contenttype'=> $type);
$smtp =# Mail::factory('smtp',array ('host'=>$host,'port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password));
$mail = #$smtp-> send($to,$headers,$message);
if(#PEAR::isError($mail)){
echo ("<p>" .$mail->getmessage(). "</p>");
}
else
{
echo '<script type="text/javascript">alert("Thank You for Contacting us, our team will get in touch with you soon.!"); </script>';
}
}
?>

You are setting your headers incorrectly. e.g you're doing stuff like
'Contenttype'=> 'Content-type: text/html';
It should be just
'Content-Type' => 'text/html'
PEAR::Mail is a horribly crappy package. You should switch to something better, like PHPMailer or Swiftmailer.

Related

Send message via php

I have problem with sending messages via php from host email to someone email with script. Like for registration code etc...
Here is my code:
$to = "$email";
$from = "support#webiste.com";
$subject = 'Web Site';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>WebSite</title></head><body style="padding: 20px;"><h1>WEBSITE</h1><p>Hello '.$name.' '.$lname.'</p><h3>Click the link below to activate your account: </h3><p><a style="text-decoration: none;" href="http://www.website.com/MB/activation.php?id='.$rewr.'&re='.$re.'">Click Here</a></p></body></html>';
$headers = "From: $from\n";
$headers = "MIME-Version: 1.0\n";
$headers = "Content-type: text/html; charset=iso-8859-1\n";
if(mail($to, $subject, $message, $headers)){
echo "Sent";}else echo "failed";
I got failed message every time
sendmail is server dependent a better solution would be to use Pear mail.
include_once 'Mail.php';
include_once 'Mail/mime.php' ;
$recipients = '<' . $this->to . '>';
$text = $this->message['text'];
$html = $this->message['html'];
$crlf = "\n";
$hdrs = array('From' => $this->from,'To' => $this->to,'Subject' => $this->subject);
$mime = new Mail_mime(array('eol' => $crlf));
if(!empty($text)){$mime->setTXTBody($text);}
if(!empty($html)){$mime->setHTMLBody($html);}
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$params["host"] = MailSMTP;
$params["port"] = MailPort;
$params["auth"] = true;
$params["username"] = $username;
$params["password"] = $password;
$mail =& Mail::factory('smtp',$params);
$value = $mail->send($recipients,$hdrs, $body);
if(PEAR::isError($value)) {
return false;
}
Above is code that I use myself. It will send multi-part mail ie HTML and plain text so they can read it on any device and it uses Pear mail and also send to 1 or multiple recipients. Pear is an extension of PHP.

PHP send mail to multiple recipients without sharing email addresses

I am trying to send email to multiple recipients using PHP. My code is like this
<?php
foreach($subscribers as $subscriber){
$email[] = $subscriber['email'];
}
$emails = implode(',', $email);
$email_from = "email#example.com";
$subject = "My Subject";
$full_name = 'Example Sender';
$from_mail = $full_name.'<'.$email_from.'>';
$email_text = file_get_contents('email.html');
$headers .= 'From: ' . $from_mail . "\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
mail($emails, $subject, $email_text, $headers);
?>
I am able to send emails correctly. But it shows others email addresses in there email box and I don't want to share email addresses with each other. Please help how can I do this. I tried to send email inside the for each loop like this
<?php
foreach($subscribers as $subscriber){
$email = $subscriber['email'];
$email_from = "email#example.com";
$subject = "My Subject";
$full_name = 'Example Sender';
$from_mail = $full_name.'<'.$email_from.'>';
$email_text = file_get_contents('email.html');
$headers .= 'From: ' . $from_mail . "\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
mail($email, $subject, $email_text, $headers);
}
?>
But it sends email to only the first email address inside loop. Please help.
You can add an empty string in the $email field and use bcc headers to add the mail addresses. bcc makes sure the receiver can't see the other email addresses.
$headers .= "Bcc:email#example.com" . PHP_EOL;

Php mail. Can't get email on one address

I have a problem with my mail in php. I code form to send email. I receive email on gmail but I have other mail address and I can't get email on it.
I checked in spam and there is no email also.
Below is my code.
<?php
$emailErr = "";
$endMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Proszę uzupełnić pole e-mail";
}
else if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$namesurname = $_REQUEST['name_surname'] ;
$email = $_REQUEST['email'] ;
$number = $_REQUEST['number'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$message = $subject . ": " . $message . " " . $number . " " . $namesurname . " " . $email;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
mail("szafor#szafor.pl", "Zamówienie pomiaru",
$message, "From: formularz#szafortest.pl \r\n"."Content-Type: text/plain; charset=UTF-8\r\n");
$endMessage = "Dziękuję za przesłanie wiadomości.";
}
}
?>
One important thing to consider with sending mail is that you should at least have the return path of the message be an email address that is actually hosted on the server that you are sending from.
You can set the From and the Reply-To address as any address, but the return-path should be set to a valid email address hosted on your server. Let's say that you want the "reply" button to send back to "this_email#wherever.com" but the server you are using hosts email for "mydomain.com". Create an email account on your server, "info#mydomain.com" for example.
$recipient = "sendto#email.com";
$subject = "Test email";
$message = "This is the message.";
$headers .= "From: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Reply-To: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Return-Path: Your Name Here <info#mydomain.com>\n\r";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .="X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .="MIME-Version: 1.0\r\n";
mail($recipient, $subject, $message, $headers);
I have found that the more valid header information that I provide, the more likely the email will be delivered. Right now these headers always work for me, and I have a scheduling program that is sending email to a hundred different email addresses every day. See if that works better for you.

Warning: mail() [function.mail]: SMTP server response: 550 Invalid recipient:

I'm creating a 'forgot password' page where the user enters their email address and the script finds the password associated to that email, and sends it to the stored email address.
I believe the problem has to do with my SMTP Mailserver. I am using WAMP which doesn't have one so I downloaded one that was free.
This is the php script I'm using:
$id = checkEmail($email);
$record = readMemberRecord($id);
$password = #mysql_result($record,0,'cred');
if($password){
$email_subject = "Email Request";
$email_body = "Your password is ".$password.".";
$header = "NinjaMan";
mail($email, $email_subject, $email_body,$header);
$msg = "Your password has been sent to ".$email.".";
}else{
$msg = "Sorry, the email ".$email." wasn't found.";
}
The $msg outputs properly so I know the code is passing the mail function.
Try sending in a proper "From" in $header.
$emailFrom = "admin#yourdomain.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Email Request";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Your password is ".$password.".";
mail($email, $subject, $message, $headers);
See details of the mail() function.
If this doesn't work, try using PHPMailer. You configure it in code, no need to edit php.ini.
I've used it in some projects (v 2.0.4, I see the latest is 5.1) and had no problems.
Try using Google's server to send mails, you can see how to do that here
Try using this
//Email information
$to = "garggarima#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "support#sltechsoft.com";
$headers = "From:" . $from;
$mail=mail($to,$subject,$message,$headers);
if($mail) {
echo "Thanks for mail";
} else {
echo "Mail not Sent";
}
//Email response
echo "Thank you for contacting us!";

Sending HTML email, via PHP form

I am trying to send my website visitors and email with some directions and tips before they show up to my studio via PHP form mailer. ( i am simplifying some of the form fields )
However the HTML formatting is not working correctly.... did i not declare the mime type of charset correctly?
<?php
if (isset($_POST['submit'])) {
//if (empty ($_POST['name']) || empty($_POST['email']))
//{
//echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information. <a class='close' href='#'>close</a></div>";
//}
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
//A bunch of other fields are here
//Additional Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//$yoursite = "My Site";
//$youremail = $email;
$subject = "Website Form";
$message = "
$name would like you to contact them about your service.
Contact PH: $phone
Email: $email
Legal Guardian: $legal
//more stuff here
";
$subject2 = "Directions and Information";
$message2 = "<html><head></head><body>
$message2 .= "<h1>Directions</h1>
<p>text</p>
<p><a href='http://example.com/schedules'>Click here</a>
<h2>How Do I find your Photo Studio?</h2>
<h2>What do I have to bring with me?</h2>
</p>";
$message2 .= "</body></html>";
$email3 = "me#mysite.com";
$email4 = "mysite#gmail.com";
//This email sends their details to me from the visitor
mail($email3, $subject, $message, "From: $email");
//This email sends directions to the visitor from me
mail($email, $subject2, $message2, "From: $email4");
echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";
}
?>
There is a lot of random junk that needs to be done to an email to send correctly. I generally tend to outsource all that responsibility to a pre-packaged class that exists in the wild, something like http://swiftmailer.org/
Maybe someone else would have a better class to offer.
I swear by the PEAR Mail_Mime package. It's simple and powerful.
PEAR: Mail_Mime
//Basic mail headers
$headers['To'] = "test#domain.com";
$headers['From'] = "sender#domain.com";
$headers['Subject'] = "Test";
//Set up the mail module
$mime = new Mail_mime("\r\n");
$mime->setTXTBody("This is a test");
$mime->setHTMLBody("<p>This is a test.</p>");
$body = $mime->get();
$headers = $mime->headers($headers);
//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25));
$mail_obj->send($headers['To'], $headers, $body);

Categories