Missing attachement in mail sent through Mime / SMTP Gmail Server (PHP, PEAR) - php

I am sending an Email from my server through SMTP Gmail using Pear's Mail Mime. However when I add an attachement it simply does not show up.
$smtpinfo["host"] = "ssl://smtp.gmail.com";
$smtpinfo["port"] = "465";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "xxx";
$smtpinfo["password"] = "xxx";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => utf8_decode($subject)
);
$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->addAttachment("http://ww.url.of.a.file.that.exists.100percent.jpg", "image/jpeg");
$body = $mime->get(array('html_charset' => 'utf-8','charset' => 'utf-8'));
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', $smtpinfo);
$mail = $smtp->send($to, $headers, $body);
Everything works fine just the attachement is entirely missing..
I've been googling for hours.. I appreciate any hints..

My first thoughts would be to check the [boolean] response of the addAttachment method to see if it returns 'not found' or other type of indication(s)
$fileAttached = $mime->addAttachment("http://ww.url.of.a.file.that.exists.100percent.jpg", "image/jpeg");
echo ( !empty($fileAttached) ) ? "Attached successfully!" : "Uh, Houston?";
My initial thought is that it's expecting that 'file' to be 'loca' to your system and not accessed via http, etc. And, even if it DOES allow for HTTP access, you might also want to check your allow_url_fopen in the .ini file to insure it's set to 'enabled' {"On" if looking at your phpinfo() result.
Additional information on the 'file' -
http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

Related

php pear mail not modifying 'from' when using google apps gmail

I have google apps setup for one of my site's emails, thus making the site's built-in email routing useless when doing things from the web. What i need to do , is have it use ssl/smtp to connect to the google apps setup.
To accomplish this, I've used PEAR Mail and mime (for the HTML contents).
The messages get sent without an issue... the ONLY problem i'm having, is that the 'From' header isn't being saved across transmission. Instead, the account email is in the 'from' header.
The accounts exist on the webserver's end (which means nothing since its all going through google), and i've added aliases to my gmail apps administration end. But no matter what i do, its not changing the 'from'.
Is this just something i'm going to run into when using a single account with google apps' gmail? (--forced 'from' from the account name?)
Thanks
--for those who were wondering, here is an example function for the mail sending:
function pearMail($from, $fromTitle, $to, $subject, $text, $html)
{
require_once "Mail.php";
require_once('Mail/mime.php');
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "name#domain.com";
$password = "PASSWORD";
$headers = array ('From' => $from,
'Return-Path' => '-do not reply-',
'To' => $to,
'Subject' => $subject);
$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();
$headers = $mime->headers($headers,true);
// Sending the email
$mail =& Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}
See the google help on changing From::
The custom 'From:' feature works only if you already own the account linked to the alternate address. To send mail with a different Gmail username, you must first sign up for that address.

pear fatal Error: Class 'Mail' not found .wamp

I am having some trouble with using the send mail functionality of pear with wamp. I went through with the steps in this link : (http://pear.php.net/manual/en/installation.checking.php) to check if my pear was installed correctly, and it seems like I have it right.
<?php
require_once 'System.php';
var_dump(class_exists('System', false));
?>
The code above returns bool(true). So I am assuming that my paths are set right.
But for the code below I am getting an error.
<?php
include 'Mail.php';
include 'Mail/mime.php' ;
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'test.xls';
$crlf = "\n";
$hdrs = array(
'From' => 'myemail#gmail.com',
'Subject' => 'Test mime message'
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('myemail2#gmail.com', $hdrs, $body);
?>
Error is on this line : $mail =& Mail::factory('mail'); . Fatal error: Class 'Mail' not found
Also, I installed pear Mail with this command : pear install Mail Mail_mime
I would appreciate any help.
Thanks,
This one works for me, try this way
function sendEmail($subject,$from,$to,$bodymsg,$cc=null)
{
require_once "Mail.php";
require_once "Mail/mime.php";
$crlf = "\n";
$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 = "myusername"; //<> give errors
$password = "mypass";
//$mime = new Mail_mime($crlf);
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc
$mime->setHTMLBody($bodymsg);
//$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), '-f bounce#domain.com');
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n
}
You need to either specify the full path to the PEAR Mail package that you've installed (instead of include 'Mail.php';), or include that path in php.ini's include_path.
Also update php.ini with your mail server's address and port... Since you are using Mail's send driver "mail", which is PHP's mail() function. Though you can specify it to use sendmail or an SMTP server instead.

SMTP email via PHP - through Send Grid

I am sending SMTP email from my PHP website, using SendGrid SMTP mail service. Everything works fine, but when I send large amounts (5,000), it takes a long time to load/send, then tries to restart over and over again, and doubles, triples,.... I just sent 30,000 emails by mistake! I need a way to see the mail que and delete them or stop the process when it does this.
How can I see the que of emails to be sent out? Or cancel them from being sent out?
Maybe through Pearl Mail?
Here is my code
require_once "Mail.php";
$from = $thom5;
$to = $allemailsever1.','.$thom5;
$subject = $_POST['subject1'];
$html = "
<html><body>
<p></p>
$thom
</body></html>
";
$host = "smtp.sendgrid.net";
$port = "587";
$username5 = "";
$password5 = "";
$mime = '1.0';
$content = 'text/html';
$charset="ISO-8859-1";
$three = 'Receipients <'.$thom5.'>';
$headers = array ('From' => $from,
'To' => $three,
'Subject' => $subject,
'Mime-version' => $mime,
'Content-Type' => $content,
'charset' => $charset);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username5,
'password' => $password5));
// Send notification //
$mail = $smtp->send($to, $headers, $html);
Sounds like your local script is having trouble managing the number of mails you are trying to send. You should implement a local queue, and since you are using PHP, a simple option is the Mail_Queue PEAR package
Use the Events API and check to see that each message is processed. http://docs.sendgrid.com/documentation/api/event-api/

How to send an email using php script?

<?php
require_once "Mail.php";
$from = "<niko#gmail.com>";
$to = "<niko#hotmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<niko#gmail.com>";
$password = "somepassworrd";
$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>");
?>
When I try to execute these php script I get these error
FATAL ERROR Class Mail NOT found ON number line 18
I know the above question is possible duplicate of these
Send email using the GMail SMTP server from a PHP page
But its not working.My PHP version is 5.3.4 Xampp 1.7.4 version.
mail('caffeinated#example.com', 'My Subject', $message); \\ I tried these
But it shows a warning saying that missing headers.
How do I send an email Using the php script?
Can we send an email without using authentication in PHP ? Because vb.net uses server authentication but most of the code i found on google is without authentication for php. so I got a doubt
Finally please help me with these trying from an 2 hours or so!
The PHP mail() function is for sending mail via Sendmail. If you want to use some SMTP server, you can use Zend_Mail which makes this thing very easy:
http://framework.zend.com/manual/en/zend.mail.html
Using Zend_Mail you only need to write something like this:
$config = array('auth' => 'login',
'username' => 'myusername',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender#test.com', 'Some Sender');
$mail->addTo('recipient#test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
The above handles authentication for you and sends a mail.

How to explicitly disable TLS with PEAR Mail factory?

Using PHP, I'm attempting to route email through AuthSMTP (a hosted SMTP service). The problem is that the PEAR mail factory automatically tries to negotiation a TLS connection with the server. Rather than simply ignoring the attempt, AuthSMTP throws an error. I need a way to explicitly tell the Mailer class not to try to use TLS. Any suggestions?
$from = "Example <noreply#example.com>";
$to = $email;
$subject = "This is an email";
$body_text = "plain text here";
$body_html = "<h1>HTML here!</h1>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mime = new Mail_mime('rn');
$mime->setTXTBody($body_text);
$mime->setHTMLBody($body_html);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$host = "mail.authsmtp.com";
$port = 26;
$username = "my_username";
$password = "whatever_password";
$mailer = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password));
if (PEAR::isError($res)) {
throw new Exception($res->getMessage());
} else {
return true;
}
AuthSMTP is giving me the following error:
SMTP: Invalid response code received from server (code: 428, response: 4.0.0 Your account is using SSL - either disable it in your email client or enable it at http://control.authsmtp.com)
The easiest way is to change $tls=true to $tls=false in the definition of the function auth in PEAR\NET\SMTP.php
This can't be done with a current release of the PEAR Mail package - but is a requested feature. I've uploaded a patch so that this can be done. Hopefully a new release will be distributed soon.
Switched to using PHPMailer instead and had it working in 5 minutes.

Categories