【发布时间】:2022-01-16 23:11:55
【问题描述】:
PHPMailer 在一台主机上发送电子邮件,但不会使用完全相同的代码在另一台主机上发送。
服务器保护免受电子邮件,因此他们表示要从其他主机或任何主机发送电子邮件,我必须使用ini_set( 'sendmail_from', 'email@example.com' );。好吧,该解决方案适用于普通的 PHP 邮件功能mail(),但不适用于 PHPMailer。
有人能帮我弄清楚如何在 PHPMailer 上解决这个问题吗?
以下是我的代码,它已经在另一个主机上进行了测试,并且仅在当前服务器上正常工作,尽管使用了ini_set(),但没有发送电子邮件
require_once('email-sending/PHPMailer/PHPMailer.php');
require_once('email-sending/PHPMailer/Exception.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/*!
*
*
* This class handles the sending and processing of emails
*
* */
class Email{
const BASE_URL = 'http://cryptodegen.co.uk'; // please enter domain name of your website without the trailing slash, e.g https://example.com
const SHORT_DOMAIN = 'cryptodegen.co.uk'; // Enter short domain without https e.g example.com
const SYSTEM_NAME = 'Crypto Degen '; // Website Name
const LOGO_URL = self::BASE_URL . '/email-sending/logo/logo-black1.png'; // your website logo, if blank emails will be sent without logo
const FOOTER_TEXT = 'Crypto Degen, San Francisco CA 94102, This text is editable'; // footer text
const EMAIL_SENDER = 'noreply@cryptodegen.co.uk'; //
const REPLY_TO = 'info@cryptodegen.co.uk'; // senders will reply to
/*!
*
* $to = recepient, $subject = Email Subject, $message = HTML MESSAGE
*
* $ttachements should be array of files url
*
*
* $attachemnet is optional, you can jsut say:
* Email::send( 'catekhui@gmail.com', 'Urgent subject', Hello html email message' );
*
*
* */
static function send( $to, $subject, $message, $attachments = array() ){
try {
ini_set( "sendmail_from", self::EMAIL_SENDER );
$email = new PHPMailer( true );
$email->SetFrom( self::EMAIL_SENDER, self::SHORT_DOMAIN );
$email->Subject = $subject;
$email->Body = get_email_template( $message );
$email->AddAddress( $to );
$email->isHTML(true);
$email->addReplyTo( self::REPLY_TO, '' );
foreach ($attachments as $key => $value) {
// code...
$email->AddAttachment( $value , basename( $value ) );
}
$email->Send();
return true;
} catch( Exception $e ){
?>
<script>
window.onload = function() {
alert("<?php echo $e; ?>");
}
</script>
<?php
}
return false;
}
}
【问题讨论】:
-
这可能会解决您的问题stackoverflow.com/questions/32435395/…