Sending a simple attached file via PHP mail() function - php

I'm going to give this another try because my last question might have been confusing.
I have a simple web form consisting of the following some inputs (for now, pretend i have two inputs, name and file input). I want the user to upload a document (if possible restrict to .doc, .docx, .pdf, if this is not possible to accomplish, let's just restrict to .doc), and i want to restrict the size to under 2MB.
Let me rephrase this. The file to be attached is NOT on the webserver. It will dynamically be uploaded to a temporary folder, send via the mail script, and then deleted.
If this is possible to accomplish, please, I need all the help that I can get.
I've tried Swiftmailer, PHPMailer, PEAR, I can't seem to get them to work. All I need is a simple script to send an attached file, nothing more. No validation necessary, nothing.
Any help would be greatly appreciated.
Thanks a lot,
Amit

It is possible to do with all 3 libraries you listed (PHPMAiler, PEAR and Swiftmailer).
For PHPMailer you can see a tutorial here:
require_once '../class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK<P></P>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
AddAttachment will take a file from your server.
How to upload a file form an HTML form can be found here. Once your email is sent you can delete (unlink) the file from the server.
The PHP manual can help you to better undersand file uploads.
All you want to do is easy to achieve, but it's longer to explain than do it :) But with all the links I gave you you have everything you need. If you have specific questions let me know.

Related

website generated e-mails are not reaching all users (php mailer)

I am building a website for a group of kitchen chefs that allows new members to subscribe without any action from the owner.
When subscribing, new users should receive an e-mail with an activation link, in order to validate their address.
In the beginning I was using the function mail(), but a few addresses weren't receiving anything from the website, so I switched to php mailer.
php mailer code (no errors shown during execution, before I had echoes to check)
require_once ('layout/phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo("$NoreplyMailURL","$NoreplyMailNAME");
$mail->AddAddress($mdest);
$mail->SetFrom("$NoreplyMailURL","$NoreplyMailNAME");
$mail->AddReplyTo("$NoreplyMailURL","$NoreplyMailNAME");
$mail->Subject = "$mobj";
$mail->AltBody = strip_tags ($mmex); // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($mmex);
$mail->Send();
}catch (phpmailerException $e) {
} catch (Exception $e) {
}
$NoreplyMailURL is the (existing) web site e-mail address
$NoreplyMailNAME is something like "Chef Enrico"
$mobj is a sentence like "new password for our website" or "account activation link"
$mmex is the message text formatted in html
Gmail, some hotmail and other users are receiving our e-mails,
some hotmail, bluewin.ch and libero.it are not.
Do you have any suggestions on how to make this code stronger? Is it possible that if receiving providers does not support HTML formatting, the e-mail is not shown? I guess they should see all tags if that was the case, am I wrong?
Thank you very much in advance!

Php Mailer - continue with next mail if sent() error occurs

I am new to use phpmailer for sending bulk mails.
The execution stops and shows error if send() fails.
Is there any way to skip the error and continue with next email id.
I am using email id's from database.
You Can Catch Exception Mechanism, to continue with next Mail with appropriate logic
Posted by Phil
PHPMAiler uses Exceptions. Try to adopt the following
require_once '../class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
use can use phplist for bulk mailing with PHP mailer or without
click Here for more
In PHP, you can suppress error display by adding # to the function. This will usually prevent the error from being displayed.
For your situation, you can put the code in try..catch. In the catch section, check if the all the emails have been sent; if not, just continue with the next one. This also means that you need a way to track the total emails and the number of emails completed.
Hope this helps.

PHP email to IMAP account

I'm using PHPmailer to send an email from a website to the website owner. It works fine to some addresses (e.g. my Gmail account), and it used to work to the owner's address, but he's recently changed from POP to IMAP and now he doesn't receive emails from the website. He does receive emails from other sources. This is the code:
$mail = new PHPMailer(true);
try {
$mail->AddAddress($to, 'Example To');
$mail->SetFrom('example#example.com', 'Example');
$mail->AddReplyTo('example#example.com', 'Example');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($message);
$mail->Send();
//echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Any advice much appreciated.
Thanks!
G
That has nothing to do with the PHP code. The IMAP protocol is just responsible for fetching the mails from the server as a user (with IMAP the mails stay on server ... you have an open stream for a long time ...).
So: did he switch to another email provider? Maybe it is in spam. Check the maillog! ;-)

Sending a HTML email with a midi file that needs to play automatic on open

I'm trying to accomplish the following, I'm sending a HTML email using PHP Mailer that reads a html file and embedding a midi file within the HTML file, and it then sends out the email and then the midi file should start playing automatically once the email is opened, is this possible, since it does not seem to work, I'm using Evolution to view the email.
My code looks like this,
HTML FILE "If i open this in my browser it plays but not in email"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Template</title>
</head>
<body>
<h1>Song is playing</h1>
<embed src="http://test.mydomain.co.za/song.mid" autostart="true" loop="true" hidden="true" />
</body>
</html>
PHP Mailer code
$email = $_GET['email'];
//Including the PHP mailer class
require_once 'class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddAddress($email);
$mail->SetFrom('webmaster#mydomain.co.za', 'Webmaster');
$mail->AddReplyTo('webmaster#mydomain.co.za', 'Webmaster');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('template.html'));
$mail->Send();
echo "Message Sent OK</p>\n";
}catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
Is this at all possible? And how?
That would be the most annoying email in the world!
Thankfully, most mail clients do not recognise media tags or embedded flash.
Others are already told you why it's a bad idea, so I am going to skip that.
There is no reliable way to do what you want.
Not even in browsers, and we only have a few popular ones. But when you're dealing with email clients, you have to deal with desktop clients (even the ones that are crossplatfom can behave differently) and web-based clients, together you get a gazillion ways why your idea is impossible.
Don't. Seriously. If you want the mail recipient to get the midi, then put a link in the body of the mail. The only thing I can imagine more annoying than a web page that automatically plays music is an email that does.

