I have a word with a special character in it that does not display properly in the subject line of an email (via mail()).
The word is O’Conner and the special character is ’
"as is" the word appears as O’Conner in the subject line of the incoming email message.
When I use html_entities_decode()the word appears as O’Conner in the subject line of the incoming email message.
When I use mb_encode_mimeheader("O’Conner", "UTF-8", "Q") the word appears as O’Conner in the subject line of the incoming email message.
Is there a way to get the word to appear is it should?
Other similar or seemingly duplicate question/answers do not see to resolve this particular issue.
I even tried UTF-7 -- that did not work.
Here's the email function:
/**
* Send an html/smtp email
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function htmlEmailer( $email, $type, $parameter ) {
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/Exception.php');
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/PHPMailer.php');
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php');
mb_internal_encoding('UTF-8'); // 2021-06-18 added per https://stackoverflow.com/questions/7669668/how-to-use-special-characters-in-recipients-name-when-using-phps-mail-function/7670192#7670192
// Instantiation and passing "true" enables exceptions
$mail = new PHPMailer(true); // create a new object
//Server settings
$mail->SMTPDebug = 0; // see best_practices.txt file for settings
$mail->isSMTP(); // set mailer to use SMTP
$mail->Host = SMTP_SERVER; // specify main and backup SMTP servers separated by semi-colon (defined in config.php)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = SMTP_USER; // SMTP username (defined in config.php)
$mail->Password = SMTP_PASSWORD; // SMTP password (defined in config.php)
$mail->SMTPSecure = 'tls'; // enable encryption [tls or ssl]
$mail->Port = 587; // TCP port to connect to [465 or 587]
//Recipients
$mail->setFrom('info#example.com', mb_encode_mimeheader(SENDER_LAST_NAME, 'UTF-8', 'Q'));
$mail->addAddress($email, ''); // add a recipient [2nd parameter "recipient's name" is optional and is separated by a comma]
$mail->addReplyTo('no_reply#example.com', 'Do Not Reply');
//$mail->addCC('me#example.com');
$mail->addBCC('you#example.com');
// Attachments
//$mail->addAttachment('/home/cpanelusername/attachment.txt'); // Add attachments
//$mail->addAttachment('/home/cpanelusername/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'The message subject';
$mail->Body = 'HTML message body'; // html formatted body
$mail->AltBody = "Plain Text message body"; // plain text body
// Send the Message
$mail->send();
} // htmlEmailer()
Just replace it before sending:
$email_to = str_replace("’", "'", $email_to);
Related
this query may sounds silly but it has become a headache for me. i am using ampps... I need to implement mail notification in my project. i have downloaded the PHPmailer rar file and extracted to my project folder.
It contains,
*get_oauth_token.php
*src-Exception.php-OAuth.php-PHPMailer.php-POP3.php-SMTP.php
I got only this files in that folder.
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Here i am getting an error message saying,
Warning: require(vendor/autoload.php): failed to open stream: No such
file or directory in C:\Program Files
(x86)\Ampps\www\emailpro\email.php on line 8
By googling I got to know that by composer we can download classes. I tried that also. >composer require phpmailer/phpmailer – ERROR- 'composer' is not recognized as an internal or external command, operable program or batch file.
I am totally confused pls anyone tell me how to implement this PHPmailer to the project.
Please help how to configure from scratch i have googled lot and i didnt got the required answer.
If you don't want to install composer then use this code for php mailer to work. It is working in my projects. You can also remove the part of code which you don't need like attachments. One more thing if you want to hide the debugging code errors and notifications remove or comment this line $mail->SMTPDebug = 2;
For more help visit this link https://github.com/PHPMailer/PHPMailer
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}`enter code here`
I have a small php website that sends a text message to any given number in US. I tried sending my self a text using it but didn't receive it. I sent it to my cousin's number and he received it. I am using Simple Mobile as my carrier and my cousin uses T-mobile. Could it be that somehow this message is being converted to short message? If so, is there any way I can fix it ? Because in future, I will need to send text messages to any given number and it would be nice if all intended recipients receive the texts without having to enable any thing.
Here is my code :
require 'PHPMailer-master/PHPMailerAutoload.php';
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPDebug = 2; // This will print debugging info
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Encoding = '7bit'; // SMS uses 7-bit encoding
// Authentication
$mail->Username = "example#gmail.com"; // Login
$mail->Password = "password"; // Password
// Compose
$mail->Subject = "Testing"; // Subject (which isn't required)
$mail->Body = "Testing"; // Body of our message
// Send To
$mail->AddAddress( "phoneNumber#tmomail.net" ); // Where to send it
var_dump( $mail->send() ); // Send!
I am working on ticketing system in PHP. I convert mails to tickets. When a user replies to the mail from the Ticketing system, it is sending the mail to the customer as a new mail. No message/mail threading.
I think, my problem is related to the added ticket id at the end of the subject. (e.g. Subject: Installation Problem [#EMSY45])
I have passed the Message ID and References in the Header
I am using PHPMailer to send the mail.
Here is my code:
$mail = new PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = str_replace('/', '', $host); // Specify main and backup SMTP servers
if($outgoing_server_details['smtp_auth'] == 1)
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $outgoing_server_details['server_username']; // SMTP username
$mail->Password = $outgoing_server_details['server_password']; // SMTP password
$mail->SMTPSecure = $protocol; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $port;
$mail->setFrom($outgoing_server_details['from_email_field'], $from_name);
$mail->addAddress($data['_from'], $to_name); // Add a recipient
$mail->addReplyTo($outgoing_server_details['from_email_field'], $reply_to_name);
$message_id = $data['message_id'];
$mail->AddCustomHeader('In-Reply-To', $message_id);
$mail->AddCustomHeader('References', $message_id);
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $data['subject'];
$mail->Body = $mail_content;
$mail->send();
If you need to add ticket ID in the end Subject of the E-Mail then try this.
$mail->Subject = "Installation Problem [".$message_id."]";
OR
$mail->Subject = $data['subject'].$message_id;
And one more Correction
$mail->isHTML(true);//should always come after the Body is set.
i.e.,
$mail->Subject = $data['subject'].$message_id;
$mail->Body = $mail_content;
$mail->isHTML(true);
$mail->send();
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I am trying to send an email with my server. I am using php's mail function. The function is returning true. But I am not receiving any email. I have checked the logs. And even the logs are not showing any error. My domain is "islamerkotha.com". My code is given below -
<?php
$msg = "First line of text\nSecond line of text";
$msg = wordwrap($msg,70);
$headers = "From: test1#islamerkotha.com";
mail("erfan.bashar.13#gmail.com", "My subject", $msg, $headers);
Thank you.
There are many points along the path an email message takes where yours could be failing, but take a look at the PHP mail() function page; it specifically says that the function returns true if the message was accepted for delivery and "it is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination."
EDIT: Look here for more information on PHP error reporting. If you don't see any errors then, look at phpinfo() to see if the mail() function is even enabled. If it is, then it's time to start looking further downstream...
Try PHP Mailer, with this, you doesn't need to have local SMTP server and also you can also use your validations
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I have a webapp to send mails and using PHPMailer.
I have generated a contact list which stores the email addresses in database.
Now in my mail function(web page), I do not have any field to enter email address manually, rather I would like to fetch the email id's from table and send mail to all at one click.
Can someone please help me with this, not knowing what to do.
public function actionSendMail(){
$model = YourModelForMails::model()->findall();
foreach($model as $m){
$this->mailfunction($m['emailcolumn']);
}
}
public function mailfunction($email){
sendyourmail to: $email;
}
SoI will not code for you the app but will give really useful hint to make you workout your app.
Use Yii CActiveRecord based Model to fetch User Data (Normally containing Email and Other Details)
Put PHPMailer in vendors Folder and Import using alias path vendor (Hint Yii::import)
Use PHPMailer and Do the Sending. Here I attach PHPMailer's own example
Put Effort to Make them work together. if you cannot, it means you need to put your work aside and go learning!
Code:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';