This question already has an answer here: What is the advantage of using wp_mail? (1 answer) Closed 7 years ago.admin管理员组文章数量:1024320
I have an email system that I am making in a WordPress plugin to send the emails with this code:
include(PLUGIN_DIR.'config/class_phpmailer.php');
$email = new PHPMailer();
$email->From = get_option('admin_email');
$email->FromName = 'my name';
$email->Subject = $subject;
$email->Body = $body;
foreach($emails as $mail) {
if(is_email($mail)) {
$email->addAddress($mail);
}
}
if($name) {
$att = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
$email->AddAttachment($att);
}
// check if email is sent or not
if(!$email->Send()) {
echo "Message was not sent <br />PHPMailer Error: " . $email->ErrorInfo;
}
else {
echo "Message has been sent";
$_POST = array();
}
This works great. Tested it online and sends to all the tested emails.
Can I make it like this? I mean, with phpmailer class not native to WP. Is this wrong? Is it best to use native WP phpmailer? If so, how can I do it?
Thanks in advance.
This question already has an answer here: What is the advantage of using wp_mail? (1 answer) Closed 7 years ago.I have an email system that I am making in a WordPress plugin to send the emails with this code:
include(PLUGIN_DIR.'config/class_phpmailer.php');
$email = new PHPMailer();
$email->From = get_option('admin_email');
$email->FromName = 'my name';
$email->Subject = $subject;
$email->Body = $body;
foreach($emails as $mail) {
if(is_email($mail)) {
$email->addAddress($mail);
}
}
if($name) {
$att = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
$email->AddAttachment($att);
}
// check if email is sent or not
if(!$email->Send()) {
echo "Message was not sent <br />PHPMailer Error: " . $email->ErrorInfo;
}
else {
echo "Message has been sent";
$_POST = array();
}
This works great. Tested it online and sends to all the tested emails.
Can I make it like this? I mean, with phpmailer class not native to WP. Is this wrong? Is it best to use native WP phpmailer? If so, how can I do it?
Thanks in advance.
Share Improve this question edited May 13, 2019 at 23:19 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Jun 26, 2017 at 10:45 eskimopesteskimopest 1491 silver badge7 bronze badges 4 |1 Answer
Reset to default 7WordPress offers its own mailing system. I suggest you use that instead of PHP's native mail.
wp_mail
accepts five arguments:
wp_mail( $to, $subject, $message, $headers, $attachments );
So your code can be written this way:
// Set the headers
$headers[] = 'From: ' . get_option('admin_email');
// Add the addresses to an array
foreach($emails as $mail) {
if(is_email($mail)) {
$to[] = $mail;
}
}
// Add the attachment
if($name) {
$att[] = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
}
// Send the emails
$results = wp_mail( $to, $subject, $body, $headers, $att );
// Output the results
echo $results;
Simple and short.
This question already has an answer here: What is the advantage of using wp_mail? (1 answer) Closed 7 years ago.I have an email system that I am making in a WordPress plugin to send the emails with this code:
include(PLUGIN_DIR.'config/class_phpmailer.php');
$email = new PHPMailer();
$email->From = get_option('admin_email');
$email->FromName = 'my name';
$email->Subject = $subject;
$email->Body = $body;
foreach($emails as $mail) {
if(is_email($mail)) {
$email->addAddress($mail);
}
}
if($name) {
$att = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
$email->AddAttachment($att);
}
// check if email is sent or not
if(!$email->Send()) {
echo "Message was not sent <br />PHPMailer Error: " . $email->ErrorInfo;
}
else {
echo "Message has been sent";
$_POST = array();
}
This works great. Tested it online and sends to all the tested emails.
Can I make it like this? I mean, with phpmailer class not native to WP. Is this wrong? Is it best to use native WP phpmailer? If so, how can I do it?
Thanks in advance.
This question already has an answer here: What is the advantage of using wp_mail? (1 answer) Closed 7 years ago.I have an email system that I am making in a WordPress plugin to send the emails with this code:
include(PLUGIN_DIR.'config/class_phpmailer.php');
$email = new PHPMailer();
$email->From = get_option('admin_email');
$email->FromName = 'my name';
$email->Subject = $subject;
$email->Body = $body;
foreach($emails as $mail) {
if(is_email($mail)) {
$email->addAddress($mail);
}
}
if($name) {
$att = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
$email->AddAttachment($att);
}
// check if email is sent or not
if(!$email->Send()) {
echo "Message was not sent <br />PHPMailer Error: " . $email->ErrorInfo;
}
else {
echo "Message has been sent";
$_POST = array();
}
This works great. Tested it online and sends to all the tested emails.
Can I make it like this? I mean, with phpmailer class not native to WP. Is this wrong? Is it best to use native WP phpmailer? If so, how can I do it?
Thanks in advance.
Share Improve this question edited May 13, 2019 at 23:19 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Jun 26, 2017 at 10:45 eskimopesteskimopest 1491 silver badge7 bronze badges 4-
WordPress already uses PHPMailer under hood - what do you expect from implementing this on your own? Just use
wp_mail()
and don't worry – kero Commented Jun 26, 2017 at 10:49 - 1 You didn't mention email volume; if more than "nominal" then consider using a separate SMTP service; there are plenty of good SMTP mailer plugins for WP. Getting consistent successful email delivery is part art and part science. – C C Commented Jun 26, 2017 at 12:24
- You may as well use the built in one. its basically PHP Mailer built into the core. – Andi Wilko Commented Jun 26, 2017 at 12:40
- @C C, you talk about email volume. this is for an mailing list im am making and i have up to 500 emails addresses. what do you mean by using separate SMTP service? tks. – eskimopest Commented Jun 26, 2017 at 15:07
1 Answer
Reset to default 7WordPress offers its own mailing system. I suggest you use that instead of PHP's native mail.
wp_mail
accepts five arguments:
wp_mail( $to, $subject, $message, $headers, $attachments );
So your code can be written this way:
// Set the headers
$headers[] = 'From: ' . get_option('admin_email');
// Add the addresses to an array
foreach($emails as $mail) {
if(is_email($mail)) {
$to[] = $mail;
}
}
// Add the attachment
if($name) {
$att[] = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
}
// Send the emails
$results = wp_mail( $to, $subject, $body, $headers, $att );
// Output the results
echo $results;
Simple and short.
本文标签: emailShould I use wpmail or PHP39s mail
版权声明:本文标题:email - Should I use wp_mail or PHP's mail? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745498088a2153260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_mail()
and don't worry – kero Commented Jun 26, 2017 at 10:49