Reputation: 237
help please send an email to.
I use a popular script phpmailer. examples on the page there is a pattern to send a message. I use a certain email as follows:
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer mail() test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
resulting in at run script does displays an inscription Message sent! _. however, the letter did not come to the specified mailbox. the problem is not clear because the error message is not displayed.
Upvotes: 0
Views: 665
Reputation: 4288
Well, I am not sure if you really need it, but I did a class some time ago to send emails easily using PHPMailer, you can use it if you want:
<?php
Namespace Email;
include_once 'class.phpmailer.php';
use PHPMailer;
Class Email {
private $mail_host = "smtp host";
private $mail_port = "smtp port";
private $mail_user = "user";
private $mail_pass = "pass";
public function sendMail($fromName, $sendAddress, $cc, $bcc, $reply, $from, $subject, $body)
{
$mail = new PHPMailer(true);
$mail->ClearAddresses();
$mail->SetLanguage("es", "");
$mail->CharSet = "UTF-8";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = $this->mail_host;
$mail->Port = $this->mail_port;
$mail->Username = $this->mail_user;
$mail->Password = $this->mail_pass;
$mail->IsHTML(true);
try {
if ($cc != false) {
if (is_array($cc)) {
foreach($cc as $value) {
$mail->AddCC($value);
}
}
else {
$mail->AddCC($cc);
}
}
if ($bcc != false) {
if (is_array($bcc)) {
foreach($bcc as $value) {
$mail->AddBCC($value[0]);
}
}
else {
$mail->AddBCC($bcc);
}
}
if (is_array($sendAddress)) {
foreach($sendAddress as $value) {
$mail->AddAddress($value);
}
}
else {
$mail->AddAddress($sendAddress);
}
$mail->AddReplyTo($reply, $fromName);
$mail->SetFrom($from, $fromName);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->Send();
return true;
}
catch(Exception $e) {
error_log("exception: " . $mail->ErrorInfo, 0);
return false;
}
}
}
Upvotes: 1