I have the following setup:
$message = file_get_contents('./site/resources/external/template.html');
$message = str_replace('[name]', $email['name'], $message);
$message = str_replace('[password]', $password, $message);
require './site/resources/external/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPSecure = 'tls';
$mail->Username = "mymail";
$mail->Password = "mypassword";
$mail->AddAddress($email);
$mail->FromName = "Marc";
$mail->CharSet = 'UTF-8';
$mail->Subject = "Din nye adgangskode";
$mail->msgHTML($message, dirname(__FILE__));
$mail->Host = "smtp.live.com";
$mail->Port = 587;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
$mail->Send();
However when i try to send it, it does not send.
However when i do like this:
However when i do the following:
$mail->msgHTML(str_replace('[password]', $password, file_get_contents('./site/resources/external/template.html')), dirname(__FILE__));
it sends without a problem.
So how can i add multiple variables to my template ?
Although I don't see how the variable substitution of $message could be breaking your code, there is a multiple replacement form of str_replace that uses array arguments:
$mail->msgHTML(
str_replace(
array('[password]','[name]'),
array( $password, $email['name'] ),
file_get_contents('./site/resources/external/template.html')
),
dirname(__FILE__)
);
I suspect something else is wrong with your code, but this might be useful.
Related
I'm using Migadu mail server and PHP Mailer to set up a simple contact form.
This is the PHP file:
<?php
require("includes/class.phpmailer.php");
require("includes/class.smtp.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$smtpHost = "smtp.migadu.com";
$smtpUsername = "info#mywebsite.com";
$smtpPassword = "mypassword";
$to = 'info#mywebsite.com';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->Host = $smtpHost;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->From = $smtpUsername;
$mail->FromName = $name;
$mail->AddAddress($to);
$mail->AddReplyTo($email);
$mail->Subject = "Contact Form";
$body = '<h1>Contact!</h1>';
$body .= '<p><b>Name: </b>'.$name.'</p>';
$body .= '<p><b>Email: </b>'.$email.'</p>';
$body .= '<p><b>Message: </b>'.$message.'</p>';
$mail->Body = $body;
$sentStatus = $mail->Send();
if($sentStatus){
echo json_encode(['status' => 'ok']);
}else{
echo json_encode(['status' => 'error', 'errorType' => 'server']);
}
?>
But it's not sending the mail. Also, it's not throwing any error, it just stuck in loading forever.
It's not showing any error because you're not displaying any errors - look in the ErrorInfo property. All of the examples provided with PHPMailer do this, so go look at them to see how to do that.
It's most likely that it's not "stuck forever", it's just that the timeout is long, and that's probably because your ISP blocks outbound SMTP, which is very common. The troubleshooting guide tells you how to diagnose this. Your ISP probably has an alternative method for sending email, for example they may provide their own relay, so you should refer to their docs.
Inconsistent duplicate emails occurring when using php mailer.
Function that mails:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require( GetPHPMailPath() );
$mail = new PHPMailer();
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!='') {
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->Username = "email#email.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "email"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
How I am calling the function:
echo SendEmail($toAddress,$ccAddress,$bccAddress,$subject,$body);
The really odd part about this whole ordeal is that it is inconsistent which means there may be nothing wrong with the code but the connection to gmail?
Any ideas maybe its a php.ini problem?
This was a lag related issue.
PHPMailer functioned properly. User was sending duplicate requests. Fixed by adding comparison check with MySQL database records.
So, i'm having problem with this code.
It's a form where it get the data and this is the phpmailer fragment, where i get the error Could not acess file
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message= $_REQUEST['message'];
$message= "--$boundary" . PHP_EOL;
$message= "Content-Type: text/html; charset='utf-8'" . PHP_EOL;
$message= "--$boundary" . PHP_EOL;
$tapete=$_REQUEST['tapete'];
$medidas=$_REQUEST['medidas'];
$cliente=$_REQUEST['cliente'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "mucapapipa.br#gmail.com";
$mail->Password = "----";
$mail->Subject = 'Formulário FacilityCom';
$mail->setFrom = ('mucapapipa.br#gmail.com');
$mail->Body = 'Tipo e marca: $tapete \nMedidas: $medidas \nCliente: $cliente\n $from';
$mail->IsHTML(true);
$mail->AddAttachment($file['tmp_name'], $file['name']);
$mail->Send();`
There's a lot wrong here.
Don't mess with boundaries - PHPMailer does all that for you.
$mail->setFrom = ('mucapapipa.br#gmail.com') should be $mail->setFrom('mucapapipa.br#gmail.com').
It would help if you based your code on the file upload example provided with PHPMailer as that shows how to handle file uploads correctly.
I'm trying to configure email by using phpmailer and smtp but Authentication error appears.
Code below
include_once("phpmailer.php");
$mail = new PHPMailer();
$tarih = date("d.m.Y - H:i:s");
$mail->IsSMTP();
$mail->Host = "mail.golcukdh.gov.tr";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "password";
$mail->IsHTML(true);
$mail->From = "email";
$mail->AddAddress("email");
$mail->Subject = "subject";
$mail->Body = "Body Part";
$mail->Send()
?>
I tried to configure this by using asp.net with these authentication information and it was successfull. But I want to use php, what is wrong ?
I am using PHPMailer API for sending emails. I was wondering how can I send the subject in Arabic (non-English) language
$mail->CharSet = 'utf-8';
$array= FetchTable('cos');
$subject = $_POST['subject'];
$body = $_POST['body'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "host";
$mail->SMTPAuth = true;
$mail->Username = "yaz#enfaltourism.com";
$mail->Password = "*******";
$mail->Port = "587";
$mail->From = "yaz#enfaltourism.com";
$mail->FromName = "Enfaltourism";
$mail->Subject = $subject;
$mail->AddAddress($email);
$mail->Send();`
The email is being successfully sent, but the problem is in sending subject in Arabic language. The email message body is displayed properly in Arabic after I set the char encoding but the the subject is being displayed in weird characters
Update
include("../mail/class.phpmailer.php");
$array= FetchTable('cos');
$subject = $_POST['subject'];
$body = $_POST['body'];
$mail = new PHPMailer();
$mail->CharSet = 'utf-8';
$mail->IsSMTP();
$mail->Host = "mail.enfaltourism.com";
$mail->SMTPAuth = true;
$mail->Username = "yaz#enfaltourism.com";
$mail->Password = "*****";
$mail->Port = "587";
$mail->From = "yaz#enfaltourism.com";
$mail->FromName = "Enfaltourism";
$mail->Subject = $subject;
i fixed as u told but the subject still weird character like this
I had this same problem with Japanese characters, both in the body and in the subject line:
I resolved it (email looks outstanding now) by doing:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
There really isn't any need to encode the subject separately so long as the object has the CharSet property set to UTF-8.
you can try using this code. Let me know if it helps
$phpmailer->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";
Try encoding subject with base64_encode
$subject = '=?UTF-8?B?'.base64_encode('سلام علیکم').'?=';
However with setting the issue must be solved.
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
Try this:
$mail->CharSet = 'UTF-8';
$mail->Subject = html_entity_decode("Recuperación de contraseña");
I did use this in my code and it worked!
Results