Two emails when sent via SMTP

I've used two PHP email scripts and routing it through my SMTP server, when I do this though it sends two of the same email.
When I use mail() this doesn't happen, but I'd much rather use SMTP.
Any ideas why this may be occuring?
If you're setting the 'To' and/or 'Recipient' header multiple times, the SMTP server could interpret that as separate e-mail address, thus you'll receive the multiple e-mails.
I'd recommend using the PEAR Mail class. Very simple to use, and handles much of the work for you. It supports multiple backends including SMTP. Likewise, if you want to expand your class to send HTML emails, the Mail_Mime class handles this very nicely, providing methods to set the plain-text body and the HTML body (in case the recipient doesn't support HTML).
So if you're only using PHPMailer without editing it's code, it's not your script's fault. Maybe check your SMTP server's configuration?
function send_email($from, $fromname, $to, $subject, $body, $alt = '')
{
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try
{
$mail->Host = 'localhost'; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//$mail->AddReplyTo($from, $fromname);
$mail->AddAddress($to);
$mail->SetFrom($from, $fromname);
$mail->Subject = $subject;
//$mail->AltBody = $alt; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo 'Message Sent OK';
}
catch (phpmailerException $e)
{
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
echo $e->getMessage(); //Boring error messages from anything else!
}
}
That's the current function so far
Based on your code, if it's the class which is at fault, you'd expect to get 'Message Sent OK' twice (I can't see why that would happen though). If you don't, then I'd be looking at your SMTP server (maybe via a call to support).
I'm assuming you've disabled Reply-to to rule it out as a cause in this case? Note: I'm not suggesting that would affect anything (other than you likely being classified as spam).
Incidentally, I moved from PHPMailer to Swift Mailer some time ago & have never looked back. If you don't get any joy from support then I'd try at least testing with Swift Mailer.
I agree with what da5id said, why dont you take the second error message out. Further have you checked the receiver whether they REALLY get 2 messages?

Categories