How can you use pear mail mime with google. I found this which lets you use pear mail with google, but not mail mime: http://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Sender <*******#googlemail.com>";
$to = "Receiver <*******#googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
$host = "smtp.gmail.com";
$port = 465;
$username = "********#googlemail.com";
$password = "********";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory("smtp",array("host" => $host,
"port" => $port,
"auth" => true,
"username" => $username,
"password" => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
I keep getting
Failed to add recipient: #localhost
[SMTP: Invalid response code received
from server (code: 555, response:
5.5.2 Syntax error. f52sm5542930wes.35)]
Edit:
The email is now received, however it turns out like this:
This is a message I sent from PHP using=
the PEAR Mail package and SMTP through Gmail. Enjoy!
It looks like you have an issue with the email header. I updated your code based on the pear mail doc (http://pear.php.net/manual/en/package.mail.mail-mime.example.php):
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Sender <*******#googlemail.com>";
$to = "Receiver <*******#googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "********#googlemail.com";
$password = "********";
//$mime = new Mail_mime($crlf);
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc
$mime->setHTMLBody($html);
//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);
$smtp = Mail::factory("smtp",array("host" => $host,
"port" => $port,
"auth" => true,
"username" => $username,
"password" => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
It works for me so I hope it will work for you!
Cheers,
Erez
#john: Using the code from the link you posted, modify it like so --
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
$from = 'Sender <sender#gmail.com>';
$to = 'Receiver <receiver#something.com>';
$subject = 'Sent from PHP on my machine';
$text = 'This is a message I sent from PHP '
. 'using the PEAR Mail package and SMTP through Gmail. Enjoy!';
$message = new Mail_mime();
$message->setTXTBody(strip_tags($text)); // for plain-text
$message->setHTMLBody($text);
$body = $message->get();
$host = 'smtp.gmail.com';
$port = 587; //According to Google you need to use 465 or 587
$username = 'sender';
$password = 'your_password';
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
?>
Update:
Edit:
The email is now received, however it turns out like this:
This is a message I sent from PHP using=
the PEAR Mail package and SMTP through Gmail. Enjoy!
#john: Update
$body = $mime->get();
to
$body = $mime->get(array('text_charset' => 'utf-8'));
and try again.
$body = $mime->get(array('text_charset' => 'utf-8'));
In addition to the above you need html_charset for html emails.
$crlf = "\n";
$body = $mime->get(array('html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'eol' => $crlf));
This will fix the abberations like  in the e-mails.
Couldn't comment on StealthyNinja's answer so I posted my own, sorry about that.
The question is also a bit old but I maybe this could be useful to others.
To get rid of all that HTML tags and weird characters you have to prepare your header so the e-mail client can read the e-mail right. Try this AFTER setting your $headers array:
$headers = $message->headers($headers);
It should work ok after that.
I have used this code to remove 3D after = sign.
$hdrs = array( 'From' => $from,
'To' => $to,
'Subject' => $subject );
$mime =& new Mail_mime();
$mime->setTXTBody($message);
if($htmlMessage==""){
$htmlMessage=$message;
}
$mime->setHTMLBody($htmlMessage);
if($attachmentIsFile){
if($attachment!=null)
$mime->addAttachment($attachment,'application/octet-stream',$attachmentName.extractExtension($attachment));
}else{
if($attachment!="")
$mime->addAttachment($attachment,'application/octet-stream',$attachmentName,false);
}
$body = $mime->get(array('text_encoding' => '8bit','html_encoding' => '8bit'));
$hdrs = $mime->headers($hdrs);
Related
I have read the similar posts about this problem but can't quite get my head around what I am doing wrong here. The 'To' email is received ok.
However, I can not get the 'CC' email to send when using the following script.
Any help would be much appreciated.
// Require Pear Mail Packages
require_once ("Mail.php");
require_once ("Mail/mime.php");
require_once ("Mail/mail.php");
$crlf = "\n";
$from = "Company <admin#example.com>";
$to = $purchasersName . "<" . $purchasersEmail . ">";
$cc = "Company <admin#example.com>";
$subject = "Company Order Confirmation";
$host = "localhost";
$port = "25";
$username = "the username";
$password = "the password";
$hdrs = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($mailBody);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$smtp =& Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $hdrs, $body);
This comment is in the pear docs on this page
http://pear.php.net/manual/en/package.mail.mail.send.php
(If you want other options, you could try http://swiftmailer.org, I have used that on lots of projects now)
Note by: arminfrey#gmail.com 2007-07-08 05:13 UTC In order to send
e-mail to cc or bcc with smtp you have to list the cc e-mail address
both as a recipient (which decides where the e-mail is sent) and in
the cc header, which tells the mail client how to display it.
Here is the code I use:
$to = 'to#example.com'; $cc = 'cc#example.com';
$recipients = $to.", ".$cc;
$headers['From'] = 'from#example.com'; $headers['To'] = $to;
$headers['Subject'] = 'Test message'; $headers['Cc'] =
'cc#example.com'; $headers['Reply-To'] = 'from#example.com';
$send = $mail->send($recipients, $headers, $body);
I am trying to using Pear mail on my hostgator server. Firstly I've installed the mail package using cpanel and net_smpt was already installed, however when I required the Mail.php class it insisted that the Net folder be within the Mail folder - they were both at the same level before. So I've moved Net inside Mail and it stopped complaining - but maybe I've just messed up everything (maybe not: let me know).
This is my test script, mostly copied from tutorials:
<?php
echo 'Test Pear<br>';
require_once("/home3/myaccountname/php/Mail.php");
$from = "info#mydomain.com";
$to = "myrealemail#gmail.com";
$subject = "Test pear";
$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit...";
$params['host'] = 'gator3015.hostgator.com';
$params['port'] = '465';
$params['username'] = 'info#mydomain.com';
$params['password'] = 'mypassword';
$params['debug'] = 'true';
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', $params);
echo 'good so far';
$mail = $smtp->send($to, $headers, $body);
echo 'Still there';
if ( PEAR::isError($mail) ) {
echo("<p>Error sending mail:<br/>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message sent.</p>");
}
?>
The SMTP parameters have been tested using the codeigniter email class, where they succesfully send emails (but as cronjobs and codeigniter refuse to work for me I'm trying to use Pear Mail instead). It requires SSL but apparently it's on by default with Pear Mail.
I've made sure the PHP error messages were displayed for my test. What happens is that I see all my echos except for "Still there" so something goes wrong when I call $smtp->send(). However no error message is displayed, despite setting debug as true. The if(PEAR::isError($mail)) doesn't seem to have any effect either. And no email is sent.
So I'm a bit at a loss to know what's going wrong. Any idea?
UPDATE: the host does not support SMTP for some reason, which explains why I had the weird issue. Using Mandrill works fine.
Try this example,
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Also this one,
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
I'm having problems sending a mail through php. I've already set in php.ini SMTP:
SMTP = xx.xxx.xxx.xx
smtp_port = 25
And I'm in php to send an email with the following code:
// Set up parameters
$to = "xpto.87#gmail.com";
$subject = "Title";
$message = "Hello world";
// Send email
$mail = mail($to,$subject,$message);
// Inform the user
if($mail == true)
echo "send mail";
else
echo "dont send";
What do I get and always a "dont send", and i dont know why. Anybody can help me please?
I have succesfully sent email from PHP over GMAIL using following code:
$from = "who";
$to = "to";
$subject = "subject";
$host = "ssl://smtp.gmail.com";
$port = 465;
$username = "yourusername";
$password = "yourpass";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp',array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
Tell me if it works in your case.
I'm trying to figure out how send email from php using google's SMTP servers, but change the FROM header to match my domain. The email sends fine, but the recipient sees it sent from myemail#gmail.com, rather than me#mydomain.com. I'm using php pear mail to send.
require_once("Mail.php");
$from = "Me <me#mydomain.com>";
$to = "Zach <myemail#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "****";
$password = "****";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
You must set a ssl encryption. Google can help you http://www.google.com/search?q=pear+mail+ssl
I've been able to send an email using SMTP in PHP, but when I try to change the Content Type to HTML, the email doesn't get delivered. This is the code I'm trying to use:
require_once "Mail.php";
$from = "FPM <forms#fpmofames.com>";
$from_name = "FPM";
$host = "localhost";
$username = "username";
$password = "password";
$subject = "Subject";
$message = "Message";
$to = "<example#example.com>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => '1.0',
'Content-Type' => "text/html; charset=ISO-8859-1"
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $message);
If I take the 'Content-Type' argument out of the headers, it sends the message just fine. I don't know why adding that causes a problem.
I did some research, then I made my own code to send mail with HTML formatting using SMTP authentication. See here:
<?php
require_once "Mail.php";
$url = $_GET['baseUrl']; // source url
$success = false;
$senderName = isset( $_GET['txtname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_GET['txtname'] ) : "";
$senderEmail = isset( $_GET['txtemail'] ) ? preg_replace( "/[^\.\-\_\#a-zA-Z0-9]/", "", $_GET['txtemail'] ) : "";
$msg = isset( $_GET['txtDesc'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_GET['txtDesc'] ) : "";
$body = '<table width="400" border="0">
<tr>
<th scope="col">Name:</th>
<th scope="col">'.$senderName.'</th>
</tr>
<tr>
<th scope="col">Company:</th>
<td scope="col">'.$_GET['txtCompany'].'</td>
</tr>
<tr>
<th scope="row">Phone:</th>
<td>'.$_GET['txtphone'].'</td>
</tr>
<tr>
<th scope="row">E-mail:</th>
<td>'.$senderEmail.'</td>
</tr>
<tr>
<th scope="row">URL:</th>
<td>'.$url.'</td>
</tr>
<tr>
<th scope="row">Massage:</th>
<td>'.$msg.'</td>
</tr>
</table>';
$from = $senderName."<".$senderEmail.">";
$to = "Contact ManagerHR<info#aequitas-infotech.com>";
$subject = "Hi!";
$host = "XXX.host.com";
$username = "username#host.com";
$password = "*****";
/* MIME-Version should be "1.0rn" and Content-Type should be "text/html; charset=ISO-8859-1rn" to send an HTML Email */
$headers = array ('MIME-Version' => '1.0rn',
'Content-Type' => "text/html; charset=ISO-8859-1rn",
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
header('Location: '.$url); // redirect to url where from inquiry made
//echo("<p>Message successfully sent!</p>");
}
?>
The problem most likely lies in the Mail class, but since we don't know what Mail class you're using, it's difficult to answer. If you're not already doing so, I'd really think about using PHPMailer: https://github.com/PHPMailer/PHPMailer
You should create the mail body via mime object. And pear will handle it from there.
Ex:
$crlf = "\n";
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
...
$body = $mime->get();
Send your HTML email using SMTP in PHP to 2 or 3 different emails
WORKING 100%
<?php
require_once "Mail.php";
$host = "ssl://smtp.gmail.com";
$username = "example#gmail.com";
$password = "password";
$port = "465";
$to = '1-to-example#example.com' . ', ';
$to = '2-to-example#example.com' . ', ';
$to = '3-to-example#example.com' . ', ';
$email_from = "from-example#example.com";
$email_subject = "Your Subject";
$email_body = "**<html> <body>**";
$email_body .= "<strong> Your HTML code </strong>";
$email_body .= "**</body></html>**";
$headers = array ('From' => $email_from, 'To' => $to, 'Subject' =>
$email_subject, 'Reply-To' => $email_address , 'MIME-Version' => '1.0', 'Content-Type' => "text/html; charset=ISO-8859-1");
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);
?>
Write content type in a first line of header just like this
$to = "<example#example.com>"; $headers = array ('Content-Type' => "text/html; charset=ISO-8859-1", 'From' => $from, 'To' => $to, 'Subject' => $subject, 'MIME-Version' => '1.0' );
This is working for me..
You can complete in two steps.
Step 1: Put code in your file:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Step:2 Download this included file in given URL.
Url: https://github.com/PHPMailer/PHPMailer.git
and click on clone or download button
and set folder your system.