Monday, February 21, 2011

php emailer your email your name reply to attach file subject?

After downloading archived files, unzip them and upload to the web server. Here's a simple code forsending email:

IsMail();

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";
$mail->Body = "Test 1 of PHPMailer.";

if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Letter sent";
}
?>

The function $mail->IsMail(); indicates that the letter must be sent using mail() function. Other methods are:

IsSendmail - via sendmail command.
IsQmail - directly via qMail MTA.
IsSMTP - via SMTP server.

Here's a sample for using SMTP. We assume that SMTP requires authorization. If it in't nessesary, just write $mail->SMTPAuth = false;. To use a number of servers use semicolumn for delimiter.

IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";
$mail->Body = "Test 1 of PHPMailer.";

if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Letter is sent";
}?>

To add inforation about sender, use following functions:

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com"; // indicates ReturnPath header
$mail->AddReplyTo("replies@example.com", "Replies for my site"); // indicates ReplyTo headers

For specifying various types of recepients use these:

$mail->AddAddress("mail1@domain.com", "Recepient 1");
$mail->AddCC("mail1@domain.com", "Recepient 1");
$mail->AddBCC("mail1@domain.com", "Recepient 1");

If you need 2 or more To: addresses, just call.
Additional languages
If the message contains cyrilic or other non-latin characters like unicode, you should specify the charset used:

$mail->CharSet="windows-1251";
$mail->CharSet="utf-8";
HTML content
HTML emails are better formatted and more attractive. As there are mail clients that do not support HTML, you should provide alterntive text only content. Here's a sample:

IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com";
$mail->AddReplyTo("replies@example.com", "Replies for my site");

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";

$mail->IsHTML(true);
$mail->Body = "

Test 1 of PHPMailer html

This is a test

";
$mail->AltBody="This is text only alternative body.";

if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Letter is sent";
}
?>
Images
There are two ways to add images in the HTML content. You can specify absolute address that points image on your site. The case is by using this technique, one can track down who opens the email. That's why most emal clients do not display such images. To code around this, you can attach the image in the message and link to it by a special URI.

IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com";
$mail->AddReplyTo("replies@example.com", "Replies for my site");

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";

$mail->IsHTML(true);
$mail->AddEmbeddedImage('logo.jpg', 'logoimg', 'logo.jpg'); // attach file logo.jpg, and later link to it using identfier logoimg
$mail->Body = "

Test 1 of PHPMailer html

This is a test picture:

";
$mail->AltBody="This is text only alternative body.";

if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Letter is sent";
}
?>
Attachments
You may need to attach a file to the letter. Use AddAttachment() for that purpose:

IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com";
$mail->AddReplyTo("replies@example.com", "Replies for my site");

$mail->AddAddress("email@example.com");
$mail->Subject = "Your invoice";

$mail->IsHTML(false);
$mail->AddAttachment('files/invoice-user-1234.pdf', 'invoice.pdf'); // attach files/invoice-user-1234.pdf, and rename it to invoice.pdf
$mail->Body = "Please find your invoice attached.";
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Letter is sent";
}
?>

No comments:

Post a Comment

Post a Comment